blob: 714e67cc1e26a2eb3151925db204ad87c2d2b464 (
plain) (
blame)
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
|
fn main() {
part1();
part2();
}
fn part1() {
let input = include_str!("../input");
let mut split = input.split("\n\n");
let start_state = split.next().unwrap();
let mut state: Vec<Vec<char>> = vec![vec![]; 9];
for line in start_state.lines() {
let mut index = 1;
let mut i = 0;
while index < line.len() {
let c = line.chars().nth(index).unwrap();
if c.is_ascii_uppercase() {
state[i].push(c);
}
index += 4;
i += 1;
}
}
for s in state.iter_mut() {
s.reverse();
}
println!("{:?}", state);
let instructions: Vec<Vec<usize>> = split
.next()
.unwrap()
.lines()
.map(|line| {
line.split(" ")
.flat_map(|w| w.parse::<usize>())
.collect::<Vec<usize>>()
})
.collect();
for instr in instructions {
let amt = instr[0];
let src = instr[1];
let dst = instr[2];
for _ in 0..amt {
let c = state[src - 1].pop().unwrap();
state[dst - 1].push(c);
}
}
for s in state {
print!("{}", s.last().unwrap());
}
println!();
}
fn part2() {
let input = include_str!("../input");
let mut split = input.split("\n\n");
let start_state = split.next().unwrap();
let mut state: Vec<Vec<char>> = vec![vec![]; 9];
for line in start_state.lines() {
let mut index = 1;
let mut i = 0;
while index < line.len() {
let c = line.chars().nth(index).unwrap();
if c.is_ascii_uppercase() {
state[i].push(c);
}
index += 4;
i += 1;
}
}
for s in state.iter_mut() {
s.reverse();
}
let instructions: Vec<Vec<usize>> = split
.next()
.unwrap()
.lines()
.map(|line| {
line.split(" ")
.flat_map(|w| w.parse::<usize>())
.collect::<Vec<usize>>()
})
.collect();
for instr in instructions {
let amt = instr[0];
let src = instr[1];
let dst = instr[2];
let mut stack = vec![];
for _ in 0..amt {
let c = state[src - 1].pop().unwrap();
stack.push(c);
}
for _ in 0..amt {
state[dst - 1].push(stack.pop().unwrap());
}
}
for s in state {
print!("{}", s.last().unwrap());
}
println!();
}
|