-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.rs
173 lines (157 loc) · 4.38 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
use std::collections::{BTreeMap, BTreeSet};
use if_chain::if_chain;
use itertools::Itertools;
struct Data<'a> {
lines: Vec<&'a str>,
}
impl Data<'_> {
fn new(data: &str) -> Data {
Data {
lines: data.lines().collect(),
}
}
fn groups(&self) -> impl Iterator<Item = BTreeSet<(usize, usize)>> + use<'_> {
self.lines
.iter()
.enumerate()
.flat_map(|(y, line)| line.bytes().enumerate().map(move |(x, b)| ((y, x), b)))
.scan(BTreeSet::new(), |visited, (pos, b)| {
if !visited.insert(pos) {
return Some(None);
}
let mut group = BTreeSet::new();
let mut stack = vec![pos];
while let Some(pos @ (y, x)) = stack.pop() {
group.insert(pos);
for (dy, dx) in [(-1, 0), (0, -1), (0, 1), (1, 0)] {
if_chain! {
if let Some(y) = y.checked_add_signed(dy);
if let Some(line) = self.lines.get(y);
if let Some(x) = x.checked_add_signed(dx);
if line.as_bytes().get(x).copied() == Some(b);
if visited.insert((y, x));
then {
stack.push((y, x));
}
}
}
}
Some(Some(group))
})
.flatten()
}
}
fn perimeter1(group: &BTreeSet<(usize, usize)>) -> usize {
let mut edges = BTreeMap::<_, u8>::new();
for (y, x) in group.iter() {
for edge in [
(2 * y, 2 * x + 1),
(2 * y + 1, 2 * x),
(2 * y + 1, 2 * x + 2),
(2 * y + 2, 2 * x + 1),
] {
*edges.entry(edge).or_default() += 1;
}
}
edges.into_values().filter(|value| *value == 1).count()
}
pub fn part1(data: &str) -> usize {
Data::new(data)
.groups()
.map(|group| group.len() * perimeter1(&group))
.sum()
}
fn perimeter2(group: &BTreeSet<(usize, usize)>) -> usize {
let mut edges = BTreeMap::<_, u8>::new();
for (y, x) in group.iter() {
*edges.entry((2 * y, 2 * x + 1)).or_default() |= 1;
*edges.entry((2 * y + 1, 2 * x)).or_default() |= 2;
*edges.entry((2 * y + 1, 2 * x + 2)).or_default() |= 8;
*edges.entry((2 * y + 2, 2 * x + 1)).or_default() |= 4;
}
let mut lines = BTreeMap::<_, BTreeSet<_>>::new();
for ((y, x), n) in edges {
if n & (n - 1) == 0 {
lines
.entry(if n.ilog2() % 2 == 0 { y + 1 } else { x })
.or_default()
.insert((x + y, n));
}
}
lines
.into_values()
.map(|line| {
line.into_iter()
.tuple_windows()
.filter(|((p, b), (q, d))| p.abs_diff(*q) > 2 || b != d)
.count()
+ 1
})
.sum()
}
pub fn part2(data: &str) -> usize {
Data::new(data)
.groups()
.map(|group| group.len() * perimeter2(&group))
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE_1: &str = indoc! {"
AAAA
BBCD
BBCC
EEEC
"};
static EXAMPLE_2: &str = indoc! {"
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO
"};
static EXAMPLE_3: &str = indoc! {"
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE
"};
static EXAMPLE_4: &str = indoc! {"
EEEEE
EXXXX
EEEEE
EXXXX
EEEEE
"};
static EXAMPLE_5: &str = indoc! {"
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA
"};
#[test]
fn part1_examples() {
assert_eq!(140, part1(EXAMPLE_1));
assert_eq!(772, part1(EXAMPLE_2));
assert_eq!(1930, part1(EXAMPLE_3));
}
#[test]
fn part2_examples() {
assert_eq!(80, part2(EXAMPLE_1));
assert_eq!(436, part2(EXAMPLE_2));
assert_eq!(236, part2(EXAMPLE_4));
assert_eq!(368, part2(EXAMPLE_5));
assert_eq!(1206, part2(EXAMPLE_3));
}
}