-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_5.rs
293 lines (230 loc) · 8.01 KB
/
day_5.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
290
291
292
293
// --- Day 5: Supply Stacks ---
// The expedition can depart as soon as the final supplies have been unloaded
// from the ships. Supplies are stored in stacks of marked crates, but because
// the needed supplies are buried under many other crates, the crates need to be
// rearranged.
// The ship has a giant cargo crane capable of moving crates between stacks. To
// ensure none of the crates get crushed or fall over, the crane operator will
// rearrange them in a series of carefully-planned steps. After the crates are
// rearranged, the desired crates will be at the top of each stack.
// The Elves don't want to interrupt the crane operator during this delicate
// procedure, but they forgot to ask her which crate will end up where, and they
// want to be ready to unload them as soon as possible so they can embark.
// They do, however, have a drawing of the starting stacks of crates and the
// rearrangement procedure (your puzzle input). For example:
// [D]
// [N] [C]
// [Z] [M] [P]
// 1 2 3
// move 1 from 2 to 1
// move 3 from 1 to 3
// move 2 from 2 to 1
// move 1 from 1 to 2
// In this example, there are three stacks of crates. Stack 1 contains two
// crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains
// three crates; from bottom to top, they are crates M, C, and D. Finally, stack
// 3 contains a single crate, P.
// Then, the rearrangement procedure is given. In each step of the procedure, a
// quantity of crates is moved from one stack to a different stack. In the first
// step of the above rearrangement procedure, one crate is moved from stack 2 to
// stack 1, resulting in this configuration:
// [D]
// [N] [C]
// [Z] [M] [P]
// 1 2 3
// In the second step, three crates are moved from stack 1 to stack 3. Crates
// are moved one at a time, so the first crate to be moved (D) ends up below the
// second and third crates:
// [Z]
// [N]
// [C] [D]
// [M] [P]
// 1 2 3
// Then, both crates are moved from stack 2 to stack 1. Again, because crates
// are moved one at a time, crate C ends up below crate M:
// [Z]
// [N]
// [M] [D]
// [C] [P]
// 1 2 3
// Finally, one crate is moved from stack 1 to stack 2:
// [Z]
// [N]
// [D]
// [C] [M] [P]
// 1 2 3
// The Elves just need to know which crate will end up on top of each stack; in
// this example, the top crates are C in stack 1, M in stack 2, and Z in stack
// 3, so you should combine these together and give the Elves the message CMZ.
// After the rearrangement procedure completes, what crate ends up on top of
// each stack?
// --- Part Two ---
// As you watch the crane operator expertly rearrange the crates, you notice the
// process isn't following your prediction.
// Some mud was covering the writing on the side of the crane, and you quickly
// wipe it away. The crane isn't a CrateMover 9000 - it's a CrateMover 9001.
// The CrateMover 9001 is notable for many new and exciting features: air
// conditioning, leather seats, an extra cup holder, and the ability to pick up
// and move multiple crates at once.
// Again considering the example above, the crates begin in the same
// configuration:
// [D]
// [N] [C]
// [Z] [M] [P]
// 1 2 3
// Moving a single crate from stack 2 to stack 1 behaves the same as before:
// [D]
// [N] [C]
// [Z] [M] [P]
// 1 2 3
// However, the action of moving three crates from stack 1 to stack 3 means that
// those three moved crates stay in the same order, resulting in this new
// configuration:
// [D]
// [N]
// [C] [Z]
// [M] [P]
// 1 2 3
// Next, as both crates are moved from stack 2 to stack 1, they retain their
// order as well:
// [D]
// [N]
// [C] [Z]
// [M] [P]
// 1 2 3
// Finally, a single crate is still moved from stack 1 to stack 2, but now it's
// crate C that gets moved:
// [D]
// [N]
// [Z]
// [M] [C] [P]
// 1 2 3
// In this example, the CrateMover 9001 has put the crates in a totally
// different order: MCD.
// Before the rearrangement process finishes, update your simulation so that the
// Elves know where they should stand to be ready to unload the final supplies.
// After the rearrangement procedure completes, what crate ends up on top of
// each stack?
use std::collections::HashMap;
struct State {
stacks: HashMap<usize, Vec<char>>,
lookups: HashMap<char, usize>,
}
impl State {
pub fn parse<'a, I: Iterator<Item = &'a str> + Sized>(input: &mut I) -> State {
let mut stacks: HashMap<usize, Vec<char>> = HashMap::new();
let mut revlabels: HashMap<char, usize> = HashMap::new();
for line in input.by_ref() {
if line.is_empty() {
continue;
}
let mut opened = None;
let mut parsed_number = false;
let mut label = ' ';
for (idx, char) in line.chars().enumerate() {
match char {
'[' => opened = Some(idx + 1),
']' if opened.is_some() => {
stacks.entry(opened.unwrap()).or_default().push(label);
opened = None;
}
_ if opened.is_some() => label = char,
a if a.is_numeric() => {
revlabels.insert(a, idx);
parsed_number = true;
}
_ => continue,
}
}
if parsed_number {
break;
}
}
stacks.values_mut().for_each(|v| v.reverse());
State {
stacks,
lookups: revlabels,
}
}
fn get(&mut self, name: char) -> &'_ mut Vec<char> {
self.stacks.get_mut(&self.lookups[&name]).unwrap()
}
fn top(&self) -> String {
let mut keys = self.lookups.keys().collect::<Vec<_>>();
keys.sort();
keys.into_iter()
.map(|label| *self.stacks[&self.lookups[label]].last().unwrap())
.collect()
}
}
fn parse_cmd(line: &str) -> (usize, char, char) {
let mut cmd_str_iter = line.split_ascii_whitespace();
assert_eq!(cmd_str_iter.next().unwrap(), "move");
let num = cmd_str_iter.next().unwrap().parse::<usize>().unwrap();
assert_eq!(cmd_str_iter.next().unwrap(), "from");
let from = cmd_str_iter.next().unwrap().chars().next().unwrap();
assert_eq!(cmd_str_iter.next().unwrap(), "to");
let to = cmd_str_iter.next().unwrap().chars().next().unwrap();
(num, from, to)
}
pub fn part_1(input: &str) -> String {
let mut iter = input.lines();
let mut s = State::parse(&mut iter);
for line in iter {
if line.is_empty() {
continue;
}
let (num, from, to) = parse_cmd(line);
for _ in 0..num {
let v = s.get(from).pop().unwrap();
s.get(to).push(v);
}
}
s.top()
}
pub fn part_2(input: &str) -> String {
let mut iter = input.lines();
let mut s = State::parse(&mut iter);
for line in iter {
if line.is_empty() {
continue;
}
let (num, from, to) = parse_cmd(line);
let mut to_xfer = vec![];
for _ in 0..num {
to_xfer.push(s.get(from).pop().unwrap());
}
for v in to_xfer.into_iter().rev() {
s.get(to).push(v);
}
}
s.top()
}
#[cfg(test)]
pub mod tests {
use crate::day_5::{part_1, part_2};
const INPUTS: &str = r#" [D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2"#;
#[test]
pub fn test_day_5_example_part1() {
assert_eq!(part_1(INPUTS), "CMZ");
}
#[test]
pub fn test_day_5_part1() {
assert_eq!(part_1(include_str!("input/day_5.txt")), "MQSHJMWNH");
}
#[test]
pub fn test_day_5_example_part2() {
assert_eq!(part_2(INPUTS), "MCD");
}
#[test]
pub fn test_day_5_part2() {
assert_eq!(part_2(include_str!("input/day_5.txt")), "LLWJRBHVZ");
}
}