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
|
use std::collections::{HashMap, HashSet};
static DAY: u8 = 12;
fn main() {
let input = advent::read_lines(DAY);
println!("{DAY}a: {}", fencing_price(&input));
println!("{DAY}b: {}", 0);
}
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
struct Position {
x: isize,
y: isize,
}
fn count_fences(region: &HashSet<Position>) -> usize {
let mut total_fences = 0;
for pos in region {
/* every plot has 4 fences, except it has neighbors of the same type */
let mut fences = 4;
for diff in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
let neighbor = Position { x: pos.x + diff.0, y: pos.y + diff.1 };
if region.contains(&neighbor) {
fences -= 1;
}
}
total_fences += fences;
}
total_fences
}
fn find_region(plots: &HashMap<Position, char>, pos: Position, visited: &mut HashSet<Position>) -> HashSet<Position> {
let mut region = HashSet::new();
if visited.contains(&pos) {
return region;
}
let plot_type = plots.get(&pos).unwrap();
region.insert(pos);
visited.insert(pos);
for diff in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
let neighbor = Position { x: pos.x + diff.0, y: pos.y + diff.1 };
if let Some(t) = plots.get(&neighbor) {
if t == plot_type {
for r in find_region(plots, neighbor, visited) {
region.insert(r);
}
}
}
}
region
}
fn find_regions(plots: &HashMap<Position, char>) -> HashMap<char, Vec<HashSet<Position>>> {
let mut regions = HashMap::new();
let mut visited = HashSet::new();
for (pos, c) in plots {
if visited.contains(pos) {
continue;
}
let region = find_region(plots, *pos, &mut HashSet::new());
for plot in ®ion {
visited.insert(*plot);
}
regions.entry(*c).or_insert(Vec::new()).push(region);
}
regions
}
fn fencing_price(input: &[String]) -> usize {
let mut plots = HashMap::new();
for (x, line) in input.iter().enumerate() {
for (y, c) in line.chars().enumerate() {
let pos = Position { x: x as isize, y: y as isize };
plots.insert(pos, c);
}
}
let all_regions = find_regions(&plots);
let mut total_price = 0;
for regions in all_regions.values() {
for region in regions {
let area = region.len();
let fences = count_fences(region);
total_price += area * fences;
}
}
total_price
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input = [
"AAAA",
"BBCD",
"BBCC",
"EEEC",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(fencing_price(&input), 140);
let input = [
"OOOOO",
"OXOXO",
"OOOOO",
"OXOXO",
"OOOOO",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(fencing_price(&input), 772);
let input = [
"RRRRIICCFF",
"RRRRIICCCF",
"VVRRRCCFFF",
"VVRCCCJFFF",
"VVVVCJJCFE",
"VVIVCCJJEE",
"VVIIICJJEE",
"MIIIIIJJEE",
"MIIISIJEEE",
"MMMISSJEEE",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(fencing_price(&input), 1930);
}
}
|