-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_17.rs
213 lines (184 loc) Β· 5.07 KB
/
day_17.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
use hashbrown::{hash_map::Entry, HashMap};
use common::{Answer, Solution};
use nd_vec::vector;
type Point = nd_vec::Vec2<i64>;
pub struct Day17;
impl Solution for Day17 {
fn name(&self) -> &'static str {
"Pyroclastic Flow"
}
fn part_a(&self, input: &str) -> Answer {
let mut world = World::new(input);
process(&mut world, 2022).into()
}
fn part_b(&self, input: &str) -> Answer {
let mut world = World::new(input);
process(&mut world, 1000000000000).into()
}
}
const WIDTH: usize = 7;
#[rustfmt::skip]
const ROCKS: [&[Point]; 5] = [
&[
vector!(0, 0),
vector!(1, 0),
vector!(2, 0),
vector!(3, 0),
],
&[
vector!(1, 0),
vector!(0, 1),
vector!(1, 1),
vector!(2, 1),
vector!(1, 2),
],
&[
vector!(2, 2),
vector!(2, 1),
vector!(0, 0),
vector!(1, 0),
vector!(2, 0),
],
&[
vector!(0, 0),
vector!(0, 1),
vector!(0, 2),
vector!(0, 3),
],
&[
vector!(0, 0),
vector!(1, 0),
vector!(0, 1),
vector!(1, 1),
],
];
fn process(world: &mut World, rocks: i64) -> i64 {
let mut seen = HashMap::new();
let mut cycle_height = 0;
let mut i = 0;
while i < rocks {
world.add_rock();
i += 1;
if world.max_height < 8 {
continue;
}
let state = world.state();
match seen.entry(state) {
Entry::Vacant(e) => {
e.insert((i, world.max_height));
}
Entry::Occupied(e) => {
let (old_i, old_height) = e.get();
let num_rocks_in_cycle = i - old_i;
let num_cycles = (rocks - i) / num_rocks_in_cycle;
i += num_rocks_in_cycle * num_cycles;
cycle_height += num_cycles * (world.max_height - old_height);
seen.clear();
}
}
}
world.max_height + cycle_height
}
#[derive(Debug, Clone, Copy)]
struct Rock {
rock_id: usize,
pos: Point,
}
#[derive(Debug, Clone)]
struct World {
rocks: Vec<Rock>,
max_height: i64,
airflow: Vec<char>,
flow_index: usize,
}
impl World {
fn new(raw: &str) -> Self {
Self {
rocks: Vec::new(),
max_height: 0,
airflow: raw.chars().collect(),
flow_index: 0,
}
}
fn state(&self) -> (Vec<Vec<bool>>, usize, usize) {
let mut world = vec![[false; WIDTH]; self.max_height as usize + 1];
for rock in &self.rocks {
for point in rock.points() {
world[point.y() as usize][point.x() as usize] = true;
}
}
let mut rows = Vec::new();
for row in world.into_iter().rev().take(8) {
rows.push(row.into_iter().collect::<Vec<_>>());
}
(
rows,
self.flow_index,
self.rocks.last().map(|x| x.rock_id).unwrap_or(0),
)
}
fn add_rock(&mut self) {
let next_rock = self.rocks.last().map(|x| x.rock_id + 1).unwrap_or(0) % ROCKS.len();
self.rocks.push(Rock {
rock_id: next_rock,
pos: vector!(2, self.max_height + 3),
});
let rock_index = self.rocks.len() - 1;
loop {
let next_flow = self.next_flow();
self.try_move_rock(rock_index, next_flow);
let gravity = self.try_move_rock(rock_index, vector!(0, -1));
if !gravity {
break;
}
}
self.max_height = self.max_height.max(
self.rocks[rock_index]
.points()
.iter()
.map(|x| x.y())
.max()
.unwrap()
+ 1,
);
}
// Returns true if rock was moved.
fn try_move_rock(&mut self, rock_index: usize, delta: Point) -> bool {
let mut rock = self.rocks[rock_index];
rock.pos += delta;
// Check if rock in new position is valid, meaning it doesn't collide with any other rock or the wall.
for point in ROCKS[rock.rock_id] {
let new_pos = rock.pos + *point;
if new_pos.x() < 0 || new_pos.x() >= WIDTH as i64 || new_pos.y() < 0 {
return false;
}
if self
.rocks
.iter()
.take(rock_index)
.any(|r| r.points().iter().any(|x| *x == new_pos))
{
return false;
}
}
self.rocks[rock_index] = rock;
true
}
fn next_flow(&mut self) -> Point {
let flow = self.airflow[self.flow_index];
self.flow_index = (self.flow_index + 1) % self.airflow.len();
match flow {
'>' => vector!(1, 0),
'<' => vector!(-1, 0),
_ => panic!("Invalid flow: {}", flow),
}
}
}
impl Rock {
fn points(&self) -> Vec<Point> {
ROCKS[self.rock_id]
.iter()
.map(|point| self.pos + *point)
.collect()
}
}