-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.rs
60 lines (55 loc) · 1.46 KB
/
24.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
#![feature(test)]
type Input = Vec<(i8, i8)>;
fn setup(input: &str) -> Input {
let lines: Vec<String> = input.lines().map(|line| line.to_string()).collect();
(0..14)
.map(|i| {
let get = |j: usize| {
lines[i * 18 + j]
.split(' ')
.nth(2)
.unwrap()
.parse()
.unwrap()
};
(get(5), get(15))
})
.collect()
}
fn part1(input: &Input) -> String {
let mut out = [9u8; 14];
let mut stack = vec![];
for (i, (mut x, y)) in input.iter().cloned().enumerate() {
if x < 0 {
let (j, y) = stack.pop().unwrap();
x += y;
if x < 0 {
out[i] -= -x as u8;
} else {
out[j] -= x as u8;
}
} else {
stack.push((i, y));
}
}
out.iter().map(|&x| (x + 0x30) as char).collect()
}
fn part2(input: &Input) -> String {
let mut out = [1u8; 14];
let mut stack = vec![];
for (i, (mut x, y)) in input.iter().cloned().enumerate() {
if x < 0 {
let (j, y) = stack.pop().unwrap();
x += y;
if x < 0 {
out[j] += -x as u8;
} else {
out[i] += x as u8;
}
} else {
stack.push((i, y));
}
}
out.iter().map(|&x| (x + 0x30) as char).collect()
}
aoc::main!(2021, 24);