-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.rs
110 lines (96 loc) · 2.57 KB
/
05.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
/*
* Day 5: Supply Stacks
* See [https://adventofcode.com/2022/day/5]
*/
use std::str::SplitAsciiWhitespace;
fn make_vecs(slen: usize) -> (usize, Box<[Vec<u8>]>) {
let lx = (1 + slen) / 4;
let v: Box<[Vec<u8>]> = vec![Vec::new(); lx].into_boxed_slice();
(lx, v)
}
fn fill_line(ls: usize, bl: &[u8], v: &mut Box<[Vec<u8>]>) {
let mut i = 1;
let mut j = 0;
loop {
let c = bl[i];
if c >= b'A' {
v[j].push(c);
}
j += 1;
i += 4;
if j >= ls {
break;
}
}
}
fn parse_opt_usize(it: &mut SplitAsciiWhitespace) -> usize {
it.next();
it.next().unwrap().parse().unwrap()
}
fn read_moves(s: &str) -> (usize, usize, usize) {
let it = &mut s.split_ascii_whitespace();
let c = parse_opt_usize(it);
let f = parse_opt_usize(it) - 1;
let t = parse_opt_usize(it) - 1;
(c, f, t)
}
pub fn part_1(input: &str) -> Option<String> {
let mut lines = input.lines().peekable();
let (lx, mut v) = make_vecs(lines.peek().unwrap().len());
let mut mode = 0;
for l in lines {
if l.is_empty() {
mode = 1;
v.iter_mut().for_each(|x| x.reverse());
continue;
}
let bl = l.as_bytes();
if mode == 0 {
fill_line(lx, bl, &mut v);
}
if mode == 1 {
let (c, f, t) = read_moves(l);
for _ in 0..c {
if let Some(u) = v[f].pop() {
v[t].push(u);
}
}
}
}
let s: String = v.iter().map(|x| *x.last().unwrap() as char).collect();
Some(s)
}
pub fn part_2(input: &str) -> Option<String> {
let mut lines = input.lines().peekable();
let mut mode = 0;
let (lx, mut v) = make_vecs(lines.peek().unwrap().len());
for l in lines {
if l.is_empty() {
mode = 1;
v.iter_mut().for_each(|x| x.reverse());
continue;
}
let bl = l.as_bytes();
if mode == 0 {
fill_line(lx, bl, &mut v);
}
if mode == 1 {
let (c, f, t) = read_moves(l);
let vf = &mut v[f];
let vv = vf.split_off( vf.len() - c);
v[t].extend_from_slice(vv.as_slice());
}
}
let s: String = v.iter().map(|x| *x.last().unwrap() as char).collect();
Some(s)
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use aoc2022::assert_ex;
use super::*;
#[test]
fn test_part_1() { assert_ex!(part_1, "CMZ"); }
#[test]
fn test_part_2() { assert_ex!(part_2, "MCD"); }
}