|
| 1 | +use crate::util; |
| 2 | +use cspuz_rs::graph; |
| 3 | +use cspuz_rs::serializer::{ |
| 4 | + problem_to_url, url_to_problem, Choice, Combinator, Dict, Grid, Spaces, |
| 5 | +}; |
| 6 | +use cspuz_rs::solver::Solver; |
| 7 | + |
| 8 | +pub fn solve_fillmat( |
| 9 | + clues: &[Vec<Option<i32>>], |
| 10 | +) -> Option<graph::BoolInnerGridEdgesIrrefutableFacts> { |
| 11 | + let (h, w) = util::infer_shape(clues); |
| 12 | + |
| 13 | + let mut solver = Solver::new(); |
| 14 | + let num = &solver.int_var_2d((h, w), 1, 4); |
| 15 | + let is_border = graph::BoolInnerGridEdges::new(&mut solver, (h, w)); |
| 16 | + solver.add_answer_key_bool(&is_border.horizontal); |
| 17 | + solver.add_answer_key_bool(&is_border.vertical); |
| 18 | + solver.add_expr( |
| 19 | + num.slice((.., ..(w - 1))) |
| 20 | + .ne(num.slice((.., 1..))) |
| 21 | + .iff(&is_border.vertical), |
| 22 | + ); |
| 23 | + solver.add_expr( |
| 24 | + num.slice((..(h - 1), ..)) |
| 25 | + .ne(num.slice((1.., ..))) |
| 26 | + .iff(&is_border.horizontal), |
| 27 | + ); |
| 28 | + for i in 1..=4 { |
| 29 | + solver.add_expr(!(num.eq(i).slice((..(h - 1), ..(w - 1))) & num.eq(i).slice((1.., 1..)))); |
| 30 | + solver.add_expr(!(num.eq(i).slice((..(h - 1), 1..)) & num.eq(i).slice((1.., ..(w - 1))))); |
| 31 | + } |
| 32 | + graph::graph_division_2d(&mut solver, num, &is_border); |
| 33 | + |
| 34 | + for y in 0..h { |
| 35 | + for x in 0..w { |
| 36 | + if let Some(n) = clues[y][x] { |
| 37 | + if n >= 0 { |
| 38 | + solver.add_expr(num.at((y, x)).eq(n)); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // No 4 intersections (technically redundant by theory but keeping it for optimization) |
| 45 | + for y in 1..h { |
| 46 | + for x in 1..w { |
| 47 | + let left = &is_border.horizontal.at((y - 1, x - 1)); |
| 48 | + let right = &is_border.horizontal.at((y - 1, x)); |
| 49 | + let up = &is_border.vertical.at((y - 1, x - 1)); |
| 50 | + let down = &is_border.vertical.at((y, x - 1)); |
| 51 | + solver.add_expr(!(left & right & up & down)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + solver.irrefutable_facts().map(|f| f.get(&is_border)) |
| 56 | +} |
| 57 | + |
| 58 | +type Problem = Vec<Vec<Option<i32>>>; |
| 59 | + |
| 60 | +fn combinator() -> impl Combinator<Problem> { |
| 61 | + Grid::new(Choice::new(vec![ |
| 62 | + Box::new(Spaces::new(None, 'a')), |
| 63 | + Box::new(Dict::new(Some(-1), ".")), |
| 64 | + Box::new(Dict::new(Some(1), "1")), |
| 65 | + Box::new(Dict::new(Some(2), "2")), |
| 66 | + Box::new(Dict::new(Some(3), "3")), |
| 67 | + Box::new(Dict::new(Some(4), "4")), |
| 68 | + ])) |
| 69 | +} |
| 70 | + |
| 71 | +pub fn serialize_problem(problem: &Problem) -> Option<String> { |
| 72 | + problem_to_url(combinator(), "fillmat", problem.clone()) |
| 73 | +} |
| 74 | + |
| 75 | +pub fn deserialize_problem(url: &str) -> Option<Problem> { |
| 76 | + url_to_problem(combinator(), &["fillmat"], url) |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + fn problem_for_tests() -> Problem { |
| 84 | + vec![ |
| 85 | + vec![Some(3), None, None, Some(3), None], |
| 86 | + vec![None, None, None, None, None], |
| 87 | + vec![None, None, Some(1), None, None], |
| 88 | + vec![None, None, None, None, None], |
| 89 | + vec![None, Some(1), None, None, Some(4)], |
| 90 | + ] |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_fillmat_problem() { |
| 95 | + let problem = problem_for_tests(); |
| 96 | + let ans = solve_fillmat(&problem); |
| 97 | + assert!(ans.is_some()); |
| 98 | + let ans = ans.unwrap(); |
| 99 | + #[rustfmt::skip] |
| 100 | + let expected = graph::BoolInnerGridEdgesIrrefutableFacts { |
| 101 | + horizontal: crate::util::tests::to_option_bool_2d([ |
| 102 | + [0, 0, 1, 1, 1], |
| 103 | + [0, 0, 1, 1, 0], |
| 104 | + [1, 0, 1, 0, 0], |
| 105 | + [0, 1, 0, 0, 0], |
| 106 | + ]), |
| 107 | + vertical: crate::util::tests::to_option_bool_2d([ |
| 108 | + [1, 1, 0, 0], |
| 109 | + [1, 1, 0, 1], |
| 110 | + [1, 1, 1, 1], |
| 111 | + [1, 1, 1, 1], |
| 112 | + [1, 1, 1, 1], |
| 113 | + ]), |
| 114 | + }; |
| 115 | + assert_eq!(ans, expected); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_fillmat_serializer() { |
| 120 | + let problem = problem_for_tests(); |
| 121 | + let url = "https://puzz.link/p?fillmat/5/5/3b3h1h1b4"; |
| 122 | + util::tests::serializer_test(problem, url, serialize_problem, deserialize_problem); |
| 123 | + } |
| 124 | +} |
0 commit comments