-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
195 lines (177 loc) · 4.91 KB
/
day12.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
use aoc_runner_derive::aoc;
use itertools::Itertools;
use strum::IntoEnumIterator;
#[aoc(day12, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
let mut map = Map::parse(input);
let mut seen_pos = std::collections::HashSet::default();
(0..map.ncols())
.cartesian_product(0..map.nrows())
.filter_map(|pos| map.flood(&mut seen_pos, pos))
.map(|(area, perim)| area.len() * perim)
.sum()
}
#[aoc(day12, part2)]
#[must_use]
pub fn part2(input: &str) -> usize {
let mut map = Map::parse(input);
let mut seen_pos = std::collections::HashSet::default();
(0..map.ncols())
.cartesian_product(0..map.nrows())
.filter_map(|x| map.flood(&mut seen_pos, x))
.map(|(area, _)| {
let mut n_corners = 0;
for &point in &area {
for a in Direction::iter() {
let b = a.right();
n_corners += usize::from(
area.contains(&a.next(point))
&& area.contains(&b.next(point))
&& !area.contains(&b.next(a.next(point))),
);
}
for a in Direction::iter() {
n_corners += usize::from(
!area.contains(&a.next(point)) && !area.contains(&a.right().next(point)),
);
}
}
n_corners * area.len()
})
.sum()
}
#[repr(transparent)]
#[derive(Clone, derive_more::Deref, derive_more::DerefMut)]
struct Map(nalgebra::DMatrix<u8>);
impl Map {
fn parse(input: &str) -> Self {
let input = input.as_bytes();
let width = input.iter().position(|&c| c == b'\n').unwrap();
let mut map = nalgebra::DMatrix::from_element(width, (input.len() - 1) / width, b'0');
input.chunks(width + 1).enumerate().for_each(|(y, line)| {
line.iter().enumerate().take(width).for_each(|(x, &c)| {
map[(x, y)] = c;
});
});
Self(map)
}
fn flood(
&mut self,
seen_pos: &mut rustc_hash::FxHashSet<(usize, usize)>,
start: (usize, usize),
) -> Option<(rustc_hash::FxHashSet<(usize, usize)>, usize)> {
if !seen_pos.insert(start) {
return None;
}
let mut area = rustc_hash::FxHashSet::default();
area.insert(start);
let mut queue = Vec::new();
queue.push(start);
let plant = self.get(start).unwrap();
let mut perim = 0;
while let Some(pos) = queue.pop() {
for next in Direction::iter().map(|x| x.next(pos)) {
if next.0 >= self.ncols() || next.1 >= self.nrows() {
perim += 1;
continue;
}
if self.get(next).unwrap() == plant {
if seen_pos.insert(next) {
area.insert(next);
queue.push(next);
}
continue;
}
perim += 1;
}
}
Some((area, perim))
}
}
#[derive(Clone, Copy, strum::EnumIter)]
enum Direction {
Up,
Down,
Left,
Right,
}
impl Direction {
#[must_use]
fn next(&self, pos: (usize, usize)) -> (usize, usize) {
match self {
Self::Up => (pos.0, pos.1.wrapping_sub(1)),
Self::Down => (pos.0, pos.1 + 1),
Self::Left => (pos.0.wrapping_sub(1), pos.1),
Self::Right => (pos.0 + 1, pos.1),
}
}
#[must_use]
fn right(self) -> Self {
match self {
Self::Up => Self::Right,
Self::Down => Self::Left,
Self::Left => Self::Up,
Self::Right => Self::Down,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE1: &str = indoc! {"
AAAA
BBCD
BBCC
EEEC
"};
const SAMPLE2: &str = indoc! {"
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO
"};
const SAMPLE3: &str = indoc! {"
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE
"};
const SAMPLE4: &str = indoc! {"
EEEEE
EXXXX
EEEEE
EXXXX
EEEEE
"};
const SAMPLE5: &str = indoc! {"
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE1), 140);
assert_eq!(part1(SAMPLE2), 772);
assert_eq!(part1(SAMPLE3), 1930);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE1), 80);
assert_eq!(part2(SAMPLE2), 436);
assert_eq!(part2(SAMPLE3), 1206);
assert_eq!(part2(SAMPLE4), 236);
assert_eq!(part2(SAMPLE5), 368);
}
}