-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-13.rs
148 lines (131 loc) · 3.7 KB
/
day-13.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
use std::fmt::Display;
use aoc_2023::{aoc, str_block, Error};
const INPUT: &str = include_str!("day-13.txt");
#[allow(dead_code)]
const INPUT_EX: &str = str_block! {"
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
"};
aoc! {
struct Day13 {
maps: Vec<Map>,
}
self(input = INPUT) {
Ok(Self {
maps: input.split("\n\n").map(|map|
Map(map.trim().lines().map(|line| line.as_bytes().to_owned()).collect())
).collect()
})
}
1 part1 usize {
self.maps.iter().map(|map| {
map.score().ok_or_else(|| format!("no reflections found in\n{map}").into())
}).sum::<Result<_, _>>()
}
2 part2 usize {
Ok(self.maps.iter_mut().map(Map::repair_score).sum::<Result<_, _>>()?)
}
INPUT_EX { 1 part1 = 405, 2 part2 = 400 }
INPUT { 1 part1 = 40006, 2 part2 = 28627 }
}
#[derive(Clone)]
struct Map(Vec<Vec<u8>>);
impl Display for Map {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for row in self.0.iter() {
for c in row.iter() {
write!(f, "{}", char::from(*c))?;
}
writeln!(f)?;
}
Ok(())
}
}
impl Map {
fn width(&self) -> usize {
self.0[0].len()
}
fn height(&self) -> usize {
self.0.len()
}
fn score(&self) -> Option<usize> {
self.find_horizontal_mirror(|_| true)
.map(|y| y * 100)
.or_else(|| self.find_vertical_mirror(|_| true))
}
fn repair_score(&mut self) -> Result<usize, Error> {
let h = self.find_horizontal_mirror(|_| true);
let v = self.find_vertical_mirror(|_| true);
for y in 0..self.height() {
for x in 0..self.width() {
let tile = self.0[y][x];
self.0[y][x] = match tile {
b'#' => b'.',
b'.' => b'#',
_ => continue,
};
let score = self
.find_horizontal_mirror(|y| if let Some(h) = h { h != y } else { true })
.map(|y| y * 100)
.or_else(|| {
self.find_vertical_mirror(|x| if let Some(v) = v { v != x } else { true })
});
self.0[y][x] = tile;
if let Some(score) = score {
return Ok(score);
}
}
}
Err(format!("no smudge found:\n{self}").into())
}
fn find_horizontal_mirror(&self, accept: impl Fn(usize) -> bool) -> Option<usize> {
'find: for y in 1..self.height() {
if !accept(y) {
continue;
}
let mut ym = y as isize - 1;
let mut yp = y;
while ym >= 0 && yp < self.height() {
if self.0[ym as usize] != self.0[yp] {
continue 'find;
}
ym -= 1;
yp += 1;
}
return Some(y);
}
None
}
fn find_vertical_mirror(&self, accept: impl Fn(usize) -> bool) -> Option<usize> {
'find: for x in 1..self.width() {
if !accept(x) {
continue;
}
let mut xm = x as isize - 1;
let mut xp = x;
while xm >= 0 && xp < self.width() {
for row in self.0.iter() {
if row[xm as usize] != row[xp] {
continue 'find;
}
}
xm -= 1;
xp += 1;
}
return Some(x);
}
None
}
}