From 9c7d3b1359f2a27ad140b94fe68060ae4f50f05b Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Mon, 5 Dec 2022 10:54:06 -0500 Subject: [PATCH] day 5 solution --- 2022/day5.txt | 512 ++++++++++++++++++++++++++++++++++++++++++ 2022/src/days/day5.rs | 80 +++++++ 2022/src/days/mod.rs | 1 + 2022/src/main.rs | 3 + 2022/tests/day5.txt | 9 + 5 files changed, 605 insertions(+) create mode 100644 2022/day5.txt create mode 100644 2022/src/days/day5.rs create mode 100644 2022/tests/day5.txt diff --git a/2022/day5.txt b/2022/day5.txt new file mode 100644 index 0000000..f8e58cb --- /dev/null +++ b/2022/day5.txt @@ -0,0 +1,512 @@ + [F] [Q] [Q] +[B] [Q] [V] [D] [S] +[S] [P] [T] [R] [M] [D] +[J] [V] [W] [M] [F] [J] [J] +[Z] [G] [S] [W] [N] [D] [R] [T] +[V] [M] [B] [G] [S] [C] [T] [V] [S] +[D] [S] [L] [J] [L] [G] [G] [F] [R] +[G] [Z] [C] [H] [C] [R] [H] [P] [D] + 1 2 3 4 5 6 7 8 9 + +move 3 from 5 to 2 +move 3 from 8 to 4 +move 7 from 7 to 3 +move 14 from 3 to 9 +move 8 from 4 to 1 +move 1 from 7 to 5 +move 2 from 6 to 4 +move 4 from 5 to 7 +move 1 from 3 to 6 +move 3 from 4 to 3 +move 1 from 4 to 1 +move 5 from 1 to 9 +move 1 from 4 to 6 +move 4 from 7 to 4 +move 15 from 9 to 2 +move 7 from 1 to 6 +move 3 from 3 to 5 +move 1 from 4 to 9 +move 2 from 5 to 3 +move 2 from 4 to 9 +move 4 from 1 to 6 +move 1 from 3 to 1 +move 1 from 3 to 2 +move 4 from 6 to 3 +move 24 from 2 to 8 +move 4 from 9 to 8 +move 1 from 1 to 3 +move 2 from 5 to 4 +move 1 from 2 to 4 +move 19 from 8 to 1 +move 5 from 3 to 9 +move 8 from 1 to 3 +move 3 from 4 to 1 +move 6 from 9 to 5 +move 2 from 3 to 4 +move 1 from 8 to 5 +move 2 from 4 to 6 +move 11 from 6 to 1 +move 8 from 8 to 7 +move 1 from 6 to 5 +move 13 from 1 to 3 +move 1 from 1 to 7 +move 2 from 7 to 8 +move 5 from 7 to 1 +move 2 from 8 to 4 +move 3 from 5 to 3 +move 11 from 3 to 1 +move 2 from 5 to 3 +move 2 from 5 to 3 +move 2 from 7 to 1 +move 7 from 3 to 1 +move 1 from 4 to 5 +move 1 from 6 to 4 +move 3 from 4 to 7 +move 3 from 7 to 1 +move 6 from 3 to 5 +move 1 from 5 to 9 +move 4 from 5 to 4 +move 2 from 3 to 4 +move 8 from 9 to 2 +move 5 from 4 to 6 +move 1 from 6 to 5 +move 1 from 4 to 9 +move 39 from 1 to 7 +move 7 from 2 to 6 +move 1 from 9 to 3 +move 1 from 2 to 7 +move 1 from 3 to 1 +move 5 from 7 to 3 +move 4 from 5 to 1 +move 19 from 7 to 9 +move 1 from 9 to 8 +move 1 from 9 to 7 +move 5 from 9 to 3 +move 6 from 6 to 7 +move 1 from 8 to 3 +move 4 from 1 to 4 +move 23 from 7 to 6 +move 1 from 1 to 6 +move 21 from 6 to 2 +move 3 from 4 to 8 +move 7 from 6 to 1 +move 1 from 4 to 9 +move 1 from 6 to 7 +move 6 from 1 to 2 +move 1 from 7 to 4 +move 15 from 2 to 8 +move 5 from 3 to 8 +move 22 from 8 to 7 +move 1 from 8 to 1 +move 5 from 3 to 4 +move 1 from 3 to 2 +move 1 from 1 to 2 +move 3 from 4 to 8 +move 3 from 8 to 9 +move 11 from 2 to 1 +move 2 from 1 to 4 +move 15 from 9 to 5 +move 22 from 7 to 3 +move 2 from 4 to 9 +move 3 from 4 to 2 +move 8 from 1 to 8 +move 6 from 8 to 6 +move 1 from 6 to 2 +move 3 from 6 to 9 +move 3 from 2 to 7 +move 4 from 2 to 9 +move 2 from 7 to 5 +move 1 from 1 to 7 +move 2 from 8 to 2 +move 2 from 7 to 5 +move 9 from 5 to 3 +move 8 from 5 to 2 +move 1 from 6 to 4 +move 1 from 6 to 9 +move 1 from 2 to 9 +move 2 from 5 to 1 +move 7 from 2 to 3 +move 1 from 4 to 3 +move 1 from 2 to 4 +move 5 from 3 to 4 +move 6 from 9 to 3 +move 1 from 2 to 6 +move 6 from 9 to 6 +move 2 from 1 to 8 +move 3 from 6 to 3 +move 2 from 8 to 6 +move 6 from 4 to 1 +move 14 from 3 to 9 +move 1 from 6 to 4 +move 3 from 3 to 9 +move 1 from 4 to 5 +move 10 from 9 to 6 +move 6 from 6 to 7 +move 2 from 1 to 8 +move 1 from 8 to 6 +move 16 from 3 to 2 +move 1 from 8 to 1 +move 1 from 7 to 1 +move 7 from 3 to 4 +move 1 from 6 to 5 +move 4 from 2 to 3 +move 5 from 4 to 9 +move 2 from 4 to 5 +move 4 from 7 to 4 +move 5 from 9 to 6 +move 2 from 5 to 4 +move 11 from 6 to 7 +move 1 from 6 to 8 +move 5 from 1 to 5 +move 2 from 6 to 4 +move 7 from 7 to 3 +move 1 from 8 to 6 +move 2 from 7 to 3 +move 1 from 1 to 3 +move 3 from 2 to 8 +move 9 from 2 to 5 +move 1 from 6 to 1 +move 1 from 4 to 8 +move 7 from 4 to 7 +move 8 from 5 to 6 +move 1 from 7 to 2 +move 1 from 7 to 4 +move 3 from 7 to 8 +move 1 from 2 to 3 +move 1 from 1 to 2 +move 1 from 1 to 7 +move 3 from 7 to 6 +move 11 from 6 to 2 +move 4 from 8 to 7 +move 2 from 8 to 7 +move 15 from 3 to 2 +move 7 from 9 to 4 +move 3 from 3 to 2 +move 4 from 4 to 7 +move 5 from 7 to 3 +move 3 from 4 to 6 +move 3 from 6 to 9 +move 1 from 4 to 2 +move 1 from 8 to 1 +move 2 from 3 to 7 +move 2 from 3 to 7 +move 23 from 2 to 5 +move 1 from 9 to 1 +move 1 from 7 to 9 +move 1 from 1 to 8 +move 8 from 7 to 1 +move 1 from 8 to 4 +move 1 from 4 to 2 +move 3 from 9 to 8 +move 1 from 7 to 9 +move 22 from 5 to 9 +move 1 from 8 to 5 +move 1 from 7 to 4 +move 1 from 4 to 5 +move 1 from 8 to 3 +move 2 from 9 to 3 +move 5 from 5 to 2 +move 5 from 5 to 4 +move 3 from 2 to 7 +move 1 from 7 to 3 +move 6 from 1 to 7 +move 4 from 3 to 1 +move 6 from 2 to 8 +move 1 from 5 to 6 +move 2 from 8 to 1 +move 12 from 9 to 4 +move 8 from 9 to 4 +move 1 from 2 to 9 +move 2 from 9 to 8 +move 3 from 2 to 8 +move 5 from 8 to 6 +move 7 from 7 to 1 +move 4 from 8 to 9 +move 1 from 6 to 1 +move 17 from 4 to 7 +move 1 from 2 to 4 +move 2 from 4 to 1 +move 6 from 4 to 6 +move 1 from 1 to 4 +move 7 from 1 to 5 +move 9 from 7 to 9 +move 8 from 9 to 8 +move 5 from 8 to 3 +move 1 from 5 to 6 +move 2 from 3 to 6 +move 1 from 9 to 1 +move 1 from 6 to 1 +move 10 from 6 to 1 +move 1 from 5 to 1 +move 2 from 9 to 1 +move 1 from 9 to 7 +move 2 from 6 to 8 +move 2 from 8 to 2 +move 1 from 6 to 8 +move 22 from 1 to 9 +move 9 from 7 to 5 +move 1 from 8 to 1 +move 2 from 8 to 3 +move 4 from 5 to 9 +move 1 from 8 to 3 +move 5 from 1 to 9 +move 2 from 7 to 3 +move 2 from 4 to 7 +move 1 from 8 to 5 +move 2 from 2 to 4 +move 1 from 5 to 8 +move 9 from 5 to 8 +move 2 from 7 to 5 +move 2 from 4 to 5 +move 3 from 8 to 4 +move 3 from 4 to 3 +move 2 from 8 to 6 +move 1 from 6 to 4 +move 3 from 5 to 9 +move 1 from 6 to 3 +move 12 from 3 to 5 +move 1 from 3 to 1 +move 7 from 5 to 4 +move 1 from 1 to 3 +move 1 from 8 to 1 +move 7 from 5 to 1 +move 6 from 9 to 6 +move 29 from 9 to 5 +move 2 from 4 to 6 +move 26 from 5 to 2 +move 24 from 2 to 7 +move 1 from 3 to 2 +move 8 from 1 to 7 +move 7 from 6 to 9 +move 2 from 5 to 3 +move 1 from 6 to 4 +move 3 from 8 to 5 +move 2 from 3 to 8 +move 2 from 2 to 8 +move 5 from 9 to 2 +move 27 from 7 to 2 +move 2 from 8 to 3 +move 2 from 9 to 5 +move 3 from 8 to 5 +move 2 from 7 to 4 +move 3 from 4 to 7 +move 2 from 3 to 2 +move 4 from 5 to 1 +move 5 from 7 to 2 +move 29 from 2 to 8 +move 9 from 8 to 3 +move 2 from 4 to 8 +move 7 from 3 to 2 +move 3 from 5 to 4 +move 1 from 7 to 5 +move 3 from 5 to 6 +move 2 from 1 to 8 +move 2 from 6 to 8 +move 3 from 4 to 2 +move 4 from 4 to 2 +move 1 from 6 to 8 +move 8 from 2 to 4 +move 2 from 3 to 5 +move 1 from 4 to 1 +move 3 from 1 to 2 +move 4 from 8 to 2 +move 3 from 4 to 9 +move 3 from 4 to 1 +move 2 from 9 to 5 +move 1 from 4 to 6 +move 4 from 5 to 1 +move 1 from 6 to 8 +move 1 from 9 to 3 +move 4 from 2 to 3 +move 15 from 8 to 2 +move 9 from 8 to 1 +move 1 from 3 to 9 +move 5 from 1 to 9 +move 3 from 9 to 7 +move 2 from 7 to 6 +move 3 from 3 to 2 +move 1 from 7 to 8 +move 1 from 9 to 6 +move 1 from 9 to 8 +move 2 from 8 to 2 +move 1 from 1 to 2 +move 1 from 3 to 7 +move 4 from 1 to 7 +move 19 from 2 to 5 +move 1 from 1 to 4 +move 1 from 7 to 4 +move 1 from 1 to 5 +move 3 from 1 to 4 +move 1 from 1 to 8 +move 6 from 2 to 4 +move 7 from 2 to 1 +move 2 from 7 to 9 +move 8 from 2 to 8 +move 2 from 7 to 3 +move 1 from 6 to 4 +move 10 from 4 to 6 +move 5 from 6 to 7 +move 2 from 9 to 8 +move 6 from 8 to 9 +move 1 from 2 to 3 +move 2 from 8 to 3 +move 5 from 1 to 8 +move 8 from 5 to 2 +move 8 from 8 to 7 +move 7 from 2 to 8 +move 1 from 1 to 2 +move 1 from 9 to 7 +move 1 from 4 to 2 +move 2 from 2 to 6 +move 5 from 9 to 3 +move 2 from 8 to 6 +move 2 from 3 to 9 +move 4 from 8 to 6 +move 7 from 6 to 1 +move 8 from 1 to 5 +move 1 from 8 to 7 +move 1 from 9 to 6 +move 12 from 5 to 3 +move 1 from 4 to 8 +move 2 from 9 to 5 +move 1 from 2 to 3 +move 3 from 5 to 1 +move 1 from 1 to 5 +move 21 from 3 to 8 +move 2 from 1 to 5 +move 6 from 5 to 7 +move 2 from 5 to 6 +move 10 from 6 to 9 +move 1 from 6 to 8 +move 13 from 8 to 2 +move 2 from 5 to 4 +move 2 from 4 to 3 +move 4 from 9 to 1 +move 5 from 7 to 8 +move 12 from 8 to 1 +move 5 from 9 to 6 +move 1 from 3 to 7 +move 2 from 6 to 5 +move 11 from 2 to 1 +move 1 from 8 to 4 +move 16 from 1 to 9 +move 1 from 2 to 6 +move 1 from 8 to 5 +move 12 from 9 to 3 +move 14 from 7 to 2 +move 1 from 7 to 9 +move 1 from 4 to 2 +move 1 from 7 to 5 +move 3 from 9 to 5 +move 4 from 6 to 9 +move 3 from 9 to 4 +move 1 from 8 to 4 +move 2 from 4 to 5 +move 1 from 7 to 1 +move 5 from 3 to 5 +move 2 from 4 to 2 +move 8 from 2 to 7 +move 7 from 2 to 4 +move 1 from 3 to 7 +move 3 from 9 to 7 +move 2 from 2 to 9 +move 3 from 4 to 5 +move 6 from 1 to 8 +move 6 from 1 to 5 +move 3 from 9 to 2 +move 22 from 5 to 9 +move 1 from 5 to 6 +move 2 from 2 to 3 +move 5 from 7 to 6 +move 5 from 8 to 9 +move 2 from 7 to 2 +move 20 from 9 to 4 +move 1 from 8 to 3 +move 2 from 2 to 5 +move 1 from 2 to 5 +move 15 from 4 to 8 +move 1 from 5 to 7 +move 6 from 9 to 1 +move 5 from 4 to 8 +move 2 from 4 to 8 +move 1 from 2 to 1 +move 5 from 6 to 5 +move 5 from 5 to 7 +move 1 from 9 to 8 +move 5 from 7 to 2 +move 2 from 5 to 1 +move 4 from 7 to 5 +move 1 from 5 to 9 +move 1 from 6 to 8 +move 1 from 7 to 2 +move 6 from 3 to 4 +move 3 from 5 to 7 +move 1 from 9 to 2 +move 6 from 2 to 3 +move 1 from 3 to 4 +move 13 from 8 to 9 +move 7 from 1 to 5 +move 6 from 9 to 2 +move 1 from 1 to 4 +move 6 from 2 to 3 +move 1 from 1 to 4 +move 5 from 9 to 7 +move 11 from 8 to 4 +move 7 from 7 to 3 +move 2 from 7 to 8 +move 1 from 8 to 2 +move 8 from 4 to 1 +move 2 from 1 to 6 +move 2 from 5 to 8 +move 3 from 1 to 9 +move 1 from 8 to 2 +move 11 from 3 to 2 +move 2 from 8 to 9 +move 9 from 4 to 7 +move 11 from 3 to 8 +move 7 from 9 to 6 +move 5 from 4 to 6 +move 3 from 7 to 3 +move 1 from 7 to 1 +move 5 from 7 to 6 +move 2 from 3 to 5 +move 1 from 3 to 4 +move 5 from 2 to 5 +move 1 from 1 to 7 +move 1 from 4 to 8 +move 1 from 7 to 6 +move 7 from 5 to 7 +move 2 from 5 to 7 +move 3 from 1 to 7 +move 1 from 2 to 3 +move 1 from 6 to 4 +move 1 from 3 to 4 +move 1 from 5 to 3 +move 18 from 6 to 4 +move 9 from 7 to 1 +move 14 from 4 to 6 +move 3 from 6 to 4 +move 12 from 6 to 7 +move 2 from 5 to 3 +move 3 from 7 to 4 +move 6 from 4 to 7 +move 5 from 1 to 7 +move 5 from 4 to 5 +move 5 from 2 to 1 +move 9 from 8 to 4 +move 9 from 1 to 3 +move 2 from 8 to 2 +move 4 from 2 to 4 +move 1 from 7 to 6 +move 1 from 2 to 3 +move 1 from 8 to 9 +move 1 from 6 to 9 +move 2 from 9 to 3 +move 3 from 4 to 1 +move 13 from 3 to 5 +move 12 from 5 to 1 +move 7 from 1 to 8 +move 1 from 3 to 6 +move 4 from 5 to 4 +move 1 from 5 to 2 +move 8 from 4 to 9 diff --git a/2022/src/days/day5.rs b/2022/src/days/day5.rs new file mode 100644 index 0000000..ca62dd8 --- /dev/null +++ b/2022/src/days/day5.rs @@ -0,0 +1,80 @@ +use anyhow::Result; +use std::fs; + +pub fn run() -> Result<()> { + let file_contents = fs::read_to_string("day5.txt")?; + let mut stacks: Vec> = Vec::with_capacity(10); + let mut moves: Vec<(usize, usize, usize)> = Vec::with_capacity(1000); + let mut numstacks: usize = 0; + + let mut containers: Vec<&str> = Vec::with_capacity(20); + for line in file_contents.lines() { + if line.contains("[") { + containers.push(line); + } else { + for stacknums in line.split(" ") { + if let Ok(num) = stacknums.parse::() { + numstacks = num; + stacks.push(Vec::with_capacity(100)) + } + } + println!("found {} numstacks", numstacks); + break; + } + } + + for line in containers.into_iter() { + for index in 0..numstacks { + let col = index * 4 + 1; + let t = &line[col..col + 1]; + if t != " " { + stacks[index].insert(0, t); + } + } + } + + for line in file_contents.lines() { + if !line.contains("move") { + continue; + } + + let mut tokens = line.split(" "); + let count = tokens.nth(1).expect("missing count").parse::()?; + let from = tokens.nth(1).expect("missing from").parse::()?; + let to = tokens.nth(1).expect("missing to").parse::()?; + moves.push((count, from, to)); + } + + let mut stacks2 = stacks.clone(); + + for (count, from, to) in moves.iter() { + for _x in 0..*count { + let c = stacks[from - 1].pop().expect("too many pops"); + stacks[to - 1].push(c); + } + } + + let final_message = stacks + .iter() + .map(|e| e[e.len() - 1]) + .collect::>() + .join(""); + println!("final {}", final_message); + + for (count, from, to) in moves.iter() { + let x = stacks2[to - 1].len(); + for _ in 0..*count { + let c = stacks2[from - 1].pop().expect("too many pops"); + stacks2[to - 1].insert(x, c); + } + } + + let second_message = stacks2 + .iter() + .map(|e| e[e.len() - 1]) + .collect::>() + .join(""); + println!("second message {}", second_message); + + Ok(()) +} diff --git a/2022/src/days/mod.rs b/2022/src/days/mod.rs index 5415e92..acd1d2c 100644 --- a/2022/src/days/mod.rs +++ b/2022/src/days/mod.rs @@ -2,3 +2,4 @@ pub mod day1; pub mod day2; pub mod day3; pub mod day4; +pub mod day5; diff --git a/2022/src/main.rs b/2022/src/main.rs index 9ac144c..9a0c24f 100644 --- a/2022/src/main.rs +++ b/2022/src/main.rs @@ -21,6 +21,9 @@ fn run_day(number: i32) -> Result<()> { 4 => { days::day4::run()?; } + 5 => { + days::day5::run()?; + } _ => return Err(anyhow!("Invalid day provided")), } Ok(()) diff --git a/2022/tests/day5.txt b/2022/tests/day5.txt new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/tests/day5.txt @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2