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
|
use std::collections::HashMap;
use std::collections::VecDeque;
pub fn part1(input: &str) -> String {
let mut h: Vec<VecDeque<char>> = Vec::with_capacity(10);
h.resize(10, VecDeque::new());
let split: Vec<&str> = input.split("\n\n").collect();
let mut m: HashMap<usize, usize> = HashMap::new();
let mut i = 0;
let mut j = 1;
while i < 10 {
m.insert(j, i);
j += 4;
i += 1;
}
assert!(split.len() == 2);
for line in split[0].lines() {
if line.chars().nth(1).unwrap() == '1' {
continue;
}
for (i, ch) in line.chars().enumerate() {
match ch {
' ' | '[' | ']' => {
continue;
}
x => {
let idx = m[&i];
h[idx].push_front(x);
}
};
}
}
for line in split[1].lines() {
let actions: Vec<usize> = line
.split(' ')
.enumerate()
.filter(|&(i, _)| i % 2 == 1)
.map(|(_, e)| e.parse::<usize>().unwrap())
.collect();
for _ in 0..actions[0] {
let x = h[actions[1] - 1].pop_back().unwrap();
h[actions[2] - 1].push_back(x);
}
}
let mut s = String::new();
for mut e in h {
if e.len() == 0 {
continue;
}
let x = e.pop_back().unwrap().to_string();
s += &x;
}
s
}
pub fn part2(input: &str) -> String {
let mut h: Vec<VecDeque<char>> = Vec::with_capacity(10);
h.resize(10, VecDeque::new());
let split: Vec<&str> = input.split("\n\n").collect();
let mut m: HashMap<usize, usize> = HashMap::new();
let mut i = 0;
let mut j = 1;
while i < 10 {
m.insert(j, i);
j += 4;
i += 1;
}
assert!(split.len() == 2);
for line in split[0].lines() {
if line.chars().nth(1).unwrap() == '1' {
continue;
}
for (i, ch) in line.chars().enumerate() {
match ch {
' ' | '[' | ']' => {
continue;
}
x => {
let idx = m[&i];
h[idx].push_front(x);
}
};
}
}
for line in split[1].lines() {
let actions: Vec<usize> = line
.split(' ')
.enumerate()
.filter(|&(i, _)| i % 2 == 1)
.map(|(_, e)| e.parse::<usize>().unwrap())
.collect();
let mut xs = Vec::<char>::new();
for _ in 0..actions[0] {
xs.push(h[actions[1] - 1].pop_back().unwrap());
}
xs.reverse();
for x in xs {
h[actions[2] - 1].push_back(x);
}
}
let mut s = String::new();
for mut e in h {
if e.len() == 0 {
continue;
}
let x = e.pop_back().unwrap().to_string();
s += &x;
}
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example() {
let input = include_str!("../example.txt");
assert_eq!(part1(input), String::from("CMZ"));
}
#[test]
fn test_input1() {
let input = include_str!("../input.txt");
assert_eq!(part1(input), String::from("RTGWZTHLD"));
}
#[test]
fn test_example2() {
let input = include_str!("../example.txt");
assert_eq!(part2(input), String::from("MCD"));
}
#[test]
fn test_input2() {
let input = include_str!("../input.txt");
assert_eq!(part2(input), String::from("STHGRZZFR"));
}
}
|