-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.rs
85 lines (74 loc) · 2.07 KB
/
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
#![feature(test)]
type Input = Vec<Grid>;
type Grid = Vec<Vec<bool>>;
fn setup(input: &str) -> Input {
input
.split("\n\n")
.map(|block| {
block
.lines()
.map(|line| line.bytes().map(|b| b == b'#').collect())
.collect()
})
.collect()
}
#[derive(Debug, Clone, Copy)]
enum Reflection {
Vertical(usize),
Horizontal(usize),
}
impl Reflection {
fn generate_for(grid: &Grid) -> impl Iterator<Item = Self> {
(1..grid.len())
.map(Self::Horizontal)
.chain((1..grid[0].len()).map(Self::Vertical))
}
fn count_mismatches(self, grid: &Grid) -> usize {
match self {
Reflection::Vertical(j) => (0..grid.len())
.map(|i| {
let len = j.min(grid[0].len() - j);
(j - len..j)
.rev()
.zip(j..j + len)
.filter(|&(j1, j2)| grid[i][j1] != grid[i][j2])
.count()
})
.sum(),
Reflection::Horizontal(i) => (0..grid[0].len())
.map(|j| {
let len = i.min(grid.len() - i);
(i - len..i)
.rev()
.zip(i..i + len)
.filter(|&(i1, i2)| grid[i1][j] != grid[i2][j])
.count()
})
.sum(),
}
}
fn summarize(self) -> usize {
match self {
Self::Vertical(j) => j,
Self::Horizontal(i) => 100 * i,
}
}
}
fn solve<const N: usize>(input: &Input) -> usize {
input
.iter()
.map(|grid| {
Reflection::generate_for(grid)
.find(|reflection| reflection.count_mismatches(grid) == N)
.unwrap()
.summarize()
})
.sum()
}
fn part1(input: &Input) -> usize {
solve::<0>(input)
}
fn part2(input: &Input) -> usize {
solve::<1>(input)
}
aoc::main!(2023, 13, ex: 1);