-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_20.rs
499 lines (453 loc) · 11.9 KB
/
day_20.rs
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use anyhow::{anyhow, Context, Result};
use fxhash::{FxHashMap as HashMap, FxHashSet as HashSet};
use itertools::Itertools;
#[derive(Eq, PartialEq, Debug, Clone)]
struct Tile {
id: usize,
content: Vec<Vec<bool>>,
}
fn parse(input: &str) -> Vec<Tile> {
input
.split("\n\n")
.filter_map(|block| {
// Tile: 1759:
let header = block.lines().next()?;
let (_, id) = header.split_once(' ')?;
let id = id[..id.len() - 1].parse().ok()?;
let content = block
.lines()
.skip(1)
.filter(|line| !line.is_empty())
.map(|line| line.as_bytes().iter().map(|ch| *ch == b'#').collect())
.collect();
Some(Tile { id, content })
})
.collect()
}
fn dec<'a>(it: &'a mut impl Iterator<Item = &'a bool>) -> u16 {
it.fold(0, |acc, bit| acc * 2 + u16::from(*bit))
}
const TOP: usize = 0;
const RIGHT: usize = 1;
const BOT: usize = 2;
const LEFT: usize = 3;
impl Tile {
fn edge_footprints(&self) -> [u16; 4] {
[
dec(&mut self.content[0].iter()), // TOP
dec(&mut self.content.iter().map(|row| &row[row.len() - 1])), // RIGHT
dec(&mut self.content[self.content.len() - 1].iter()), // BOT
dec(&mut self.content.iter().map(|row| &row[0])), // LEFT
]
}
}
#[derive(Eq, PartialOrd, PartialEq, Debug, Copy, Clone)]
struct Orientation {
rotations: u8,
flip_x: bool,
}
// Actually rotated this 2 x 2 matrix on pen / paper to derive all legal configurations:
// 12 | 31 | 43 | 24 | 12
// 34 | 42 | 21 | 13 | 34
// A rotation: read the columns from bottom to top; they are the rows of the result
// A flip_x: read the rows in reverse, they are the rows of the result
// Flipping any rotation around y only results in outputs that are already covered by the above
const LEGAL: [Orientation; 8] = [
Orientation {
rotations: 0,
flip_x: false,
},
Orientation {
rotations: 0,
flip_x: true,
},
Orientation {
rotations: 1,
flip_x: false,
},
Orientation {
rotations: 1,
flip_x: true,
},
Orientation {
rotations: 2,
flip_x: false,
},
Orientation {
rotations: 2,
flip_x: true,
},
Orientation {
rotations: 3,
flip_x: false,
},
Orientation {
rotations: 3,
flip_x: true,
},
];
impl Orientation {
fn of(&self, tile: &Tile) -> Tile {
let mut out = tile.clone();
self.on(&mut out);
out
}
fn on(&self, tile: &mut Tile) {
for _ in 0..self.rotations {
let v = (0..tile.content.len())
.map(|col| tile.content.iter().map(|row| row[col]).rev().collect_vec())
.collect_vec();
tile.content = v;
}
if self.flip_x {
tile.content.iter_mut().for_each(|row| row.reverse());
}
}
fn edges(&self, tile: &Tile) -> [u16; 4] {
let transformed = self.of(tile);
[
dec(&mut transformed.content[0].iter()), // top
dec(&mut transformed.content.iter().map(|row| &row[row.len() - 1])), // right
dec(&mut transformed.content[tile.content.len() - 1].iter()), // bot
dec(&mut transformed.content.iter().map(|row| &row[0])), // left
]
}
}
fn possible_edge_sets(tile: &Tile) -> [[u16; 4]; 8] {
LEGAL
.iter()
.map(|orientation| orientation.edges(tile))
.collect_vec()
.try_into()
.unwrap()
}
fn take_corners(tiles: &[Tile], edge_map: &HashMap<u16, HashSet<usize>>) -> [usize; 4] {
tiles
.iter()
.filter(|tile| {
possible_edge_sets(tile)
.iter()
.map(|edge_set| {
edge_set
.iter()
.filter(|edge| has_heighbour(**edge, edge_map))
.count()
})
.max()
.unwrap()
== 2
})
.map(|tile| tile.id)
.take(4)
.collect_vec()
.try_into()
.unwrap()
}
#[inline]
fn has_heighbour(edge: u16, edge_map: &HashMap<u16, HashSet<usize>>) -> bool {
edge_map
.get(&edge)
.map(|set| set.len() > 1)
.unwrap_or(false)
}
fn edge_map(tiles: &[Tile]) -> HashMap<u16, HashSet<usize>> {
let mut edge_map: HashMap<u16, HashSet<usize>> = HashMap::default();
for tile in tiles.iter() {
for edge_set in possible_edge_sets(tile) {
for edge in edge_set {
edge_map.entry(edge).or_default().insert(tile.id);
}
}
}
edge_map
}
fn corners(tiles: &[Tile]) -> [usize; 4] {
let edge_map = edge_map(tiles);
take_corners(tiles, &edge_map)
}
pub fn part_1(_input: &str) -> Result<String> {
let tiles = parse(_input);
let n: usize = corners(&tiles).into_iter().product();
Ok(format!("{n}"))
}
fn topleft(tiles: &[Tile], edge_map: &HashMap<u16, HashSet<usize>>) -> Option<Tile> {
let nw = take_corners(tiles, edge_map)[0];
let original = tiles.iter().find(|t| t.id == nw)?;
for orientation in LEGAL.iter() {
let oriented = orientation.of(original);
let edges = oriented.edge_footprints();
let top = edges[TOP];
let left = edges[LEFT];
let bot = edges[BOT];
let right = edges[RIGHT];
if !has_heighbour(top, edge_map)
&& !has_heighbour(left, edge_map)
&& has_heighbour(bot, edge_map)
&& has_heighbour(right, edge_map)
{
return Some(oriented);
}
}
None
}
fn backtracking_search(
used_tiles: &mut HashSet<usize>,
graph: &mut Vec<Vec<Tile>>,
tiles: &HashMap<usize, Tile>,
edge_map: &HashMap<u16, HashSet<usize>>,
connect_to: (usize, usize),
) -> bool {
// Connected all the pieces
if used_tiles.len() == tiles.len() {
return true;
}
let available = graph[connect_to.0][connect_to.1].edge_footprints();
let right = available[RIGHT];
let mut undo_push = false;
// Check if we filled a row (this assumes input is square)
let width = graph[connect_to.0].len();
let (next, (my_edge, their_side)) = if tiles.len() / width == width {
// Rewind to left so we can fill towards right again
let bot_left = graph[connect_to.0][0].edge_footprints()[BOT];
((connect_to.0 + 1, 0), (bot_left, TOP))
} else {
((connect_to.0, connect_to.1 + 1), (right, LEFT))
};
// Add new row and record it so we can backtrack
if next.0 >= graph.len() {
graph.push(vec![]);
undo_push = true;
}
let choices = edge_map
.get(&my_edge)
.iter()
.flat_map(|set| set.iter())
.filter(|choice| !used_tiles.contains(choice))
.collect_vec();
for choice in choices {
let candidate = tiles.get(choice).unwrap();
for orientation in LEGAL.iter() {
let add = orientation.of(candidate);
if add.edge_footprints()[their_side] == my_edge {
used_tiles.insert(add.id);
graph[next.0].push(add);
let solution = backtracking_search(used_tiles, graph, tiles, edge_map, next);
if solution {
return solution;
} else {
// backtrack and try another orientation or choice
let remove = graph[next.0].pop().unwrap();
used_tiles.remove(&remove.id);
}
}
}
}
if undo_push {
graph.pop();
}
false
}
fn fit_pieces(input: &str) -> Option<Vec<Vec<Tile>>> {
let tiles = parse(input);
let edge_map = edge_map(&tiles);
let nw = topleft(&tiles, &edge_map)?;
let tiles: HashMap<usize, Tile> = tiles.into_iter().map(|tile| (tile.id, tile)).collect();
let mut used_tiles: HashSet<_> = [nw.id].into_iter().collect();
let mut graph = vec![vec![nw]];
if backtracking_search(&mut used_tiles, &mut graph, &tiles, &edge_map, (0, 0)) {
Some(graph)
} else {
None
}
}
fn assemble_image(input: &str) -> Result<Tile> {
let mut solved_puzzle =
fit_pieces(input).with_context(|| anyhow!("Unable to puzzle tiles!"))?;
let cols = solved_puzzle[0].len();
let mut row_offs = 0;
let mut buf = vec![];
for tiles in solved_puzzle.iter_mut() {
for (tile_col, tile) in tiles.iter_mut().enumerate() {
tile.content.remove(0);
tile.content.pop();
tile.content.iter_mut().for_each(|row| {
row.remove(0);
});
tile.content.iter_mut().for_each(|row| {
row.pop();
});
if tile_col == 0 {
//
for _ in 0..tile.content.len() {
buf.push(vec![]);
}
}
for (row_no, row) in tile.content.iter().enumerate() {
buf[row_offs + row_no].extend(row.iter().copied());
}
if tile_col == cols - 1 {
row_offs += tile.content.len();
}
}
}
Ok(Tile {
content: buf,
id: 0,
})
}
const MONSTER: &str = " #
# ## ## ###
# # # # # # ";
fn count_sea_monsters(tile: &Tile) -> usize {
let monster: Vec<Vec<_>> = MONSTER
.lines()
.map(|line| line.chars().map(|ch| ch == '#').collect())
.collect();
let mut found = 0;
let yoff = tile.content.len() - monster.len();
let xoff = tile.content[0].len() - monster[0].len();
for tile_y in 0..yoff {
for tile_x in 0..xoff {
if (0..monster.len())
.cartesian_product(0..monster[0].len())
.all(|(y, x)| (tile.content[tile_y + y][tile_x + x]) || !(monster[y][x]))
{
found += monster
.iter()
.flat_map(|row| row.iter().filter(|b| **b))
.count();
}
}
}
found
}
fn solve_2(input: &str) -> Result<usize> {
let img = assemble_image(input)?;
let bits_set = img
.content
.iter()
.flat_map(|row| row.iter().filter(|b| **b))
.count();
let seamonster_bits = LEGAL
.iter()
.map(|orientation| count_sea_monsters(&orientation.of(&img)))
.max()
.unwrap_or(0);
Ok(bits_set - seamonster_bits)
}
pub fn part_2(input: &str) -> Result<String> {
solve_2(input).map(|n| format!("{n}"))
}
#[cfg(test)]
mod tests {
use super::*;
use std::assert_eq;
#[test]
fn test_parse() {
let tiles = parse(EXAMPLE);
assert_eq!(tiles.len(), 9);
}
#[test]
fn test_example_p2() {
assert_eq!(solve_2(EXAMPLE).unwrap(), 273);
}
const EXAMPLE: &str = "Tile 2311:
..##.#..#.
##..#.....
#...##..#.
####.#...#
##.##.###.
##...#.###
.#.#.#..##
..#....#..
###...#.#.
..###..###
Tile 1951:
#.##...##.
#.####...#
.....#..##
#...######
.##.#....#
.###.#####
###.##.##.
.###....#.
..#.#..#.#
#...##.#..
Tile 1171:
####...##.
#..##.#..#
##.#..#.#.
.###.####.
..###.####
.##....##.
.#...####.
#.##.####.
####..#...
.....##...
Tile 1427:
###.##.#..
.#..#.##..
.#.##.#..#
#.#.#.##.#
....#...##
...##..##.
...#.#####
.#.####.#.
..#..###.#
..##.#..#.
Tile 1489:
##.#.#....
..##...#..
.##..##...
..#...#...
#####...#.
#..#.#.#.#
...#.#.#..
##.#...##.
..##.##.##
###.##.#..
Tile 2473:
#....####.
#..#.##...
#.##..#...
######.#.#
.#...#.#.#
.#########
.###.#..#.
########.#
##...##.#.
..###.#.#.
Tile 2971:
..#.#....#
#...###...
#.#.###...
##.##..#..
.#####..##
.#..####.#
#..#.#..#.
..####.###
..#.#.###.
...#.#.#.#
Tile 2729:
...#.#.#.#
####.#....
..#.#.....
....#..#.#
.##..##.#.
.#.####...
####.#.#..
##.####...
##..#.##..
#.##...##.
Tile 3079:
#.#.#####.
.#..######
..#.......
######....
####.#..#.
.#...#.##.
#.#####.##
..#.###...
..#.......
..#.###...";
}