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
|
use std::{collections::VecDeque, num::ParseIntError, str::FromStr};
const STACK_ELEMENT_WIDTH: usize = 3;
const STACKS_SIZE: usize = 9;
#[derive(Debug, Clone)]
struct Stack {
stack: VecDeque<StackElement>,
}
impl FromStr for Stack {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Stack {
stack: s
.lines()
.map(|line| line.parse::<StackElement>().unwrap())
.collect::<VecDeque<_>>(),
})
}
}
impl Stack {
fn new() -> Self {
Stack {
stack: VecDeque::new(),
}
}
}
#[derive(Debug, Clone)]
struct StackElement {
ele: char,
}
impl FromStr for StackElement {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let ch = s.chars().nth(1).unwrap();
Ok(StackElement { ele: ch })
}
}
#[derive(Debug)]
struct Command {
num_of_times: usize,
from: usize,
target: usize,
}
impl FromStr for Command {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s: Vec<usize> = s
.split_whitespace()
.collect::<Vec<_>>()
.iter()
.filter(|c| c.bytes().all(|c| c.is_ascii_digit()))
.map(|n| n.parse().unwrap())
.collect();
let (num_of_times, from, target) = (s[0], s[1], s[2]);
Ok(Command {
num_of_times,
from,
target,
})
}
}
fn apply_command(stacks: &mut Vec<Stack>, cmd: &Command, part1: bool) {
if part1 {
for _ in 0..cmd.num_of_times {
let element_to_push = stacks[cmd.from - 1]
.stack
.pop_back()
.expect("welp the stack is empty");
stacks[cmd.target - 1].stack.push_back(element_to_push);
}
} else {
let mut tmp_stack: VecDeque<StackElement> = VecDeque::new();
for _ in 0..cmd.num_of_times {
let element_to_push = stacks[cmd.from - 1]
.stack
.pop_back()
.expect("welp the stack is empty");
tmp_stack.push_back(element_to_push);
}
for _ in 0..cmd.num_of_times {
stacks[cmd.target - 1]
.stack
.push_back(tmp_stack.pop_back().unwrap());
}
}
}
fn solve_part_one(data: &str) -> String {
let mut stacks: Vec<Stack> = vec![Stack::new(); STACKS_SIZE];
let stack = data.split("\n\n").nth(0).unwrap();
for x in stack.lines() {
let chars = x.chars().clone();
for (i, ch) in x.chars().enumerate() {
if ch == '[' {
stacks[i / 4].stack.push_front(
chars.as_str()[i..i + STACK_ELEMENT_WIDTH]
.parse::<StackElement>()
.unwrap(),
)
}
}
}
let commands: Vec<Command> = data
.split("\n\n")
.nth(1)
.unwrap()
.lines()
.map(|command| command.parse::<Command>().unwrap())
.collect();
for cmd in &commands {
apply_command(&mut stacks, cmd, true);
}
let mut ans = "".to_string();
for i in 0..STACKS_SIZE {
if stacks[i].stack.len() >= 1 {
ans += stacks[i]
.stack
.iter()
.last()
.unwrap()
.ele
.to_string()
.as_str();
}
}
ans
}
fn solve_part_two(data: &str) -> String {
let mut stacks: Vec<Stack> = vec![Stack::new(); STACKS_SIZE];
let stack = data.split("\n\n").nth(0).unwrap();
for x in stack.lines() {
let chars = x.chars().clone();
for (i, ch) in x.chars().enumerate() {
if ch == '[' {
stacks[i / 4].stack.push_front(
chars.as_str()[i..i + STACK_ELEMENT_WIDTH]
.parse::<StackElement>()
.unwrap(),
)
}
}
}
let commands: Vec<Command> = data
.split("\n\n")
.nth(1)
.unwrap()
.lines()
.map(|command| command.parse::<Command>().unwrap())
.collect();
for cmd in &commands {
apply_command(&mut stacks, cmd, false);
}
let mut ans = "".to_string();
for i in 0..STACKS_SIZE {
if stacks[i].stack.len() >= 1 {
ans += stacks[i]
.stack
.iter()
.last()
.unwrap()
.ele
.to_string()
.as_str();
}
}
ans
}
fn main() {
let data_test = include_str!("../input/day5.test");
let data_prod = include_str!("../input/day5.prod");
println!("part one test: {}", solve_part_one(data_test));
println!("part one prod: {}", solve_part_one(data_prod));
println!("part two test: {}", solve_part_two(data_test));
println!("part two prod: {}", solve_part_two(data_prod));
}
|