-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_12.rs
137 lines (113 loc) Β· 3.53 KB
/
day_12.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
use std::{collections::HashSet, convert::identity};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use itertools::Itertools;
use nd_vec::{vector, Vec2};
solution!("Garden Groups", 12);
fn part_a(input: &str) -> Answer {
let mut garden = Garden::parse(input);
garden
.points()
.filter_map(|x| garden.flood(x))
.map(|(area, perimeter)| area.len() * perimeter)
.sum::<usize>()
.into()
}
fn part_b(input: &str) -> Answer {
let mut garden = Garden::parse(input);
let mut sum = 0;
for (area, _) in garden.points().filter_map(|x| garden.flood(x)) {
let mut corners = 0;
for &point in area.iter() {
// Count convex corners by checking to see that the wall is not in
// any cardinal direction and a direction orthogonal to that
for a in Direction::ALL {
corners += (!area.contains(&a.wrapping_advance(point))
&& !area.contains(&a.turn_right().wrapping_advance(point)))
as u32;
}
// Count the concave angles by looking for when both the orthogonal
// directions are in the area, but not the diagonal between them.
for a in Direction::ALL {
let b = a.turn_right();
corners += (area.contains(&a.wrapping_advance(point))
&& area.contains(&b.wrapping_advance(point))
&& !area.contains(&b.wrapping_advance(a.wrapping_advance(point))))
as u32;
}
}
sum += area.len() as u32 * corners;
}
sum.into()
}
struct Garden {
matrix: Grid<char>,
seen: HashSet<Vec2<usize>>,
}
impl Garden {
fn parse(input: &str) -> Self {
let matrix = Grid::parse(input, identity);
Self {
matrix,
seen: HashSet::new(),
}
}
fn points(&self) -> impl Iterator<Item = Vec2<usize>> {
let size = self.matrix.size;
(0..size.x())
.cartesian_product(0..size.y())
.map(|(x, y)| vector!(x, y))
}
fn flood(&mut self, start: Vec2<usize>) -> Option<(HashSet<Vec2<usize>>, usize)> {
if !self.seen.insert(start) {
return None;
}
let mut area = HashSet::new();
let mut perimeter = 0;
let mut queue = Vec::new();
let plant = self.matrix.get(start).unwrap();
area.insert(start);
queue.push(start);
while let Some(pos) = queue.pop() {
for next in Direction::ALL.map(|x| x.wrapping_advance(pos)) {
if !self.matrix.contains(next) {
perimeter += 1;
continue;
}
if self.matrix.get(next).unwrap() == plant {
if self.seen.insert(next) {
area.insert(next);
queue.push(next);
}
} else {
perimeter += 1;
}
}
}
Some((area, perimeter))
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 1930.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 1206.into());
}
}