-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.rs
195 lines (173 loc) · 4.6 KB
/
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
#![feature(test)]
use rustc_hash::{FxHashMap, FxHashSet};
const SHAPES: &[Shape] = &[
Shape {
width: 4,
height: 1,
rock: &[(0, 0), (0, 1), (0, 2), (0, 3)],
},
Shape {
width: 3,
height: 3,
rock: &[(0, 1), (1, 0), (1, 1), (1, 2), (2, 1)],
},
Shape {
width: 3,
height: 3,
rock: &[(0, 2), (1, 2), (2, 0), (2, 1), (2, 2)],
},
Shape {
width: 1,
height: 4,
rock: &[(0, 0), (1, 0), (2, 0), (3, 0)],
},
Shape {
width: 2,
height: 2,
rock: &[(0, 0), (0, 1), (1, 0), (1, 1)],
},
];
const WIDTH: u8 = 7;
const START_X: u8 = 2;
const START_Y: u64 = 3;
type Input = Vec<Direction>;
#[derive(Clone, Copy)]
enum Direction {
Left,
Right,
}
struct Shape<'a> {
width: u8,
height: u64,
rock: &'a [(u64, u8)],
}
fn setup(input: &str) -> Input {
input
.trim()
.bytes()
.map(|c| match c {
b'<' => Direction::Left,
b'>' => Direction::Right,
_ => panic!(),
})
.collect()
}
struct Simulation<'a> {
input: &'a Input,
rock: FxHashSet<(u64, u8)>,
height: u64,
jet: usize,
shape: usize,
}
impl<'a> Simulation<'a> {
fn new(input: &'a Input) -> Self {
Self {
input,
rock: FxHashSet::with_capacity_and_hasher(16384, Default::default()),
height: 0,
jet: 0,
shape: 0,
}
}
fn next_jet(&mut self) -> Direction {
let out = self.input[self.jet];
self.jet = (self.jet + 1) % self.input.len();
out
}
fn next_shape(&mut self) -> &'static Shape<'static> {
let out = &SHAPES[self.shape];
self.shape = (self.shape + 1) % SHAPES.len();
out
}
fn test(&self, shape: &Shape, x: u8, y: u64) -> bool {
y + 1 >= shape.height
&& x + shape.width <= WIDTH
&& shape
.rock
.iter()
.all(|&(i, j)| !self.rock.contains(&(y - i, x + j)))
}
fn add(&mut self, shape: &Shape, x: u8, y: u64) {
for &(i, j) in shape.rock {
self.rock.insert((y - i, x + j));
}
}
}
struct Step {
height: u64,
jet: usize,
shape: usize,
}
impl PartialEq for Step {
fn eq(&self, other: &Self) -> bool {
(self.jet, self.shape) == (other.jet, other.shape)
}
}
impl Iterator for Simulation<'_> {
type Item = Step;
fn next(&mut self) -> Option<Self::Item> {
let out = Some(Step {
height: self.height,
jet: self.jet,
shape: self.shape,
});
let shape = self.next_shape();
let mut x = START_X;
let mut y = self.height + START_Y + shape.height - 1;
loop {
if let Some(dx) = match self.next_jet() {
Direction::Left => x.checked_sub(1),
Direction::Right => Some(x + 1),
}
.and_then(|dx| self.test(shape, dx, y).then_some(dx))
{
x = dx;
}
if y > 0 && self.test(shape, x, y - 1) {
y -= 1;
} else {
self.add(shape, x, y);
self.height = self.height.max(y + 1);
break;
}
}
out
}
}
fn part1(input: &Input) -> u64 {
Simulation::new(input).nth(2022).unwrap().height
}
fn part2(input: &Input) -> u64 {
let mut steps = Vec::with_capacity(4096);
let mut idx = FxHashMap::with_capacity_and_hasher(4096, Default::default());
Simulation::new(input)
.enumerate()
.find_map(
|(
round,
step @ Step {
mut height,
jet,
shape,
},
)| {
steps.push(step);
if let Some(i) = idx.get(&(jet, shape)) {
let n = steps.len();
let l = n - i - 1;
if n >= l * 2 && steps[n - l..] == steps[n - 2 * l..n - l] {
let left = 1000000000000 - round;
let s: &Step = &steps[n - l - 1];
let t: &Step = &steps[n - l - 1 + left % l];
height += (left / l) as u64 * (steps[n - 1].height - s.height);
height += t.height - s.height;
return Some(height);
}
}
idx.insert((jet, shape), round);
None
},
)
.unwrap()
}
aoc::main!(2022, 17, ex: 1);