-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathripple.rs
More file actions
98 lines (91 loc) · 4.74 KB
/
Copy pathripple.rs
File metadata and controls
98 lines (91 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::board::{Board, BoardKind, Item, ItemKind};
use crate::uniqueness::check_uniqueness;
use cspuz_rs_puzzles::puzzles::ripple;
pub fn solve(url: &str) -> Result<Board, &'static str> {
let (borders, clues, is_hole) = ripple::deserialize_problem(url).ok_or("invalid url")?;
let ans = ripple::solve_ripple(&borders, &clues, &is_hole);
let height = clues.len();
let width = clues[0].len();
let mut board = Board::new(BoardKind::Grid, height, width, check_uniqueness(&ans));
board.add_borders(&borders, "black");
for y in 0..height {
for x in 0..width {
if let Some(is_hole) = &is_hole {
if is_hole[y][x] {
board.push(Item::cell(y, x, "black", ItemKind::Fill));
continue;
}
}
if let Some(n) = clues[y][x] {
if n >= 0 {
board.push(Item::cell(y, x, "black", ItemKind::Num(n)));
} else {
board.push(Item::cell(y, x, "black", ItemKind::Text("?")));
}
} else if let Some(ans) = &ans {
if let Some(n) = ans[y][x] {
board.push(Item::cell(y, x, "green", ItemKind::Num(n)));
}
}
}
}
Ok(board)
}
#[cfg(test)]
mod tests {
use super::solve;
use crate::board::*;
use crate::compare_board_and_check_no_solution_case;
use crate::uniqueness::Uniqueness;
#[test]
#[rustfmt::skip]
fn test_solve() {
compare_board_and_check_no_solution_case!(
solve("https://puzz.link/p?ripple/5/4/ld8g2sug4g3u1"),
Board {
kind: BoardKind::Grid,
height: 4,
width: 5,
data: vec![
Item { y: 1, x: 2, color: "black", kind: ItemKind::BoldWall },
Item { y: 1, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 2, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 4, x: 1, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 2, color: "black", kind: ItemKind::BoldWall },
Item { y: 4, x: 3, color: "black", kind: ItemKind::BoldWall },
Item { y: 4, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 6, color: "black", kind: ItemKind::BoldWall },
Item { y: 3, x: 8, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 1, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 3, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 4, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 5, color: "black", kind: ItemKind::BoldWall },
Item { y: 6, x: 7, color: "black", kind: ItemKind::BoldWall },
Item { y: 5, x: 8, color: "black", kind: ItemKind::BoldWall },
Item { y: 7, x: 8, color: "black", kind: ItemKind::BoldWall },
Item { y: 1, x: 1, color: "green", kind: ItemKind::Num(1) },
Item { y: 1, x: 3, color: "black", kind: ItemKind::Num(4) },
Item { y: 1, x: 5, color: "green", kind: ItemKind::Num(2) },
Item { y: 1, x: 7, color: "black", kind: ItemKind::Num(3) },
Item { y: 1, x: 9, color: "green", kind: ItemKind::Num(5) },
Item { y: 3, x: 1, color: "green", kind: ItemKind::Num(2) },
Item { y: 3, x: 3, color: "green", kind: ItemKind::Num(3) },
Item { y: 3, x: 5, color: "green", kind: ItemKind::Num(1) },
Item { y: 3, x: 7, color: "green", kind: ItemKind::Num(2) },
Item { y: 3, x: 9, color: "green", kind: ItemKind::Num(4) },
Item { y: 5, x: 1, color: "green", kind: ItemKind::Num(1) },
Item { y: 5, x: 3, color: "green", kind: ItemKind::Num(2) },
Item { y: 5, x: 5, color: "green", kind: ItemKind::Num(3) },
Item { y: 5, x: 7, color: "green", kind: ItemKind::Num(1) },
Item { y: 5, x: 9, color: "green", kind: ItemKind::Num(2) },
Item { y: 7, x: 1, color: "green", kind: ItemKind::Num(3) },
Item { y: 7, x: 3, color: "green", kind: ItemKind::Num(1) },
Item { y: 7, x: 5, color: "green", kind: ItemKind::Num(2) },
Item { y: 7, x: 7, color: "green", kind: ItemKind::Num(4) },
Item { y: 7, x: 9, color: "black", kind: ItemKind::Num(1) },
],
uniqueness: Uniqueness::Unique,
},
);
}
}