/
day_13.rs
157 lines (136 loc) · 3.64 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
149
150
151
152
153
154
155
156
157
use std::{collections::VecDeque, fmt::Display};
use aoc_core::AoCError;
use crate::{AoCData, AoCResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Tile {
Ash,
Rock,
}
#[derive(Debug, Clone)]
pub struct Data(Vec<VecDeque<Vec<Tile>>>);
fn reflects_at(grid: &VecDeque<Vec<Tile>>, smudges: usize) -> Option<usize> {
(1..grid.len()).find(|&offset| {
let half1 = grid.iter().take(offset).rev();
let half2 = grid.iter().skip(offset);
let combined = half1.zip(half2); // the shortest half determines how long this is!
let found_smudges: usize = combined
.map(|(row1, row2)| {
row1.iter()
.zip(row2.iter())
.filter(|(a, b)| a != b)
.count()
})
.sum();
found_smudges == smudges
})
}
impl AoCData<'_> for Data {
fn try_new(input: &str) -> AoCResult<Self> {
let blocks = input
.split("\n\n")
.map(|block| {
block
.lines()
.map(|line| {
line.chars()
.map(|c| match c {
'.' => Ok(Tile::Ash),
'#' => Ok(Tile::Rock),
_ => Err(AoCError::Parsing),
})
.collect::<AoCResult<Vec<_>>>()
})
.collect::<AoCResult<VecDeque<_>>>()
})
.collect::<AoCResult<_>>()?;
Ok(Self(blocks))
}
fn part_1(&self) -> AoCResult<impl Display> {
let sum: usize = self
.0
.iter()
.map(|grid| {
// check horizontal
if let Some(i) = reflects_at(grid, 0) {
return i * 100;
}
// check vertical
let cols = (0..grid[0].len())
.map(|i| grid.iter().map(|row| row[i]).collect())
.collect();
if let Some(i) = reflects_at(&cols, 0) {
return i;
}
// no reflection found
0
})
.sum();
Ok(sum)
}
fn part_2(&self) -> AoCResult<impl Display> {
let sum: usize = self
.0
.iter()
.map(|grid| {
// check horizontal
if let Some(i) = reflects_at(grid, 1) {
return i * 100;
}
// check vertical
let cols = (0..grid[0].len())
.map(|i| grid.iter().map(|row| row[i]).collect())
.collect();
if let Some(i) = reflects_at(&cols, 1) {
return i;
}
// no reflection found
0
})
.sum();
Ok(sum)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn part_1() {
let input = "#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#";
let data = Data::try_new(input).unwrap();
let result = data.part_1().unwrap().to_string();
assert_eq!(result, "405");
}
#[test]
fn part_2() {
let input = "#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#";
let data = Data::try_new(input).unwrap();
let result = data.part_2().unwrap().to_string();
assert_eq!(result, "400");
}
}