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
use crate::Solver;
use std::collections::HashSet;
enum Color {
Red,
Yellow,
Blue,
Green,
Black,
}
pub struct Aztec {
n: usize,
solver: Solver,
}
impl Aztec {
pub fn new(n: usize) -> Aztec {
let mut solver = Solver::new(2 * n * (n + 1));
let max = 2 * n * (n + 1);
let row_ends_top = (1..=n).map(|x| x * (x + 1));
let row_ends_bottom = (1..=n).map(|x| max - x * (x - 1));
let row_ends = row_ends_top.chain(row_ends_bottom);
let row_ends_set: HashSet<usize> = HashSet::from_iter(row_ends);
println!();
for x in 1..=2 * n * (n + 1) {
if !row_ends_set.contains(&x) {
let pos1 = x;
let pos2 = pos1 + 1;
let con_name = format!("H{}#{}", pos1, pos2);
solver.add_option(&con_name, &[pos1, pos2]);
}
}
for j in 1..=n - 1 {
for i in 1..=2 * j {
let pos1 = j * (j - 1) + i;
let pos2 = pos1 + 2 * j + 1;
let con_name = format!("V{}#{}", pos1, pos2);
solver.add_option(&con_name, &[pos1, pos2]);
let mpos1 = max - pos1 + 1;
let mpos2 = max - pos2 + 1;
let mcon_name = format!("V{}#{}", mpos2, mpos1);
solver.add_option(&mcon_name, &[mpos2, mpos1]);
}
}
let final_min = n * (n - 1) + 1;
let final_max = final_min + 2 * n - 1;
for x in final_min..=final_max {
let pos1 = x;
let pos2 = x + 2 * n;
let con_name = format!("V{}#{}", pos1, pos2);
solver.add_option(&con_name, &[pos1, pos2]);
}
Aztec { solver, n }
}
pub fn pretty_print_sol(sol: &[(usize, usize)]) {
let n = (sol.len() as f64).sqrt() as usize;
let max = 2 * n * (n + 1);
let row_ends_top = (1..=n).map(|x| x * (x + 1));
let row_ends_bottom = (1..=n).map(|x| max - x * (x - 1));
let row_ends = row_ends_top.chain(row_ends_bottom);
let row_ends_set: HashSet<usize> = HashSet::from_iter(row_ends);
let mut solc: Vec<Color> = Vec::with_capacity(2 * sol.len());
for _ in 1..=2 * sol.len() {
solc.push(Color::Black);
}
for item in sol {
let min = (item.0).min(item.1);
let max = (item.0).max(item.1);
let par = match min {
x if x > n * (n + 1) => 1,
_ => 0,
};
if max == min + 1 {
if min % 2 == par {
solc[min - 1] = Color::Blue;
solc[max - 1] = Color::Blue;
} else {
solc[min - 1] = Color::Yellow;
solc[max - 1] = Color::Yellow;
}
} else if min % 2 == par {
solc[min - 1] = Color::Green;
solc[max - 1] = Color::Green;
} else {
solc[min - 1] = Color::Red;
solc[max - 1] = Color::Red;
}
}
let mut row_dir = true;
let mut row_pad = n;
let mut rr = " ".repeat(row_pad);
print!("{}", rr);
for (i, _) in solc.iter().enumerate() {
match solc[i] {
Color::Red => print!("\x1b[31;41mX\x1b[0m"),
Color::Green => print!("\x1b[32;42mX\x1b[0m"),
Color::Yellow => print!("\x1b[33;43mX\x1b[0m"),
Color::Blue => print!("\x1b[34;44mX\x1b[0m"),
Color::Black => print!("\x1b[30;40mX\x1b[0m"),
};
if row_ends_set.contains(&(i + 1)) {
if row_dir {
row_pad -= 1;
if row_pad == 0 {
row_dir = false;
row_pad += 1;
}
} else {
row_pad += 1;
}
println!();
rr = " ".repeat(row_pad);
print!("{}", rr);
}
}
println!();
}
}
impl Iterator for Aztec {
type Item = Vec<(usize, usize)>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(sol) = self.solver.next() {
let mut dom_solved = Vec::with_capacity(self.n);
for i in sol {
let i = i.as_str();
let s: Vec<&str> = i.split(&['H', 'V', '#']).collect();
let p1: usize = s[1].parse().unwrap();
let p2: usize = s[2].parse().unwrap();
dom_solved.push((p1, p2));
}
Some(dom_solved)
} else {
None
}
}
}