|
| 1 | +use crate::board::{Board, BoardKind}; |
| 2 | +use crate::uniqueness::check_uniqueness; |
| 3 | +use cspuz_rs_puzzles::puzzles::doubleback; |
| 4 | + |
| 5 | +pub fn solve(url: &str) -> Result<Board, &'static str> { |
| 6 | + let borders = doubleback::deserialize_problem(url).ok_or("invalid url")?; |
| 7 | + let ans = doubleback::solve_doubleback(&borders); |
| 8 | + |
| 9 | + let height = borders.vertical.len(); |
| 10 | + let width = borders.vertical[0].len() + 1; |
| 11 | + let mut board = Board::new(BoardKind::Grid, height, width, check_uniqueness(&ans)); |
| 12 | + board.add_borders(&borders, "black"); |
| 13 | + |
| 14 | + if let Some(is_line) = &ans { |
| 15 | + board.add_lines_irrefutable_facts(is_line, "green", None); |
| 16 | + } |
| 17 | + |
| 18 | + Ok(board) |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(test)] |
| 22 | +mod tests { |
| 23 | + use super::solve; |
| 24 | + use crate::board::*; |
| 25 | + use crate::compare_board_and_check_no_solution_case; |
| 26 | + use crate::uniqueness::Uniqueness; |
| 27 | + |
| 28 | + #[test] |
| 29 | + #[rustfmt::skip] |
| 30 | + fn test_solve() { |
| 31 | + compare_board_and_check_no_solution_case!( |
| 32 | + solve("https://puzz.link/p?doubleback/4/4/14063o"), |
| 33 | + Board { |
| 34 | + kind: BoardKind::Grid, |
| 35 | + height: 4, |
| 36 | + width: 4, |
| 37 | + data: vec![ |
| 38 | + Item { y: 2, x: 5, color: "black", kind: ItemKind::BoldWall }, |
| 39 | + Item { y: 2, x: 7, color: "black", kind: ItemKind::BoldWall }, |
| 40 | + Item { y: 3, x: 4, color: "black", kind: ItemKind::BoldWall }, |
| 41 | + Item { y: 6, x: 1, color: "black", kind: ItemKind::BoldWall }, |
| 42 | + Item { y: 6, x: 3, color: "black", kind: ItemKind::BoldWall }, |
| 43 | + Item { y: 5, x: 4, color: "black", kind: ItemKind::BoldWall }, |
| 44 | + Item { y: 6, x: 5, color: "black", kind: ItemKind::BoldWall }, |
| 45 | + Item { y: 6, x: 7, color: "black", kind: ItemKind::BoldWall }, |
| 46 | + Item { y: 2, x: 1, color: "green", kind: ItemKind::Line }, |
| 47 | + Item { y: 2, x: 3, color: "green", kind: ItemKind::Cross }, |
| 48 | + Item { y: 2, x: 5, color: "green", kind: ItemKind::Cross }, |
| 49 | + Item { y: 2, x: 7, color: "green", kind: ItemKind::Line }, |
| 50 | + Item { y: 4, x: 1, color: "green", kind: ItemKind::Line }, |
| 51 | + Item { y: 4, x: 3, color: "green", kind: ItemKind::Line }, |
| 52 | + Item { y: 4, x: 5, color: "green", kind: ItemKind::Line }, |
| 53 | + Item { y: 4, x: 7, color: "green", kind: ItemKind::Line }, |
| 54 | + Item { y: 6, x: 1, color: "green", kind: ItemKind::Line }, |
| 55 | + Item { y: 6, x: 3, color: "green", kind: ItemKind::Line }, |
| 56 | + Item { y: 6, x: 5, color: "green", kind: ItemKind::Line }, |
| 57 | + Item { y: 6, x: 7, color: "green", kind: ItemKind::Line }, |
| 58 | + Item { y: 1, x: 2, color: "green", kind: ItemKind::Line }, |
| 59 | + Item { y: 1, x: 4, color: "green", kind: ItemKind::Line }, |
| 60 | + Item { y: 1, x: 6, color: "green", kind: ItemKind::Line }, |
| 61 | + Item { y: 3, x: 2, color: "green", kind: ItemKind::Cross }, |
| 62 | + Item { y: 3, x: 4, color: "green", kind: ItemKind::Line }, |
| 63 | + Item { y: 3, x: 6, color: "green", kind: ItemKind::Cross }, |
| 64 | + Item { y: 5, x: 2, color: "green", kind: ItemKind::Cross }, |
| 65 | + Item { y: 5, x: 4, color: "green", kind: ItemKind::Cross }, |
| 66 | + Item { y: 5, x: 6, color: "green", kind: ItemKind::Cross }, |
| 67 | + Item { y: 7, x: 2, color: "green", kind: ItemKind::Line }, |
| 68 | + Item { y: 7, x: 4, color: "green", kind: ItemKind::Cross }, |
| 69 | + Item { y: 7, x: 6, color: "green", kind: ItemKind::Line }, |
| 70 | + ], |
| 71 | + uniqueness: Uniqueness::Unique, |
| 72 | + }, |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments