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