-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_18.rs
147 lines (134 loc) · 4.14 KB
/
day_18.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
//! https://adventofcode.com/2018/day/18
//! https://adventofcode.com/2018/day/18/input
use std::{fs::read_to_string, time::Instant};
#[derive(Copy, Clone, Debug, Hash, Eq, PartialOrd, PartialEq, Ord)]
enum Acre {
Ground,
Trees,
Lumberyard,
}
type Acres = Vec<Vec<Acre>>;
impl From<char> for Acre {
fn from(char: char) -> Self {
match char {
'.' => Self::Ground,
'|' => Self::Trees,
'#' => Self::Lumberyard,
_ => panic!("Invalid char: {}", char),
}
}
}
fn parse(input: &str) -> Acres {
input
.lines()
.map(|line| line.chars().map(Acre::from).collect())
.collect()
}
fn neighbors(i: usize, j: usize, size: usize) -> impl Iterator<Item = (usize, usize)> {
(i.saturating_sub(1)..=(i + 1).min(size - 1))
.flat_map(move |i| (j.saturating_sub(1)..=(j + 1).min(size - 1)).map(move |j| (i, j)))
.filter(move |neighbor| *neighbor != (i, j))
}
fn minute(acres: &mut Acres, support: &mut Acres) {
for i in 0..acres.len() {
for (j, acre) in acres[i].iter().enumerate() {
let (_, trees, lumberyards) =
neighbors(i, j, acres.len()).fold((0, 0, 0), |mut acc, (ni, nj)| {
match acres[ni][nj] {
Acre::Ground => acc.0 += 1,
Acre::Trees => acc.1 += 1,
Acre::Lumberyard => acc.2 += 1,
}
acc
});
support[i][j] = match acre {
Acre::Ground if trees >= 3 => Acre::Trees,
Acre::Trees if lumberyards >= 3 => Acre::Lumberyard,
Acre::Lumberyard if lumberyards * trees == 0 => Acre::Ground,
_ => *acre,
};
}
}
acres
.iter_mut()
.enumerate()
.for_each(|(i, line)| line.copy_from_slice(&support[i]));
}
pub mod part1 {
use super::{minute, parse, Acre};
pub fn solve(input: &str) -> usize {
let mut acres = parse(input);
let mut support = acres.clone();
for _ in 0..10 {
minute(&mut acres, &mut support);
}
let (trees, lumberyards) = acres.iter().flatten().fold((0, 0), |mut acc, acre| {
match acre {
Acre::Trees => acc.0 += 1,
Acre::Lumberyard => acc.1 += 1,
_ => {}
}
acc
});
trees * lumberyards
}
}
pub mod part2 {
use std::collections::{hash_map::Entry, HashMap};
use super::{minute, parse, Acre};
pub fn solve(input: &str) -> usize {
let mut acres = parse(input);
let mut support = acres.clone();
let mut states = HashMap::new();
let mut min = 0;
while min < 1000000000 {
match states.entry(acres.clone()) {
Entry::Occupied(entry) => {
let last = entry.get();
let interval = min - last;
let left = 1000000000 - min;
let cycles = left / interval;
min += cycles * interval;
}
Entry::Vacant(entry) => {
entry.insert(min);
}
}
minute(&mut acres, &mut support);
min += 1;
}
let (trees, lumberyards) = acres.iter().flatten().fold((0, 0), |mut acc, acre| {
match acre {
Acre::Trees => acc.0 += 1,
Acre::Lumberyard => acc.1 += 1,
_ => {}
}
acc
});
trees * lumberyards
}
}
pub fn main(test: bool) {
let test_input = ".#.#...|#.
.....#|##|
.|..|...#.
..|#.....#
#.#|||#|#|
...#.||...
.|....|...
||...#|.#|
|.||||..|.
...#.|..|."
.to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2018/day_18_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}