-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
176 lines (160 loc) · 4.15 KB
/
mod.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
use std::collections::{HashMap, HashSet};
use aoc_runner_derive::{aoc, aoc_generator};
#[allow(unused)]
use itertools::Itertools;
use crate::utils::point::Point;
type Num = usize;
type Output = Num;
type Pos = Point;
type Input = Vec<HashSet<Point>>;
#[aoc_generator(day12)]
pub fn input_generator(input: &str) -> Input {
let garden: HashMap<Point, char> = input
.lines()
.enumerate()
.flat_map(|(y, line)| {
line.chars()
.enumerate()
.map(move |(x, c)| (Point::from((x as isize, y as isize)), c))
})
.collect();
let mut regions: HashMap<Point, HashSet<Point>> = garden
.keys()
.map(|pos| (*pos, HashSet::from_iter(vec![*pos])))
.collect::<HashMap<_, _>>();
let directions = [
Point::from((1, 0)),
Point::from((0, 1)),
Point::from((-1, 0)),
Point::from((0, -1)),
];
for plant in garden.keys() {
for dir in directions {
if garden.contains_key(&(plant + &&dir)) && garden[plant] == garden[&(plant + &&dir)] {
*regions.get_mut(plant).unwrap() = regions[plant]
.union(®ions[&(plant + &&dir)])
.cloned()
.collect();
for other_plants in ®ions[plant].clone() {
*regions.get_mut(other_plants).unwrap() = regions[plant].clone();
}
}
}
}
regions.values().fold(Vec::new(), |mut urs, region| {
if !urs.contains(region) {
urs.push(region.clone());
urs
} else {
urs
}
})
}
fn area(region: &HashSet<Pos>) -> Num {
region.len()
}
fn perimeter(region: &HashSet<Point>) -> Num {
let directions = [
Point::from((1, 0)),
Point::from((0, 1)),
Point::from((-1, 0)),
Point::from((0, -1)),
];
region
.iter()
.cartesian_product(directions.iter())
.map(|(plant, dir)| plant + &dir)
.filter(|neighbour| !region.contains(neighbour))
.count()
}
#[aoc(day12, part1)]
pub fn solve_part1(input: &Input) -> Output {
input
.iter()
.map(|region| {
let area = area(region);
let perimeter = perimeter(region);
area * perimeter
})
.sum()
}
/// The amount of sides is equal to the amount of corners.
/// By "walking" the perimeter and checking wether a point is a corner *per direction* sides can be
/// counted. A corner is any point `a` with direction `a -> b` for which:
///
/// ```text
/// a b
/// c d
/// ```
///
/// * `b` and `c` are not part of `a`'s region:
///
/// ```text
/// A B
/// B ?
/// ```
///
/// * `b` is not part of of `a`'s region, but `c` and `d` are:
///
/// ```text
/// A B
/// A A
/// ```
fn corners(region: &HashSet<Point>) -> Num {
let directions = [
Point::from((1, 0)),
Point::from((0, 1)),
Point::from((-1, 0)),
Point::from((0, -1)),
];
region
.iter()
.cartesian_product(directions.iter())
.filter(|(pos, dir)| {
!region.contains(&(*dir + pos))
&& (!region.contains(&(**pos + dir.turn_clockwise()))
|| (region.contains(&(**pos + **dir + dir.turn_clockwise()))))
})
.count()
}
#[aoc(day12, part2)]
pub fn solve_part2(input: &Input) -> Output {
input
.iter()
.map(|region| {
let area = area(region);
let sides = corners(region);
area * sides
})
.sum()
}
pub fn part1(input: &str) -> impl std::fmt::Display {
solve_part1(&input_generator(input))
}
pub fn part2(input: &str) -> impl std::fmt::Display {
solve_part2(&input_generator(input))
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> &'static str {
"RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE"
}
#[test]
fn samples_part1() {
assert_eq!(1930, solve_part1(&input_generator(sample())));
}
#[test]
fn samples_part2() {
assert_eq!(1206, solve_part2(&input_generator(sample())));
}
}