generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12.rs
161 lines (129 loc) · 4.32 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
use advent_of_code::{Direction, Grid, Vec2};
use std::{collections::HashSet, vec};
advent_of_code::solution!(12);
const DIRECTIONS: [Direction; 4] = [
Direction::Up,
Direction::Right,
Direction::Down,
Direction::Left,
];
#[derive(Debug)]
struct Region {
id: char,
members: Vec<Vec2>,
}
impl Region {
pub fn area(&self) -> u32 {
self.members.len() as u32
}
pub fn perimiter(&self) -> u32 {
let mut result = 0;
for member in &self.members {
for direction in DIRECTIONS {
let neighbour_cell = member.add(&direction.get_offset());
if !self.members.contains(&neighbour_cell) {
result += 1;
}
}
}
result
}
pub fn sides(&self) -> u32 {
let mut fences = vec![];
for member in &self.members {
for direction in DIRECTIONS {
let neighbour_cell = member.add(&direction.get_offset());
if !self.members.contains(&neighbour_cell) {
fences.push((neighbour_cell, direction));
}
}
}
for (possible_side_starting_fence_pos, fence_direction) in fences.clone() {
let offset_x = Vec2::new(1, 0);
let mut expected_next_fence = (
possible_side_starting_fence_pos.add(&offset_x),
fence_direction.clone(),
);
while fences.contains(&expected_next_fence) {
let index = fences
.iter()
.position(|p| *p == expected_next_fence)
.unwrap();
fences.remove(index);
expected_next_fence.0 = expected_next_fence.0.add(&offset_x);
}
let offset_y = Vec2::new(0, 1);
let mut expected_next_fence = (
possible_side_starting_fence_pos.add(&offset_y),
fence_direction.clone(),
);
while fences.contains(&expected_next_fence) {
let index = fences
.iter()
.position(|p| *p == expected_next_fence)
.unwrap();
fences.remove(index);
expected_next_fence.0 = expected_next_fence.0.add(&offset_y);
}
}
fences.len() as u32
}
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = Grid::from_input(input);
let regions = parse_regions(&grid);
Some(regions.iter().map(|r| r.area() * r.perimiter()).sum())
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = Grid::from_input(input);
let regions = parse_regions(&grid);
Some(regions.iter().map(|r| r.area() * r.sides()).sum())
}
fn parse_regions(grid: &Grid) -> Vec<Region> {
let mut visited_positions: HashSet<Vec2> = HashSet::new();
let mut regions = vec![];
for (positon, c) in grid.iter() {
if !visited_positions.contains(&positon) {
let mut queue = vec![];
let mut region = Region {
id: c,
members: vec![],
};
queue.push(positon);
while !queue.is_empty() {
let current_position = queue.pop().unwrap();
if visited_positions.contains(¤t_position) {
continue;
}
region.members.push(current_position);
visited_positions.insert(current_position);
for direction in &DIRECTIONS {
if let Some((neighbour_pos, neighbour_id)) =
grid.get_cell_in_direction(¤t_position, direction)
{
if neighbour_id != region.id {
continue;
}
queue.push(neighbour_pos);
}
}
}
regions.push(region);
}
}
regions
}
#[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));
}
}