This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.rs
289 lines (237 loc) · 8.73 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
advent_of_code::solution!(14);
use std::{
collections::BTreeMap,
hash::{DefaultHasher, Hash, Hasher},
};
use advent_of_code::tools::{Coords, UCoords};
/* == Definitions == */
const SPIN_CYCLES: usize = 1_000_000_000;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Occupation {
Empty,
Fixed,
Rolling,
}
enum Direction {
East,
North,
South,
West,
}
struct Platform {
occupation: Vec<Occupation>,
size: UCoords,
}
/* == Solutions == */
pub fn part_one(input: &str) -> Option<u64> {
let mut platform = Platform::parse_str(input);
let load = platform.tilt_platform(Direction::North);
Some(load)
}
pub fn part_two(input: &str) -> Option<u64> {
let mut platform = Platform::parse_str(input);
let mut hashes = BTreeMap::new();
let mut load = 0;
// Keep spinning the cycle and saving the load
for i in 0..SPIN_CYCLES {
load = platform.spin_platform();
let hash = platform.hash();
// Thankfully, a cycle is always found before the end...
// match history.iter().enumerate().find(|(_, (h, _))| *h == hash) {
match hashes.get(&hash) {
None => {
hashes.insert(hash, (i, platform.north_beam_load()));
}
Some((j, _)) => {
// Compute the expected load after SPIN_CYCLES by exploiting
// the cyclic nature and the saved loads after each cycle.
let index = j - 1 + (SPIN_CYCLES - i) % (i - j);
load = *hashes
.values()
.find(|(i, _)| *i == index)
.map(|(_, l)| l)
.unwrap();
break;
}
}
}
Some(load)
}
/* == Implementations == */
impl Platform {
fn parse_str(input: &str) -> Platform {
let mut occupation = Vec::new();
let mut height = 0;
for line in input.lines() {
occupation.extend(line.bytes().map(Occupation::from));
height += 1;
}
let width = occupation.len() / height;
Platform {
occupation,
size: UCoords::new(width, height),
}
}
/// Tilts the platform North, West, South and East, returning the
/// final load north beam load.
fn spin_platform(&mut self) -> u64 {
self.tilt_platform(Direction::North);
self.tilt_platform(Direction::West);
self.tilt_platform(Direction::South);
self.tilt_platform(Direction::East)
}
/// Tilts the platform in the given direction, returning the load
/// of the north beam.
fn tilt_platform(&mut self, direction: Direction) -> u64 {
// Not really sure how to do this with less code, without macros
// or dynamic dispatching/allocation. This works fine.
match direction {
Direction::East => (0..self.size.y)
.map(|y| {
let minimum = UCoords::new(self.size.x - 1, y).into();
let gradient = Coords::new(-1, 0);
self.process_line(minimum, gradient)
})
.sum(),
Direction::North => (0..self.size.x)
.map(|x| {
let minimum = UCoords::new(x, 0).into();
let gradient = Coords::new(0, 1);
self.process_line(minimum, gradient)
})
.sum(),
Direction::South => (0..self.size.x)
.map(|x| {
let minimum = UCoords::new(x, self.size.y - 1).into();
let gradient = Coords::new(0, -1);
self.process_line(minimum, gradient)
})
.sum(),
Direction::West => (0..self.size.y)
.map(|y| {
let minimum = UCoords::new(0, y).into();
let gradient = Coords::new(1, 0);
self.process_line(minimum, gradient)
})
.sum(),
}
}
/// Shifts all rolling stones in a single line of the platform.
/// The minimum should be the coordinates at the bottom of the slope,
/// with the gradient being a unit vector pointing up the slope.
///
/// This algorithm works by iterating over each free spot, peeking ahead
/// to find the next rolling stone and swapping them. Encountering fixed
/// objects during the peek will jump the search to the spot after it.
///
/// Efficiently collects and returns the final load of the line, **computed
/// in the direction of the gradient** (not always the north beam!).
fn process_line(&mut self, minimum: Coords, gradient: Coords) -> u64 {
let mut at = minimum;
// Determine the distance based on platform size (should be square anyway...)
let mut distance = match gradient {
Coords { x: 0, y: _ } => self.size.y,
Coords { x: _, y: 0 } => self.size.x,
_ => unreachable!(),
} as u64;
// Collect the load during the pass
let mut load = 0;
'outer: loop {
// Check the current position
match at.ucoords(&self.size).and_then(|x| Some((x, self.get(x)?))) {
Some((at_ucoords, Occupation::Empty)) => {
// If it's empty, search for the nearest rolling stone
let mut cursor = at + gradient;
loop {
match cursor
.ucoords(&self.size)
.and_then(|x| Some((x, self.get(x)?)))
{
// Rolling stone found, roll it to current position
Some((cursor_ucoords, Occupation::Rolling)) => {
self.set(cursor_ucoords, Occupation::Empty);
self.set(at_ucoords, Occupation::Rolling);
load += distance;
break;
}
// Next object is static, move search to after it
Some((_, Occupation::Fixed)) => {
distance -= (cursor - at).norm_l1();
at = cursor;
break;
}
// Keep going...
Some((_, Occupation::Empty)) => (),
// Cursor left the map, we're done here
None => break 'outer,
}
cursor += gradient;
}
}
// We're on a rolling stone, add it to the load
Some((_, Occupation::Rolling)) => load += distance,
// Immovable object
Some((_, Occupation::Fixed)) => (),
// Reached the end, all done
None => break 'outer,
}
distance -= 1;
at += gradient;
}
load
}
/// Computes the load of the north beam, by multiplying each rolling stone by
/// the distance to south end of the platform.
fn north_beam_load(&self) -> u64 {
(0..self.size.y)
.map(|y| {
let distance = self.size.y - y;
distance
* self.occupation[y * self.size.x..(y + 1) * self.size.x]
.iter()
.filter(|&&x| x == Occupation::Rolling)
.count()
})
.sum::<usize>() as u64
}
fn get(&self, coords: UCoords) -> Option<Occupation> {
let index = coords.y * self.size.x + coords.x;
self.occupation.get(index).copied()
}
fn set(&mut self, coords: UCoords, value: Occupation) {
let index = coords.y * self.size.x + coords.x;
self.occupation[index] = value;
}
/// Computes a hash of the platform's current state using the default algorithm.
fn hash(&self) -> u64 {
let mut hasher = DefaultHasher::default();
self.occupation.hash(&mut hasher);
hasher.finish()
}
}
impl From<u8> for Occupation {
fn from(value: u8) -> Self {
match value {
b'.' => Occupation::Empty,
b'#' => Occupation::Fixed,
b'O' => Occupation::Rolling,
_ => unreachable!(),
}
}
}
/* == Tests == */
#[cfg(test)]
mod tests {
use super::*;
use advent_of_code::template::*;
#[test]
fn test_part_one() {
let result = part_one(&read_example(DAY));
assert_eq!(result, Some(136));
}
#[test]
fn test_part_two() {
let result = part_two(&read_example(DAY));
assert_eq!(result, Some(64));
}
}