-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
164 lines (131 loc) · 3.99 KB
/
day12.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
//! [Day 12: Subterranean Sustainability](https://adventofcode.com/2018/day/12)
use rustc_hash::FxHashSet;
struct Puzzle {
state: String,
rules: FxHashSet<Vec<char>>,
}
impl Puzzle {
fn new(data: &str) -> Self {
let mut state = String::new();
let mut rules = FxHashSet::default();
for line in data.lines() {
if let Some(s) = line.strip_prefix("initial state: ") {
state = s.to_string();
} else if let Some(from) = line.strip_suffix(" => #") {
rules.insert(from.chars().collect::<Vec<_>>());
}
}
Self { state, rules }
}
/// Solve part one.
fn part1(&self) -> i32 {
let mut state = self.state.chars().collect::<Vec<_>>();
let mut pots = 0;
for _ in 0..20 {
// left
while state[0..4] != ['.', '.', '.', '.'] {
pots -= 1;
state.insert(0, '.');
}
// right
while state[(state.len() - 4)..] != ['.', '.', '.', '.'] {
state.push('.');
}
let mut new_state = vec![];
pots += 2;
for c in state.windows(5) {
if self.rules.contains(c) {
new_state.push('#');
} else {
new_state.push('.');
}
}
state = new_state;
}
state
.iter()
.enumerate()
.filter_map(|(i, c)| {
if c == &'#' {
Some((i32::try_from(i).unwrap()) + pots)
} else {
None
}
})
.sum()
}
/// Solve part two.
fn part2(&self) -> i64 {
let mut state = self.state.chars().collect::<Vec<_>>();
let mut score = 0;
let mut pots = 0;
for seconds in 1..10000 {
// left
while state[0..4] != ['.', '.', '.', '.'] {
pots -= 1;
state.insert(0, '.');
}
// right
while state[(state.len() - 4)..] != ['.', '.', '.', '.'] {
state.push('.');
}
let mut new_state = vec![];
pots += 2;
for c in state.windows(5) {
if self.rules.contains(c) {
new_state.push('#');
} else {
new_state.push('.');
}
}
let new_score = new_state
.iter()
.enumerate()
.filter_map(|(i, c)| {
if c == &'#' {
Some((i64::try_from(i).unwrap()) + pots)
} else {
None
}
})
.sum();
let p1 = state.iter().position(|&c| c == '#').unwrap();
let p2 = state.iter().rposition(|&c| c == '#').unwrap();
let n1 = new_state.iter().position(|&c| c == '#').unwrap();
let n2 = new_state.iter().rposition(|&c| c == '#').unwrap();
if state[p1..p2] == new_state[n1..n2] {
// state is now stable
// extrapolate the value at 5e9 seconds
return new_score + (new_score - score) * (50_000_000_000 - seconds);
}
state = new_state;
score = new_score;
}
0
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (i32, i64) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test01() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part1(), 325);
}
#[test]
fn test02() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part2(), 999_999_999_374);
}
}