summaryrefslogtreecommitdiff
path: root/aoc22/day5/src/main.rs
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2024-07-06 14:10:15 +0200
committerMathias Magnusson <mathias@magnusson.space>2024-07-06 14:12:06 +0200
commit8ab3bcb7992ecd4d0234ff31ab2e50d1562f67ba (patch)
treef13ddef97559d6d14004357f77ea153a732f217e /aoc22/day5/src/main.rs
parent4e8ac826929117f95baeb37e1518773d1169d900 (diff)
downloadprogramming-problem-solving-8ab3bcb7992ecd4d0234ff31ab2e50d1562f67ba.tar.gz
Some old uncommited aoc22
Diffstat (limited to 'aoc22/day5/src/main.rs')
-rw-r--r--aoc22/day5/src/main.rs107
1 files changed, 107 insertions, 0 deletions
diff --git a/aoc22/day5/src/main.rs b/aoc22/day5/src/main.rs
new file mode 100644
index 0000000..714e67c
--- /dev/null
+++ b/aoc22/day5/src/main.rs
@@ -0,0 +1,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!();
+}