|
| 1 | +use crate::board::{Board, BoardKind, Item, ItemKind}; |
| 2 | +use crate::uniqueness::check_uniqueness; |
| 3 | +use cspuz_rs_puzzles::puzzles::road_planning; |
| 4 | + |
| 5 | +pub fn solve(url: &str) -> Result<Board, &'static str> { |
| 6 | + let problem = road_planning::deserialize_problem(url).ok_or("invalid url")?; |
| 7 | + let ans = road_planning::solve_road_planning(&problem); |
| 8 | + |
| 9 | + let height = problem.len() - 1; |
| 10 | + let width = problem[0].len() - 1; |
| 11 | + let mut board = Board::new( |
| 12 | + BoardKind::ColoredGrid("#cccccc"), |
| 13 | + height, |
| 14 | + width, |
| 15 | + check_uniqueness(&ans), |
| 16 | + ); |
| 17 | + |
| 18 | + board.add_borders_as_answer(ans.as_ref()); |
| 19 | + |
| 20 | + for y in 0..=height { |
| 21 | + for x in 0..=width { |
| 22 | + if problem[y][x] { |
| 23 | + board.push(Item { |
| 24 | + y: y * 2, |
| 25 | + x: x * 2, |
| 26 | + color: "white", |
| 27 | + kind: ItemKind::SmallFilledCircle, |
| 28 | + }); |
| 29 | + board.push(Item { |
| 30 | + y: y * 2, |
| 31 | + x: x * 2, |
| 32 | + color: "black", |
| 33 | + kind: ItemKind::SmallCircle, |
| 34 | + }); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + Ok(board) |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::solve; |
| 45 | + use crate::board::*; |
| 46 | + use crate::compare_board_and_check_no_solution_case; |
| 47 | + use crate::uniqueness::Uniqueness; |
| 48 | + |
| 49 | + #[test] |
| 50 | + #[rustfmt::skip] |
| 51 | + fn test_solve() { |
| 52 | + compare_board_and_check_no_solution_case!( |
| 53 | + solve("https://opt-pan.github.io/penpa-edit/#m=solve&p=tVRRb9owEH7Pr5j8fA+xgRb81nXrXjq2DqYKWREKkJaoAXdOslZG9Lf3fIkGJuZl2hT505fPF/vsuy/lrzo1GQygD70hxMDxEWIIfIT8ckAjbp9pXhWZ/ABXdbXWBgnAtzE8pEWZRaoNSiLFBAManCVvdvKmGAOeRDv7Q+7sXKpkD/bngQ4PdCJ3iGNCTjiTOzbsMak4sGVulkU2n0wYiATYsB+UeSzO6OFleHxuncEZfRTWBQ/oeIQbOoggnOI5wfYIPxHGhAPCW4r5THhPeE3YJ7ygmEt3U1GkRFMq92CRzjOsB149K3UxL2vzkC4zJqliQNq23iwy40mF1s9FvvXj8setNllwyonZ6jEUv9BmdbL6S1oUntA0oCc1N+hJlcm999QY/eIpm7Rae8IirbBdy3X+7K+UbSs/gSr1U0yf0pPdNocz7yP2ymgoAeICBHX2SNo7sF+k1/tg77C1v0o7c53duMAVWeGkaxms9B96T/OOXTcij5GPW450htTvOPtdKjsF5jb6SJ87yjb6N+ZK39H7Um8WeBrFju6jmSnrlX6q21juWvWqyXcSyLd3yNfRJl/HAvm65I7yvW0W+qfpjpJ9U4n4r/8r/8mar63btAkaDuWA51ANeqvVO/ZCvWMkt2HXS6gG7ITqqaNQ6poKxY6vUDtjLbfqqbtcVqcGc1t1POa2OrYZ/raIvQM="), |
| 54 | + Board { |
| 55 | + kind: BoardKind::ColoredGrid("#cccccc"), |
| 56 | + height: 4, |
| 57 | + width: 5, |
| 58 | + data: vec![ |
| 59 | + Item { y: 2, x: 1, color: "green", kind: ItemKind::Cross }, |
| 60 | + Item { y: 1, x: 2, color: "green", kind: ItemKind::BoldWall }, |
| 61 | + Item { y: 2, x: 3, color: "green", kind: ItemKind::Cross }, |
| 62 | + Item { y: 1, x: 4, color: "green", kind: ItemKind::BoldWall }, |
| 63 | + Item { y: 2, x: 5, color: "green", kind: ItemKind::BoldWall }, |
| 64 | + Item { y: 1, x: 6, color: "green", kind: ItemKind::Cross }, |
| 65 | + Item { y: 2, x: 7, color: "green", kind: ItemKind::BoldWall }, |
| 66 | + Item { y: 1, x: 8, color: "green", kind: ItemKind::Cross }, |
| 67 | + Item { y: 2, x: 9, color: "green", kind: ItemKind::Cross }, |
| 68 | + Item { y: 4, x: 1, color: "green", kind: ItemKind::Cross }, |
| 69 | + Item { y: 3, x: 2, color: "green", kind: ItemKind::BoldWall }, |
| 70 | + Item { y: 4, x: 3, color: "green", kind: ItemKind::BoldWall }, |
| 71 | + Item { y: 3, x: 4, color: "green", kind: ItemKind::Cross }, |
| 72 | + Item { y: 4, x: 5, color: "green", kind: ItemKind::BoldWall }, |
| 73 | + Item { y: 3, x: 6, color: "green", kind: ItemKind::Cross }, |
| 74 | + Item { y: 4, x: 7, color: "green", kind: ItemKind::BoldWall }, |
| 75 | + Item { y: 3, x: 8, color: "green", kind: ItemKind::BoldWall }, |
| 76 | + Item { y: 4, x: 9, color: "green", kind: ItemKind::BoldWall }, |
| 77 | + Item { y: 6, x: 1, color: "green", kind: ItemKind::BoldWall }, |
| 78 | + Item { y: 5, x: 2, color: "green", kind: ItemKind::Cross }, |
| 79 | + Item { y: 6, x: 3, color: "green", kind: ItemKind::BoldWall }, |
| 80 | + Item { y: 5, x: 4, color: "green", kind: ItemKind::BoldWall }, |
| 81 | + Item { y: 6, x: 5, color: "green", kind: ItemKind::Cross }, |
| 82 | + Item { y: 5, x: 6, color: "green", kind: ItemKind::BoldWall }, |
| 83 | + Item { y: 6, x: 7, color: "green", kind: ItemKind::Cross }, |
| 84 | + Item { y: 5, x: 8, color: "green", kind: ItemKind::Cross }, |
| 85 | + Item { y: 6, x: 9, color: "green", kind: ItemKind::Cross }, |
| 86 | + Item { y: 7, x: 2, color: "green", kind: ItemKind::Cross }, |
| 87 | + Item { y: 7, x: 4, color: "green", kind: ItemKind::Cross }, |
| 88 | + Item { y: 7, x: 6, color: "green", kind: ItemKind::BoldWall }, |
| 89 | + Item { y: 7, x: 8, color: "green", kind: ItemKind::Cross }, |
| 90 | + Item { y: 0, x: 2, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 91 | + Item { y: 0, x: 2, color: "black", kind: ItemKind::SmallCircle }, |
| 92 | + Item { y: 0, x: 4, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 93 | + Item { y: 0, x: 4, color: "black", kind: ItemKind::SmallCircle }, |
| 94 | + Item { y: 4, x: 4, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 95 | + Item { y: 4, x: 4, color: "black", kind: ItemKind::SmallCircle }, |
| 96 | + Item { y: 4, x: 6, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 97 | + Item { y: 4, x: 6, color: "black", kind: ItemKind::SmallCircle }, |
| 98 | + Item { y: 4, x: 8, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 99 | + Item { y: 4, x: 8, color: "black", kind: ItemKind::SmallCircle }, |
| 100 | + Item { y: 4, x: 10, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 101 | + Item { y: 4, x: 10, color: "black", kind: ItemKind::SmallCircle }, |
| 102 | + Item { y: 6, x: 0, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 103 | + Item { y: 6, x: 0, color: "black", kind: ItemKind::SmallCircle }, |
| 104 | + Item { y: 8, x: 6, color: "white", kind: ItemKind::SmallFilledCircle }, |
| 105 | + Item { y: 8, x: 6, color: "black", kind: ItemKind::SmallCircle }, |
| 106 | + ], |
| 107 | + uniqueness: Uniqueness::Unique, |
| 108 | + }, |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments