|
| 1 | +use crate::board::{Board, BoardKind, Item, ItemKind}; |
| 2 | +use crate::uniqueness::check_uniqueness; |
| 3 | +use cspuz_rs_puzzles::puzzles::canalview; |
| 4 | + |
| 5 | +pub fn solve(url: &str) -> Result<Board, &'static str> { |
| 6 | + let problem = canalview::deserialize_problem(url).ok_or("invalid url")?; |
| 7 | + let ans = canalview::solve_canalview(&problem); |
| 8 | + |
| 9 | + let height = problem.len(); |
| 10 | + let width = problem[0].len(); |
| 11 | + let mut board = Board::new(BoardKind::Grid, height, width, check_uniqueness(&ans)); |
| 12 | + for y in 0..height { |
| 13 | + for x in 0..width { |
| 14 | + if let Some(clue) = problem[y][x] { |
| 15 | + if clue > 0 { |
| 16 | + board.push(Item::cell(y, x, "black", ItemKind::Num(clue))); |
| 17 | + } else { |
| 18 | + board.push(Item::cell(y, x, "black", ItemKind::Text("?"))); |
| 19 | + } |
| 20 | + } else if let Some(ans) = &ans { |
| 21 | + if let Some(a) = ans[y][x] { |
| 22 | + board.push(Item::cell( |
| 23 | + y, |
| 24 | + x, |
| 25 | + "green", |
| 26 | + if a { ItemKind::Block } else { ItemKind::Dot }, |
| 27 | + )); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + Ok(board) |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use super::solve; |
| 39 | + use crate::board::*; |
| 40 | + use crate::compare_board_and_check_no_solution_case; |
| 41 | + use crate::uniqueness::Uniqueness; |
| 42 | + |
| 43 | + #[test] |
| 44 | + #[rustfmt::skip] |
| 45 | + fn test_solve() { |
| 46 | + compare_board_and_check_no_solution_case!( |
| 47 | + solve("https://puzz.link/p?canal/6/6/3i3h2z4h5i2"), |
| 48 | + Board { |
| 49 | + kind: BoardKind::Grid, |
| 50 | + height: 6, |
| 51 | + width: 6, |
| 52 | + data: vec![ |
| 53 | + Item { y: 1, x: 1, color: "black", kind: ItemKind::Num(3) }, |
| 54 | + Item { y: 1, x: 3, color: "green", kind: ItemKind::Block }, |
| 55 | + Item { y: 1, x: 5, color: "green", kind: ItemKind::Block }, |
| 56 | + Item { y: 1, x: 7, color: "green", kind: ItemKind::Block }, |
| 57 | + Item { y: 1, x: 9, color: "black", kind: ItemKind::Num(3) }, |
| 58 | + Item { y: 1, x: 11, color: "green", kind: ItemKind::Dot }, |
| 59 | + Item { y: 3, x: 1, color: "green", kind: ItemKind::Dot }, |
| 60 | + Item { y: 3, x: 3, color: "black", kind: ItemKind::Num(2) }, |
| 61 | + Item { y: 3, x: 5, color: "green", kind: ItemKind::Block }, |
| 62 | + Item { y: 3, x: 7, color: "green", kind: ItemKind::Dot }, |
| 63 | + Item { y: 3, x: 9, color: "green", kind: ItemKind::Dot }, |
| 64 | + Item { y: 3, x: 11, color: "green", kind: ItemKind::Dot }, |
| 65 | + Item { y: 5, x: 1, color: "green", kind: ItemKind::Dot }, |
| 66 | + Item { y: 5, x: 3, color: "green", kind: ItemKind::Dot }, |
| 67 | + Item { y: 5, x: 5, color: "green", kind: ItemKind::Block }, |
| 68 | + Item { y: 5, x: 7, color: "green", kind: ItemKind::Dot }, |
| 69 | + Item { y: 5, x: 9, color: "green", kind: ItemKind::Block }, |
| 70 | + Item { y: 5, x: 11, color: "green", kind: ItemKind::Dot }, |
| 71 | + Item { y: 7, x: 1, color: "green", kind: ItemKind::Dot }, |
| 72 | + Item { y: 7, x: 3, color: "green", kind: ItemKind::Block }, |
| 73 | + Item { y: 7, x: 5, color: "green", kind: ItemKind::Block }, |
| 74 | + Item { y: 7, x: 7, color: "green", kind: ItemKind::Block }, |
| 75 | + Item { y: 7, x: 9, color: "green", kind: ItemKind::Block }, |
| 76 | + Item { y: 7, x: 11, color: "green", kind: ItemKind::Block }, |
| 77 | + Item { y: 9, x: 1, color: "green", kind: ItemKind::Block }, |
| 78 | + Item { y: 9, x: 3, color: "green", kind: ItemKind::Block }, |
| 79 | + Item { y: 9, x: 5, color: "green", kind: ItemKind::Dot }, |
| 80 | + Item { y: 9, x: 7, color: "green", kind: ItemKind::Block }, |
| 81 | + Item { y: 9, x: 9, color: "black", kind: ItemKind::Num(4) }, |
| 82 | + Item { y: 9, x: 11, color: "green", kind: ItemKind::Block }, |
| 83 | + Item { y: 11, x: 1, color: "green", kind: ItemKind::Block }, |
| 84 | + Item { y: 11, x: 3, color: "black", kind: ItemKind::Num(5) }, |
| 85 | + Item { y: 11, x: 5, color: "green", kind: ItemKind::Block }, |
| 86 | + Item { y: 11, x: 7, color: "green", kind: ItemKind::Block }, |
| 87 | + Item { y: 11, x: 9, color: "green", kind: ItemKind::Dot }, |
| 88 | + Item { y: 11, x: 11, color: "black", kind: ItemKind::Num(2) }, |
| 89 | + ], |
| 90 | + uniqueness: Uniqueness::Unique, |
| 91 | + }, |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments