generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.rs
265 lines (244 loc) · 7.61 KB
/
14.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::{
collections::{hash_map::Entry, HashMap},
fmt::{Display, Write},
};
advent_of_code::solution!(14);
pub fn part_one(input: &str) -> Option<u32> {
let platform = input
.lines()
.map(|line| line.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut score = 0;
for col in 0..platform[0].len() {
let mut empty_spaces = 0;
for row in 0..platform.len() {
match platform[row][col] {
'.' => empty_spaces += 1,
'#' => empty_spaces = 0,
'O' => {
let new_row = row - empty_spaces;
score += platform.len() - new_row;
}
_ => unreachable!(),
}
}
}
Some(score as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut platform = Platform::parse(input);
let mut count = 0u64;
let mut seen = HashMap::new();
loop {
let key = platform.cycle();
count += 1;
match seen.entry(key) {
Entry::Occupied(val) => {
let cycle_length = count - *val.get();
let remainder = 1_000_000_000
- (((1_000_000_000 - count) as f64 / cycle_length as f64).floor() as u64
* cycle_length
+ count);
for _ in 0..remainder {
platform.cycle();
}
break;
}
Entry::Vacant(e) => {
e.insert(count);
}
}
}
Some(platform.load())
}
#[derive(Debug, Default, Clone)]
struct Platform {
width: u8,
height: u8,
round_rocks: Vec<Coordinate>,
distance_to_cubed_rocks: Vec<Vec<CachedDistance>>,
stacks: Vec<Vec<u8>>,
iteration: u16,
}
impl Platform {
pub fn parse(input: &str) -> Self {
let grid = input
.lines()
.map(|line| line.chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let width = grid[0].len() as u8;
let height = grid.len() as u8;
let mut round_rocks = Vec::new();
let mut distance_to_cubed_rocks =
vec![vec![CachedDistance::default(); grid[0].len()]; grid.len()];
for row in 0..width as usize {
for col in 0..height as usize {
if grid[row][col] == 'O' {
round_rocks.push(Coordinate::new(row as u8, col as u8));
}
let north = (0..)
.find(|&d| d > row || grid[row - d][col] == '#')
.unwrap() as u8;
let south = (0..)
.find(|&d| row + d >= height as usize || grid[row + d][col] == '#')
.unwrap() as u8;
let west = (0..)
.find(|&d| d > col || grid[row][col - d] == '#')
.unwrap() as u8;
let east = (0..)
.find(|&d| col + d >= width as usize || grid[row][col + d] == '#')
.unwrap() as u8;
distance_to_cubed_rocks[row][col] = CachedDistance {
north,
south,
west,
east,
};
}
}
Self {
round_rocks,
distance_to_cubed_rocks,
iteration: 0,
width,
height,
stacks: vec![vec![0; width as usize]; height as usize],
}
}
pub fn cycle(&mut self) -> u128 {
let mut key = 0;
self.tilt(Direction::North);
key += (self.load() as u128) << 96;
self.tilt(Direction::West);
key += (self.load() as u128) << 64;
self.tilt(Direction::South);
key += (self.load() as u128) << 32;
self.tilt(Direction::East);
key += self.load() as u128;
key
}
fn tilt(&mut self, dir: Direction) {
self.iteration += 1;
for s in &mut self.stacks {
s.fill(0);
}
for rock in &mut self.round_rocks {
let distance =
self.distance_to_cubed_rocks[rock.row as usize][rock.col as usize].get(dir);
let cubed_rock = rock
.move_in_dir(dir, distance)
.limit_to(self.height - 1, self.width - 1);
let stack = &mut self.stacks[cubed_rock.row as usize][cubed_rock.col as usize];
if *stack > distance - 1 {
let total_move_distance = *stack - distance + 1;
*rock = rock.move_in_dir(dir.rev(), total_move_distance);
} else {
let total_move_distance = distance - 1 - *stack;
*rock = rock.move_in_dir(dir, total_move_distance);
}
*stack += 1;
}
}
fn load(&self) -> u32 {
self.round_rocks
.iter()
.map(|&r| (self.height - r.row) as u32)
.sum()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Direction {
North,
West,
South,
East,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
struct Coordinate {
row: u8,
col: u8,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
struct CachedDistance {
north: u8,
south: u8,
west: u8,
east: u8,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
struct RockStack {
iteration: u16,
count: u8,
}
impl Coordinate {
const fn new(row: u8, col: u8) -> Self {
Self { row, col }
}
const fn move_in_dir(self, dir: Direction, distance: u8) -> Self {
match dir {
Direction::North => Self::new(self.row.saturating_sub(distance), self.col),
Direction::South => Self::new(self.row + distance, self.col),
Direction::West => Self::new(self.row, self.col.saturating_sub(distance)),
Direction::East => Self::new(self.row, self.col + distance),
}
}
fn limit_to(self, max_row: u8, max_col: u8) -> Self {
Self::new(self.row.min(max_row), self.col.min(max_col))
}
}
impl CachedDistance {
const fn get(self, dir: Direction) -> u8 {
match dir {
Direction::North => self.north,
Direction::West => self.west,
Direction::South => self.south,
Direction::East => self.east,
}
}
}
impl Direction {
const fn rev(self) -> Self {
match self {
Self::North => Self::South,
Self::West => Self::East,
Self::South => Self::North,
Self::East => Self::West,
}
}
}
impl Display for Platform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut grid = vec![vec!['.'; self.width as usize]; self.height as usize];
for row in 0..self.height {
for col in 0..self.width {
if self.distance_to_cubed_rocks[row as usize][col as usize].north == 0 {
grid[row as usize][col as usize] = '#';
}
}
}
for rock in &self.round_rocks {
grid[rock.row as usize][rock.col as usize] = 'O';
}
for row in grid {
for ch in row {
f.write_char(ch)?;
}
f.write_char('\n')?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(136));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(64));
}
}