forked from semiexp/cspuz_core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaquarium.rs
More file actions
226 lines (206 loc) · 6.84 KB
/
Copy pathaquarium.rs
File metadata and controls
226 lines (206 loc) · 6.84 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use cspuz_rs::graph;
use cspuz_rs::serializer::{
problem_to_url_with_context, url_to_problem, Choice, Combinator, Context, Dict, HexInt,
Optionalize, OutsideCells2, PrefixAndSuffix, Rooms, Size, Spaces, Tuple2,
};
use cspuz_rs::solver::Solver;
pub fn solve_aquarium(
region_aware: bool,
borders: &graph::InnerGridEdges<Vec<Vec<bool>>>,
clues_up: &[Option<i32>],
clues_left: &[Option<i32>],
) -> Option<Vec<Vec<Option<bool>>>> {
let (h, w) = borders.base_shape();
let mut solver = Solver::new();
let is_black = &solver.bool_var_2d((h, w));
solver.add_answer_key_bool(is_black);
for y in 0..h {
if let Some(n) = clues_left[y] {
let row = is_black.slice_fixed_y((y, ..));
solver.add_expr(row.count_true().eq(n));
}
}
for x in 0..w {
if let Some(n) = clues_up[x] {
let col = is_black.slice_fixed_x((.., x));
solver.add_expr(col.count_true().eq(n));
}
}
if region_aware {
let regions = graph::borders_to_rooms(borders);
for mut region in regions {
region.sort();
for i in 1..region.len() {
if region[i - 1].0 == region[i].0 {
solver.add_expr(is_black.at(region[i - 1]).iff(is_black.at(region[i])));
} else {
solver.add_expr(is_black.at(region[i - 1]).imp(is_black.at(region[i])));
}
}
}
} else {
for y in 0..h {
for x in 0..w {
let mut visited = vec![vec![false; w]; h];
let mut stack = vec![(y, x)];
while let Some((cy, cx)) = stack.pop() {
if visited[cy][cx] {
continue;
}
visited[cy][cx] = true;
if cx > 0 && !borders.vertical[cy][cx - 1] {
stack.push((cy, cx - 1));
}
if cx + 1 < w && !borders.vertical[cy][cx] {
stack.push((cy, cx + 1));
}
if cy < h - 1 && !borders.horizontal[cy][cx] {
stack.push((cy + 1, cx));
}
if cy > y && !borders.horizontal[cy - 1][cx] {
stack.push((cy - 1, cx));
}
}
for y2 in 0..h {
for x2 in 0..w {
if visited[y2][x2] {
solver.add_expr(is_black.at((y, x)).imp(is_black.at((y2, x2))));
}
}
}
}
}
}
solver.irrefutable_facts().map(|f| f.get(is_black))
}
type Problem = (
bool,
(
graph::InnerGridEdges<Vec<Vec<bool>>>,
(Vec<Option<i32>>, Vec<Option<i32>>),
),
);
fn combinator() -> impl Combinator<Problem> {
Tuple2::new(
Choice::new(vec![
Box::new(Dict::new(true, "r/")),
Box::new(Dict::new(false, "")),
]),
Size::new(Tuple2::new(
Rooms,
PrefixAndSuffix::new(
"/",
OutsideCells2::new(Choice::new(vec![
Box::new(Optionalize::new(HexInt)),
Box::new(Spaces::new(None, 'g')),
])),
"",
),
)),
)
}
pub fn serialize_problem(problem: &Problem) -> Option<String> {
let height = problem.1 .1 .1.len();
let width = problem.1 .1 .0.len();
problem_to_url_with_context(
combinator(),
"aquarium",
problem.clone(),
&Context::sized(height, width),
)
}
pub fn deserialize_problem(url: &str) -> Option<Problem> {
url_to_problem(combinator(), &["aquarium"], url)
}
#[cfg(test)]
mod tests {
use super::*;
fn problem_for_tests1() -> Problem {
let borders = graph::InnerGridEdges {
horizontal: crate::util::tests::to_bool_2d([
[0, 1, 1, 1, 1],
[1, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
]),
vertical: crate::util::tests::to_bool_2d([
[0, 1, 0, 0],
[1, 1, 0, 1],
[0, 1, 1, 1],
[0, 1, 0, 1],
]),
};
let clues_up = vec![Some(3), None, None, Some(1), Some(2)];
let clues_left = vec![None, None, Some(1), Some(3)];
(false, (borders, (clues_up, clues_left)))
}
fn problem_for_tests2() -> Problem {
let borders = graph::InnerGridEdges {
horizontal: crate::util::tests::to_bool_2d([
[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
]),
vertical: crate::util::tests::to_bool_2d([
[0, 0, 0, 0],
[1, 0, 0, 1],
[1, 1, 1, 1],
[0, 1, 0, 1],
]),
};
let clues_up = vec![None, None, None, None, None];
let clues_left = vec![None, Some(2), Some(4), None];
(true, (borders, (clues_up, clues_left)))
}
#[test]
fn test_aquarium_problem() {
{
let (region_aware, (borders, (clues_up, clues_left))) = problem_for_tests1();
let ans = solve_aquarium(region_aware, &borders, &clues_up, &clues_left);
assert!(ans.is_some());
let ans = ans.unwrap();
let expected = crate::util::tests::to_option_bool_2d([
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 0, 0, 1],
]);
assert_eq!(ans, expected);
}
{
let (region_aware, (borders, (clues_up, clues_left))) = problem_for_tests2();
let ans = solve_aquarium(region_aware, &borders, &clues_up, &clues_left);
assert!(ans.is_some());
let ans = ans.unwrap();
let expected = crate::util::tests::to_option_bool_2d([
[-1, -1, -1, -1, -1],
[1, 0, 0, 0, 1],
[-1, 1, -1, 1, -1],
[-1, -1, -1, -1, -1],
]);
assert_eq!(ans, expected);
}
}
#[test]
fn test_aquarium_serializer() {
{
let problem = problem_for_tests1();
let url = "https://puzz.link/p?aquarium/5/4/9lqgfk4/3h12h13";
crate::util::tests::serializer_test(
problem,
url,
serialize_problem,
deserialize_problem,
);
}
{
let problem = problem_for_tests2();
let url = "https://puzz.link/p?aquarium/r/5/4/17qgela/l24g";
crate::util::tests::serializer_test(
problem,
url,
serialize_problem,
deserialize_problem,
);
}
}
}