-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
231 lines (193 loc) · 5.19 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use anyhow::{Context, Result};
use crate::utils::AocError::*;
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Copy)]
pub enum Tile {
RoundRock,
CubeRock,
Empty,
}
impl Tile {
fn from_char(c: char) -> Result<Self> {
use Tile::*;
Ok(match c {
'O' => RoundRock,
'#' => CubeRock,
'.' => Empty,
_ => Err(GenericError).context("Unknown tile type")?,
})
}
}
#[aoc_generator(day14)]
pub fn input_generator(input: &str) -> Result<Vec<Vec<Tile>>> {
input
.lines()
.filter(|s| !s.is_empty())
.map(|l| {
l.chars()
.map(Tile::from_char)
.collect::<Result<Vec<_>>>()
})
.collect::<Result<Vec<_>>>()
.context("Error while parsing input")
}
fn tilt_north(dish: &[Vec<Tile>]) -> Vec<Vec<Tile>> {
let mut dish = dish.to_vec();
loop {
let mut has_moved = false;
for i in 1..dish.len() {
for j in 0..dish[i].len() {
let c = dish[i][j];
let above = dish[i - 1][j];
if c == Tile::RoundRock && above == Tile::Empty {
dish[i - 1][j] = Tile::RoundRock;
dish[i][j] = Tile::Empty;
has_moved = true;
}
}
}
if !has_moved {
break;
}
}
dish
}
fn eval(dish: &[Vec<Tile>]) -> usize {
let len = dish.len();
dish.iter()
.enumerate()
.map(|(i, l)| l.iter().filter(|r| **r == Tile::RoundRock).count() * (len - i))
.sum()
}
#[aoc(day14, part1)]
pub fn solve_part1(input: &[Vec<Tile>]) -> Result<usize> {
let tilted = tilt_north(input);
let result = eval(&tilted);
Ok(result)
}
fn cycle(dish: &[Vec<Tile>]) -> Vec<Vec<Tile>> {
let mut dish = tilt_north(dish);
// west
loop {
let mut has_moved = false;
for i in 0..dish.len() {
for j in 1..dish[i].len() {
let c = dish[i][j];
let left = dish[i][j - 1];
if c == Tile::RoundRock && left == Tile::Empty {
dish[i][j - 1] = Tile::RoundRock;
dish[i][j] = Tile::Empty;
has_moved = true;
}
}
}
if !has_moved {
break;
}
}
// south
loop {
let mut has_moved = false;
for i in (0..dish.len() - 1).rev() {
for j in 0..dish[i].len() {
let c = dish[i][j];
let below = dish[i + 1][j];
if c == Tile::RoundRock && below == Tile::Empty {
dish[i + 1][j] = Tile::RoundRock;
dish[i][j] = Tile::Empty;
has_moved = true;
}
}
}
if !has_moved {
break;
}
}
// east
loop {
let mut has_moved = false;
for i in 0..dish.len() {
for j in (0..dish[i].len() - 1).rev() {
let c = dish[i][j];
let right = dish[i][j + 1];
if c == Tile::RoundRock && right == Tile::Empty {
dish[i][j + 1] = Tile::RoundRock;
dish[i][j] = Tile::Empty;
has_moved = true;
}
}
}
if !has_moved {
break;
}
}
dish
}
fn matches(haystack: &[usize], offset: usize, cycle_size: usize) -> bool {
(0..cycle_size).all(|i| {
haystack[offset + i] == haystack[offset + cycle_size + i]
&& haystack[offset + i] == haystack[offset + 2 * cycle_size + i]
})
}
#[aoc(day14, part2)]
pub fn solve_part2(input: &[Vec<Tile>]) -> Result<usize> {
let reps = 250;
let mut evals = vec![];
(0..reps).fold(input.to_vec(), |acc, _| {
evals.push(eval(&acc));
cycle(&acc)
});
let mut offset = None;
let mut cycle_size = None;
// increase offset
'outer: for off in 0..150 {
for cyc in 3..25 {
if matches(&evals, off, cyc) {
offset = Some(off);
cycle_size = Some(cyc);
break 'outer;
}
}
}
match (offset, cycle_size) {
(Some(offset), Some(cycle_size)) => {
let rest = (1_000_000_000 - offset) % cycle_size;
let result = *evals
.iter()
.skip(offset)
.nth(rest)
.ok_or(GenericError)
.context("Could not find result")?;
Ok(result)
}
_ => Err(GenericError).context("Could not find cycle")?,
}
}
#[cfg(test)]
mod test {
use super::*;
fn sample() -> &'static str {
"O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#...."
}
fn input() -> Result<Vec<Vec<Tile>>> {
input_generator(sample())
}
#[test]
fn part1_sample() -> Result<()> {
let data = input()?;
Ok(assert_eq!(136, solve_part1(&data)?))
}
#[test]
fn part2_sample() -> Result<()> {
let data = input()?;
Ok(assert_eq!(64, solve_part2(&data)?))
}
}