-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
182 lines (160 loc) · 5.36 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
use std::collections::{HashSet, VecDeque};
use aoc_lib::{
answer::Answer,
directions::{Advance, Cardinal, Direction},
matrix::Matrix,
solution::Solution,
vec2::Vec2,
};
pub struct Day12;
impl Solution for Day12 {
fn part_a(&self, input: &[String]) -> Answer {
let mut map = Map::from_input(input);
map.fence_cost(false).into()
}
fn part_b(&self, input: &[String]) -> Answer {
let mut map = Map::from_input(input);
map.fence_cost(true).into()
}
}
struct Map {
map: Matrix<char>,
regions: Option<Vec<Region>>,
}
struct Region {
area: HashSet<Vec2<usize>>,
perimeter: usize,
corners: usize,
}
impl Region {
fn new() -> Self {
Self {
area: HashSet::new(),
perimeter: 0,
corners: 0,
}
}
}
impl Map {
fn from_input(input: &[String]) -> Self {
Self {
map: Matrix::from_chars(input),
regions: None,
}
}
fn compute_regions(&mut self) {
let mut all = vec![];
let mut visited = HashSet::new();
for y in 0..self.map.rows {
for x in 0..self.map.cols {
let pos = Vec2::new(x, y);
if !visited.contains(&pos) {
let region = self.flood(pos, &mut visited);
all.push(region);
}
}
}
self.regions = Some(all);
}
fn flood(&self, initial: Vec2<usize>, visited: &mut HashSet<Vec2<usize>>) -> Region {
let mut region = Region::new();
let mut queue: VecDeque<Vec2<usize>> = VecDeque::new();
queue.push_front(initial);
visited.insert(initial);
while let Some(pos) = queue.pop_front() {
region.area.insert(pos);
for direction in Cardinal::all_clockwise() {
let next: Vec2<isize> = direction.advance(pos.into());
let Some(&next_plant_type) = self.map.get(&next) else {
region.perimeter += 1;
continue;
};
if next_plant_type != self.map[pos] {
region.perimeter += 1;
continue;
}
if let Ok(n) = Vec2::<usize>::try_from(&next) {
if !visited.contains(&n) {
visited.insert(n);
queue.push_back(n);
}
}
}
}
region
}
// The corners can be counted only after all the connected components of the graph
// has been fully computed
fn count_corners(&mut self) {
for region in self.regions.as_mut().unwrap() {
let area = region
.area
.iter()
.map(|&p| Vec2::<isize>::from(p))
.collect::<HashSet<_>>();
for tile in ®ion.area {
let sized_tile = Vec2::<isize>::from(tile);
// Check for corners going inside exterior (Concave from the point of view of the
// tile)
// Exemple : You don't have front and right, you are at a corner.
for direction in Cardinal::all_clockwise() {
if !area.contains(&direction.advance(sized_tile))
&& !area.contains(&direction.turn_right().advance(sized_tile))
{
region.corners += 1;
}
}
// Check for corners going inside a hole (Convex from the point of view of the tile)
// A convex corner is when you have both neighbors but missing the diagonal
// Exemple : You have front and right but don't have front right, you are at a
// corner
for direction in Cardinal::all_clockwise() {
let diagonal = direction
.turn_right()
.advance(direction.advance(sized_tile));
if area.contains(&direction.advance(sized_tile))
&& area.contains(&direction.turn_right().advance(sized_tile))
&& !area.contains(&diagonal)
{
region.corners += 1;
}
}
}
}
}
fn fence_cost(&mut self, discount: bool) -> usize {
self.compute_regions();
if discount {
self.count_corners();
}
let mut total = 0;
for region in self.regions.as_ref().unwrap() {
let mul = if discount {
region.corners
} else {
region.perimeter
};
total += region.area.len() * mul;
}
total
}
}
#[cfg(test)]
mod test {
use aoc_lib::{answer::Answer, input, solution::Solution};
use super::Day12;
#[test]
fn test_a() {
let input =
input::read_file(&format!("{}day_12_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day12.part_a(&input);
assert_eq!(<i32 as Into<Answer>>::into(1930), answer);
}
#[test]
fn test_b() {
let input =
input::read_file(&format!("{}day_12_test.txt", crate::FILES_PREFIX_TEST)).unwrap();
let answer = Day12.part_b(&input);
assert_eq!(<i32 as Into<Answer>>::into(1206), answer);
}
}