-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.rs
201 lines (167 loc) · 4.64 KB
/
day14.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
use ahash::HashMapExt;
use aoc_runner_derive::{aoc, aoc_generator};
use rustc_hash::FxHashMap as HashMap;
const TARGET: usize = 1_000_000_000;
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Dish {
grid: Vec<u8>,
width: usize,
height: usize,
}
impl std::fmt::Debug for Dish {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for row in self.grid.chunks(self.width) {
writeln!(f, "{}", String::from_utf8_lossy(row))?;
}
Ok(())
}
}
impl Dish {
fn roll(
&mut self,
x: impl IntoIterator<Item = usize>,
y: impl IntoIterator<Item = usize> + Clone,
f: impl Fn(usize, usize) -> usize,
offset: u8,
) {
let mut available_slots = [u8::MAX; 100];
for x in x {
for y in y.clone() {
let idx = f(x, y);
let slot = &mut available_slots[y];
match self.grid[idx] {
b'.' if *slot == u8::MAX => *slot = x as u8,
b'#' => *slot = u8::MAX,
b'O' if *slot != u8::MAX => {
self.grid[idx] = b'.';
self.grid[f(usize::from(*slot), y)] = b'O';
*slot = slot.wrapping_add(offset);
}
_ => {}
}
}
}
}
fn roll_north(&mut self) {
let w = self.width;
self.roll(0..self.height, 0..w, |r, c| r * w + c, 1);
}
fn roll_west(&mut self) {
let w = self.width;
self.roll(0..w, 0..self.height, |c, r| r * w + c, 1);
}
fn roll_south(&mut self) {
let w = self.width;
let h = self.height;
// 0u8.wrapping_sub(1) is -1
self.roll((0..h).rev(), 0..w, |r, c| r * w + c, 0u8.wrapping_sub(1));
}
fn roll_east(&mut self) {
let w = self.width;
let h = self.height;
self.roll((0..w).rev(), 0..h, |c, r| r * w + c, 0u8.wrapping_sub(1));
}
fn cycle(&mut self) {
self.roll_north();
self.roll_west();
self.roll_south();
self.roll_east();
}
fn load(&self) -> usize {
let mut sum = 0;
for row in 0..self.height {
let mult = self.height - row;
let row_offset = row * self.width;
for col in 0..self.width {
if self.grid[row_offset + col] == b'O' {
sum += mult;
}
}
}
sum
}
}
#[aoc_generator(day14)]
pub fn generator(input: &str) -> Dish {
let width = input.find('\n').unwrap();
let height = input.len() / width;
let mut grid = Vec::with_capacity(width * height);
for line in input.lines() {
grid.extend(line.bytes());
}
Dish {
grid,
width,
height,
}
}
#[aoc(day14, part1)]
pub fn part1(platform: &Dish) -> usize {
let mut platform = platform.clone();
platform.roll_north();
platform.load()
}
#[aoc(day14, part2)]
pub fn part2(platform: &Dish) -> usize {
let mut platform = platform.clone();
let mut seen = HashMap::with_capacity(256);
let mut t = 0;
while t < TARGET {
t += 1;
platform.cycle();
if let Some(old) = seen.get(&platform.grid) {
let cyc = t - old;
let amt = (TARGET - t) / cyc;
t += amt * cyc;
}
seen.insert(platform.grid.clone(), t);
}
platform.load()
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = r"O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....";
#[test]
pub fn input_test() {
let mut platform = generator(SAMPLE);
println!("{:?}", platform);
platform.roll_north();
println!("{:?}", platform);
// push_left(&mut platform.dish[9]);
// for i in 0..platform.dish.len() {
// push_left(&mut platform.dish[i]);
// }
// println!("{:?}", platform);
// assert_eq!(generator(SAMPLE), Object());
}
#[test]
pub fn part1_test() {
assert_eq!(part1(&generator(SAMPLE)), 136);
}
#[test]
pub fn part2_test() {
assert_eq!(part2(&generator(SAMPLE)), 64);
}
mod regression {
use super::*;
const INPUT: &str = include_str!("../input/2023/day14.txt");
const ANSWERS: (usize, usize) = (109385, 93102);
#[test]
pub fn test() {
let input = INPUT.trim_end_matches('\n');
let output = generator(input);
assert_eq!(part1(&output), ANSWERS.0);
assert_eq!(part2(&output), ANSWERS.1);
}
}
}