-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcrosswall.rs
More file actions
129 lines (122 loc) · 7.12 KB
/
Copy pathcrosswall.rs
File metadata and controls
129 lines (122 loc) · 7.12 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::board::{Board, BoardKind, Item, ItemKind};
use crate::uniqueness::{is_unique, Uniqueness};
use cspuz_rs_puzzles::puzzles::crosswall;
pub fn solve(url: &str) -> Result<Board, &'static str> {
let problem = crosswall::deserialize_problem(url).ok_or("invalid url")?;
let ans = crosswall::solve_crosswall(&problem);
let height = problem.len();
let width = problem[0].len();
let mut board = Board::new(
BoardKind::DotGrid,
height,
width,
ans.as_ref().map_or(Uniqueness::NoAnswer, |a| is_unique(a)),
);
for y in 0..height {
for x in 0..width {
if let Some((size, level)) = problem[y][x] {
if size > 0 {
board.push(Item::cell(y, x, "black", ItemKind::Num(size)));
}
if level >= 0 {
board.push(Item::cell(y, x, "black", ItemKind::NumLowerRight(level)));
}
}
}
}
if let Some(is_line) = &ans {
board.add_grid_edges(is_line, "green", ItemKind::Wall, ItemKind::Cross);
}
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://pedros.works/paper-puzzle-player?W=5x5&L-N=(2)6(3)1(4)2(4)1(1)1(6)11&L-S=(0)9(2)3(2)6&G=crosswall"),
Board {
kind: BoardKind::DotGrid,
height: 5,
width: 5,
data: vec![
Item { y: 1, x: 3, color: "black", kind: ItemKind::Num(4) },
Item { y: 1, x: 3, color: "black", kind: ItemKind::NumLowerRight(0) },
Item { y: 3, x: 7, color: "black", kind: ItemKind::NumLowerRight(2) },
Item { y: 5, x: 3, color: "black", kind: ItemKind::Num(3) },
Item { y: 5, x: 5, color: "black", kind: ItemKind::NumLowerRight(2) },
Item { y: 5, x: 9, color: "black", kind: ItemKind::Num(6) },
Item { y: 7, x: 3, color: "black", kind: ItemKind::Num(2) },
Item { y: 7, x: 5, color: "black", kind: ItemKind::Num(1) },
Item { y: 9, x: 5, color: "black", kind: ItemKind::Num(4) },
Item { y: 1, x: 0, color: "green", kind: ItemKind::Cross },
Item { y: 1, x: 2, color: "green", kind: ItemKind::Cross },
Item { y: 1, x: 4, color: "green", kind: ItemKind::Cross },
Item { y: 1, x: 6, color: "green", kind: ItemKind::Wall },
Item { y: 1, x: 8, color: "green", kind: ItemKind::Cross },
Item { y: 1, x: 10, color: "green", kind: ItemKind::Wall },
Item { y: 3, x: 0, color: "green", kind: ItemKind::Wall },
Item { y: 3, x: 2, color: "green", kind: ItemKind::Wall },
Item { y: 3, x: 4, color: "green", kind: ItemKind::Wall },
Item { y: 3, x: 6, color: "green", kind: ItemKind::Wall },
Item { y: 3, x: 8, color: "green", kind: ItemKind::Wall },
Item { y: 3, x: 10, color: "green", kind: ItemKind::Wall },
Item { y: 5, x: 0, color: "green", kind: ItemKind::Wall },
Item { y: 5, x: 2, color: "green", kind: ItemKind::Cross },
Item { y: 5, x: 4, color: "green", kind: ItemKind::Wall },
Item { y: 5, x: 6, color: "green", kind: ItemKind::Wall },
Item { y: 5, x: 8, color: "green", kind: ItemKind::Cross },
Item { y: 5, x: 10, color: "green", kind: ItemKind::Wall },
Item { y: 7, x: 0, color: "green", kind: ItemKind::Cross },
Item { y: 7, x: 2, color: "green", kind: ItemKind::Cross },
Item { y: 7, x: 4, color: "green", kind: ItemKind::Wall },
Item { y: 7, x: 6, color: "green", kind: ItemKind::Wall },
Item { y: 7, x: 8, color: "green", kind: ItemKind::Wall },
Item { y: 7, x: 10, color: "green", kind: ItemKind::Wall },
Item { y: 9, x: 0, color: "green", kind: ItemKind::Wall },
Item { y: 9, x: 2, color: "green", kind: ItemKind::Cross },
Item { y: 9, x: 4, color: "green", kind: ItemKind::Wall },
Item { y: 9, x: 6, color: "green", kind: ItemKind::Cross },
Item { y: 9, x: 8, color: "green", kind: ItemKind::Cross },
Item { y: 9, x: 10, color: "green", kind: ItemKind::Cross },
Item { y: 0, x: 1, color: "green", kind: ItemKind::Cross },
Item { y: 0, x: 3, color: "green", kind: ItemKind::Cross },
Item { y: 0, x: 5, color: "green", kind: ItemKind::Cross },
Item { y: 0, x: 7, color: "green", kind: ItemKind::Wall },
Item { y: 0, x: 9, color: "green", kind: ItemKind::Wall },
Item { y: 2, x: 1, color: "green", kind: ItemKind::Wall },
Item { y: 2, x: 3, color: "green", kind: ItemKind::Cross },
Item { y: 2, x: 5, color: "green", kind: ItemKind::Wall },
Item { y: 2, x: 7, color: "green", kind: ItemKind::Wall },
Item { y: 2, x: 9, color: "green", kind: ItemKind::Cross },
Item { y: 4, x: 1, color: "green", kind: ItemKind::Cross },
Item { y: 4, x: 3, color: "green", kind: ItemKind::Wall },
Item { y: 4, x: 5, color: "green", kind: ItemKind::Wall },
Item { y: 4, x: 7, color: "green", kind: ItemKind::Wall },
Item { y: 4, x: 9, color: "green", kind: ItemKind::Cross },
Item { y: 6, x: 1, color: "green", kind: ItemKind::Wall },
Item { y: 6, x: 3, color: "green", kind: ItemKind::Wall },
Item { y: 6, x: 5, color: "green", kind: ItemKind::Wall },
Item { y: 6, x: 7, color: "green", kind: ItemKind::Wall },
Item { y: 6, x: 9, color: "green", kind: ItemKind::Cross },
Item { y: 8, x: 1, color: "green", kind: ItemKind::Wall },
Item { y: 8, x: 3, color: "green", kind: ItemKind::Wall },
Item { y: 8, x: 5, color: "green", kind: ItemKind::Wall },
Item { y: 8, x: 7, color: "green", kind: ItemKind::Cross },
Item { y: 8, x: 9, color: "green", kind: ItemKind::Wall },
Item { y: 10, x: 1, color: "green", kind: ItemKind::Wall },
Item { y: 10, x: 3, color: "green", kind: ItemKind::Wall },
Item { y: 10, x: 5, color: "green", kind: ItemKind::Cross },
Item { y: 10, x: 7, color: "green", kind: ItemKind::Cross },
Item { y: 10, x: 9, color: "green", kind: ItemKind::Cross },
],
uniqueness: Uniqueness::Unique,
},
);
}
}