generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.rs
165 lines (146 loc) · 4.96 KB
/
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
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
use std::collections::HashSet;
use itertools::Itertools;
advent_of_code::solution!(12);
const DIRECTIONS: [(isize, isize); 4] = [
(0, -1),
(1, 0),
(0, 1),
(-1, 0),
];
#[derive(Debug)]
struct Grid {
width: usize,
height: usize,
data: Vec<char>,
}
impl Grid {
fn parse(input: &str) -> Grid {
let width = input.lines().next().unwrap_or_default().len();
let data = input.lines()
.flat_map(|line| line.chars())
.collect_vec();
let height = data.len() / width;
Grid {
height,
width,
data,
}
}
fn get_perimeter(&self, index: usize, target: &char, visited: &mut HashSet<usize>) -> u32 {
if visited.contains(&index) {
return 0;
}
if self.data[index] != *target {
return 1;
}
visited.insert(index);
DIRECTIONS.iter()
.map(|direction| self.offset(&index, direction))
.map(|pos| pos.map(|pos| self.get_perimeter(pos, target, visited)).unwrap_or(1))
.sum()
}
fn get_vertices(&self, index: usize, target: &char, visited: &mut HashSet<usize>) -> u32 {
if self.data[index] != *target || visited.contains(&index) {
return 0;
}
visited.insert(index);
let vertices = [
((0, -1), (1, 0)),
((1, 0), (0, 1)),
((0, 1), (-1, 0)),
((-1, 0), (0, -1))
].iter()
.filter_map(|adjecent| self.get_vertex(index, target, *adjecent))
.count() as u32;
vertices + (DIRECTIONS.iter()
.filter_map(|direction| self.offset(&index, direction))
.map(|pos| self.get_vertices(pos, target, visited))
.sum::<u32>())
}
fn get_vertex(&self, index: usize, target: &char, adjacent: ((isize, isize), (isize, isize))) -> Option<(isize, isize)> {
let corner_offset = (adjacent.0.0 + adjacent.1.0, adjacent.0.1 + adjacent.1.1);
let is_corner_present = self.offset(&index, &corner_offset)
.map(|index| self.data[index])
.is_some_and(|char| char == *target);
let is_first_present = self.offset(&index, &adjacent.0)
.map(|index| self.data[index])
.is_some_and(|char| char == *target);
let is_second_present = self.offset(&index, &adjacent.1)
.map(|index| self.data[index])
.is_some_and(|char| char == *target);
if (is_first_present && is_second_present && !is_corner_present)
|| (!is_first_present && !is_second_present) {
let pos = self.index_to_xy(&index)?;
return Some((pos.0 as isize + corner_offset.0, pos.1 as isize + corner_offset.1));
}
None
}
fn next_unvisited(&self, mut current: usize, visited: &HashSet<usize>) -> usize {
while visited.contains(¤t) {
current += 1;
}
current
}
fn offset(&self, index: &usize, direction: &(isize, isize)) -> Option<usize> {
let pos = self.index_to_xy(index)?;
let x = pos.0.checked_add_signed(direction.0)?;
let y = pos.1.checked_add_signed(direction.1)?;
self.xy_to_index((x, y))
}
fn xy_to_index(&self, pos: (usize, usize)) -> Option<usize> {
if (0..self.width).contains(&pos.0) && (0..self.height).contains(&pos.1) {
Some(pos.1 * self.width + pos.0)
} else {
None
}
}
fn index_to_xy(&self, index: &usize) -> Option<(usize, usize)> {
if index < &(self.width * self.height) {
Some((index % self.width, index / self.width))
} else {
None
}
}
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = Grid::parse(input);
let len = grid.width * grid.height;
let mut index = 0;
let mut visited = HashSet::new();
let mut sum = 0;
while index < len {
let mut area = HashSet::new();
sum += grid.get_perimeter(index, &grid.data[index], &mut area) * area.len() as u32;
visited.extend(area);
index = grid.next_unvisited(index + 1, &visited);
}
Some(sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = Grid::parse(input);
let len = grid.width * grid.height;
let mut index = 0;
let mut visited = HashSet::new();
let mut sum = 0;
while index < len {
let mut area = HashSet::new();
sum += grid.get_vertices(index, &grid.data[index], &mut area) * area.len() as u32;
visited.extend(area);
index = grid.next_unvisited(index + 1, &visited);
}
Some(sum)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(1930));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(1206));
}
}