|
| 1 | +use crate::board::{Board, BoardKind, Item, ItemKind}; |
| 2 | +use crate::uniqueness::check_uniqueness; |
| 3 | +use cspuz_rs_puzzles::puzzles::gokigen::{self, GOKIGEN_SLASH}; |
| 4 | + |
| 5 | +pub fn solve(url: &str) -> Result<Board, &'static str> { |
| 6 | + let problem = gokigen::deserialize_problem(url).ok_or("invalid url")?; |
| 7 | + let ans = gokigen::solve_gokigen(&problem); |
| 8 | + |
| 9 | + let height = problem.len() - 1; |
| 10 | + let width = problem[0].len() - 1; |
| 11 | + let mut board = Board::new(BoardKind::Empty, height, width, check_uniqueness(&ans)); |
| 12 | + if let Some(ans) = ans { |
| 13 | + for y in 0..height { |
| 14 | + for x in 0..width { |
| 15 | + if let Some(a) = ans[y][x] { |
| 16 | + board.push(Item::cell( |
| 17 | + y, |
| 18 | + x, |
| 19 | + "green", |
| 20 | + if a == GOKIGEN_SLASH { |
| 21 | + ItemKind::Slash |
| 22 | + } else { |
| 23 | + ItemKind::Backslash |
| 24 | + }, |
| 25 | + )); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + for y in 0..=height { |
| 31 | + for x in 0..=width { |
| 32 | + if y < height { |
| 33 | + board.push(Item { |
| 34 | + y: y * 2 + 1, |
| 35 | + x: x * 2, |
| 36 | + color: "black", |
| 37 | + kind: ItemKind::DottedWall, |
| 38 | + }); |
| 39 | + } |
| 40 | + if x < width { |
| 41 | + board.push(Item { |
| 42 | + y: y * 2, |
| 43 | + x: x * 2 + 1, |
| 44 | + color: "black", |
| 45 | + kind: ItemKind::DottedWall, |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + for y in 0..=height { |
| 51 | + for x in 0..=width { |
| 52 | + if let Some(n) = problem[y][x] { |
| 53 | + board.push(Item { |
| 54 | + y: y * 2, |
| 55 | + x: x * 2, |
| 56 | + color: "white", |
| 57 | + kind: ItemKind::FilledCircle, |
| 58 | + }); |
| 59 | + board.push(Item { |
| 60 | + y: y * 2, |
| 61 | + x: x * 2, |
| 62 | + color: "black", |
| 63 | + kind: ItemKind::Circle, |
| 64 | + }); |
| 65 | + if n >= 0 { |
| 66 | + board.push(Item { |
| 67 | + y: y * 2, |
| 68 | + x: x * 2, |
| 69 | + color: "black", |
| 70 | + kind: ItemKind::Num(n), |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Ok(board) |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::solve; |
| 83 | + use crate::board::*; |
| 84 | + use crate::compare_board_and_check_no_solution_case; |
| 85 | + use crate::uniqueness::Uniqueness; |
| 86 | + |
| 87 | + #[test] |
| 88 | + #[rustfmt::skip] |
| 89 | + fn test_solve() { |
| 90 | + compare_board_and_check_no_solution_case!( |
| 91 | + solve("https://puzz.link/p?gokigen/4/3/l372666bg"), |
| 92 | + Board { |
| 93 | + kind: BoardKind::Empty, |
| 94 | + height: 3, |
| 95 | + width: 4, |
| 96 | + data: vec![ |
| 97 | + Item { y: 1, x: 1, color: "green", kind: ItemKind::Backslash }, |
| 98 | + Item { y: 1, x: 3, color: "green", kind: ItemKind::Slash }, |
| 99 | + Item { y: 1, x: 5, color: "green", kind: ItemKind::Backslash }, |
| 100 | + Item { y: 1, x: 7, color: "green", kind: ItemKind::Backslash }, |
| 101 | + Item { y: 3, x: 1, color: "green", kind: ItemKind::Slash }, |
| 102 | + Item { y: 3, x: 3, color: "green", kind: ItemKind::Slash }, |
| 103 | + Item { y: 3, x: 5, color: "green", kind: ItemKind::Backslash }, |
| 104 | + Item { y: 3, x: 7, color: "green", kind: ItemKind::Slash }, |
| 105 | + Item { y: 5, x: 1, color: "green", kind: ItemKind::Slash }, |
| 106 | + Item { y: 5, x: 3, color: "green", kind: ItemKind::Slash }, |
| 107 | + Item { y: 5, x: 5, color: "green", kind: ItemKind::Slash }, |
| 108 | + Item { y: 5, x: 7, color: "green", kind: ItemKind::Slash }, |
| 109 | + Item { y: 1, x: 0, color: "black", kind: ItemKind::DottedWall }, |
| 110 | + Item { y: 0, x: 1, color: "black", kind: ItemKind::DottedWall }, |
| 111 | + Item { y: 1, x: 2, color: "black", kind: ItemKind::DottedWall }, |
| 112 | + Item { y: 0, x: 3, color: "black", kind: ItemKind::DottedWall }, |
| 113 | + Item { y: 1, x: 4, color: "black", kind: ItemKind::DottedWall }, |
| 114 | + Item { y: 0, x: 5, color: "black", kind: ItemKind::DottedWall }, |
| 115 | + Item { y: 1, x: 6, color: "black", kind: ItemKind::DottedWall }, |
| 116 | + Item { y: 0, x: 7, color: "black", kind: ItemKind::DottedWall }, |
| 117 | + Item { y: 1, x: 8, color: "black", kind: ItemKind::DottedWall }, |
| 118 | + Item { y: 3, x: 0, color: "black", kind: ItemKind::DottedWall }, |
| 119 | + Item { y: 2, x: 1, color: "black", kind: ItemKind::DottedWall }, |
| 120 | + Item { y: 3, x: 2, color: "black", kind: ItemKind::DottedWall }, |
| 121 | + Item { y: 2, x: 3, color: "black", kind: ItemKind::DottedWall }, |
| 122 | + Item { y: 3, x: 4, color: "black", kind: ItemKind::DottedWall }, |
| 123 | + Item { y: 2, x: 5, color: "black", kind: ItemKind::DottedWall }, |
| 124 | + Item { y: 3, x: 6, color: "black", kind: ItemKind::DottedWall }, |
| 125 | + Item { y: 2, x: 7, color: "black", kind: ItemKind::DottedWall }, |
| 126 | + Item { y: 3, x: 8, color: "black", kind: ItemKind::DottedWall }, |
| 127 | + Item { y: 5, x: 0, color: "black", kind: ItemKind::DottedWall }, |
| 128 | + Item { y: 4, x: 1, color: "black", kind: ItemKind::DottedWall }, |
| 129 | + Item { y: 5, x: 2, color: "black", kind: ItemKind::DottedWall }, |
| 130 | + Item { y: 4, x: 3, color: "black", kind: ItemKind::DottedWall }, |
| 131 | + Item { y: 5, x: 4, color: "black", kind: ItemKind::DottedWall }, |
| 132 | + Item { y: 4, x: 5, color: "black", kind: ItemKind::DottedWall }, |
| 133 | + Item { y: 5, x: 6, color: "black", kind: ItemKind::DottedWall }, |
| 134 | + Item { y: 4, x: 7, color: "black", kind: ItemKind::DottedWall }, |
| 135 | + Item { y: 5, x: 8, color: "black", kind: ItemKind::DottedWall }, |
| 136 | + Item { y: 6, x: 1, color: "black", kind: ItemKind::DottedWall }, |
| 137 | + Item { y: 6, x: 3, color: "black", kind: ItemKind::DottedWall }, |
| 138 | + Item { y: 6, x: 5, color: "black", kind: ItemKind::DottedWall }, |
| 139 | + Item { y: 6, x: 7, color: "black", kind: ItemKind::DottedWall }, |
| 140 | + Item { y: 2, x: 2, color: "white", kind: ItemKind::FilledCircle }, |
| 141 | + Item { y: 2, x: 2, color: "black", kind: ItemKind::Circle }, |
| 142 | + Item { y: 2, x: 2, color: "black", kind: ItemKind::Num(3) }, |
| 143 | + Item { y: 2, x: 4, color: "white", kind: ItemKind::FilledCircle }, |
| 144 | + Item { y: 2, x: 4, color: "black", kind: ItemKind::Circle }, |
| 145 | + Item { y: 2, x: 4, color: "black", kind: ItemKind::Num(2) }, |
| 146 | + Item { y: 2, x: 8, color: "white", kind: ItemKind::FilledCircle }, |
| 147 | + Item { y: 2, x: 8, color: "black", kind: ItemKind::Circle }, |
| 148 | + Item { y: 2, x: 8, color: "black", kind: ItemKind::Num(2) }, |
| 149 | + Item { y: 4, x: 0, color: "white", kind: ItemKind::FilledCircle }, |
| 150 | + Item { y: 4, x: 0, color: "black", kind: ItemKind::Circle }, |
| 151 | + Item { y: 4, x: 0, color: "black", kind: ItemKind::Num(1) }, |
| 152 | + Item { y: 4, x: 4, color: "white", kind: ItemKind::FilledCircle }, |
| 153 | + Item { y: 4, x: 4, color: "black", kind: ItemKind::Circle }, |
| 154 | + Item { y: 4, x: 4, color: "black", kind: ItemKind::Num(1) }, |
| 155 | + Item { y: 4, x: 8, color: "white", kind: ItemKind::FilledCircle }, |
| 156 | + Item { y: 4, x: 8, color: "black", kind: ItemKind::Circle }, |
| 157 | + Item { y: 4, x: 8, color: "black", kind: ItemKind::Num(1) }, |
| 158 | + Item { y: 6, x: 2, color: "white", kind: ItemKind::FilledCircle }, |
| 159 | + Item { y: 6, x: 2, color: "black", kind: ItemKind::Circle }, |
| 160 | + Item { y: 6, x: 2, color: "black", kind: ItemKind::Num(1) }, |
| 161 | + ], |
| 162 | + uniqueness: Uniqueness::Unique, |
| 163 | + }, |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments