From 009bd54c796d3eded44181b653ecc83e69ae1268 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Mon, 5 Dec 2022 09:12:45 -0500 Subject: [PATCH] Add 2022 day 5 --- 2022/5/Cargo.lock | 14 ++ 2022/5/Cargo.toml | 9 + 2022/5/input | 501 +++++++++++++++++++++++++++++++++++++++++++++ 2022/5/src/main.rs | 69 +++++++ 4 files changed, 593 insertions(+) create mode 100644 2022/5/Cargo.lock create mode 100644 2022/5/Cargo.toml create mode 100644 2022/5/input create mode 100644 2022/5/src/main.rs diff --git a/2022/5/Cargo.lock b/2022/5/Cargo.lock new file mode 100644 index 0000000..8659611 --- /dev/null +++ b/2022/5/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "common" +version = "0.1.0" + +[[package]] +name = "day5" +version = "0.1.0" +dependencies = [ + "common", +] diff --git a/2022/5/Cargo.toml b/2022/5/Cargo.toml new file mode 100644 index 0000000..9d9950e --- /dev/null +++ b/2022/5/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day5" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +common = { path = "../common" } diff --git a/2022/5/input b/2022/5/input new file mode 100644 index 0000000..e1a3595 --- /dev/null +++ b/2022/5/input @@ -0,0 +1,501 @@ +move 1 from 5 to 6 +move 5 from 6 to 7 +move 10 from 7 to 3 +move 4 from 8 to 4 +move 2 from 5 to 4 +move 4 from 3 to 6 +move 6 from 2 to 4 +move 8 from 6 to 9 +move 5 from 9 to 2 +move 7 from 2 to 7 +move 2 from 1 to 4 +move 3 from 3 to 8 +move 1 from 5 to 9 +move 1 from 3 to 8 +move 1 from 1 to 2 +move 11 from 4 to 6 +move 2 from 5 to 6 +move 10 from 9 to 1 +move 4 from 8 to 3 +move 7 from 7 to 1 +move 9 from 1 to 2 +move 1 from 6 to 5 +move 1 from 5 to 9 +move 5 from 3 to 8 +move 2 from 9 to 1 +move 5 from 3 to 9 +move 3 from 6 to 8 +move 5 from 9 to 6 +move 6 from 6 to 3 +move 3 from 3 to 2 +move 1 from 9 to 8 +move 13 from 2 to 3 +move 3 from 8 to 1 +move 11 from 1 to 4 +move 3 from 4 to 1 +move 2 from 6 to 5 +move 4 from 6 to 8 +move 17 from 3 to 9 +move 1 from 1 to 8 +move 1 from 6 to 5 +move 1 from 3 to 7 +move 1 from 7 to 4 +move 3 from 4 to 1 +move 1 from 3 to 8 +move 4 from 8 to 1 +move 3 from 5 to 9 +move 1 from 6 to 4 +move 4 from 4 to 8 +move 2 from 8 to 4 +move 2 from 1 to 6 +move 4 from 8 to 6 +move 1 from 8 to 3 +move 6 from 6 to 3 +move 6 from 3 to 9 +move 6 from 1 to 4 +move 5 from 8 to 4 +move 1 from 3 to 6 +move 3 from 1 to 7 +move 1 from 6 to 7 +move 4 from 4 to 5 +move 24 from 9 to 5 +move 2 from 9 to 1 +move 27 from 5 to 7 +move 13 from 7 to 2 +move 1 from 5 to 9 +move 7 from 2 to 7 +move 1 from 9 to 8 +move 5 from 2 to 8 +move 1 from 2 to 5 +move 1 from 5 to 7 +move 21 from 4 to 1 +move 1 from 4 to 6 +move 1 from 6 to 5 +move 22 from 7 to 5 +move 2 from 7 to 8 +move 7 from 5 to 4 +move 1 from 4 to 5 +move 2 from 7 to 9 +move 5 from 5 to 2 +move 5 from 4 to 2 +move 3 from 5 to 1 +move 7 from 8 to 7 +move 1 from 4 to 1 +move 23 from 1 to 8 +move 2 from 9 to 4 +move 11 from 8 to 3 +move 3 from 1 to 3 +move 1 from 4 to 2 +move 12 from 3 to 2 +move 7 from 7 to 3 +move 3 from 2 to 1 +move 1 from 4 to 9 +move 1 from 1 to 3 +move 9 from 8 to 6 +move 2 from 5 to 4 +move 3 from 1 to 7 +move 3 from 2 to 4 +move 7 from 2 to 3 +move 9 from 3 to 4 +move 7 from 5 to 2 +move 2 from 7 to 2 +move 1 from 7 to 2 +move 13 from 4 to 6 +move 1 from 9 to 8 +move 2 from 8 to 2 +move 12 from 2 to 1 +move 3 from 3 to 1 +move 1 from 8 to 1 +move 5 from 3 to 7 +move 3 from 2 to 8 +move 7 from 2 to 5 +move 3 from 8 to 3 +move 1 from 4 to 8 +move 22 from 6 to 4 +move 1 from 3 to 6 +move 3 from 5 to 8 +move 4 from 5 to 8 +move 1 from 3 to 9 +move 8 from 4 to 2 +move 8 from 8 to 3 +move 1 from 6 to 3 +move 4 from 2 to 6 +move 1 from 9 to 4 +move 5 from 3 to 9 +move 2 from 8 to 1 +move 3 from 2 to 1 +move 10 from 4 to 8 +move 4 from 7 to 6 +move 10 from 1 to 3 +move 9 from 8 to 2 +move 1 from 7 to 1 +move 15 from 3 to 1 +move 1 from 8 to 9 +move 4 from 4 to 1 +move 17 from 1 to 3 +move 3 from 2 to 3 +move 3 from 6 to 8 +move 5 from 9 to 7 +move 11 from 1 to 8 +move 4 from 7 to 8 +move 6 from 2 to 5 +move 2 from 1 to 4 +move 4 from 6 to 8 +move 16 from 8 to 6 +move 2 from 6 to 1 +move 1 from 9 to 5 +move 1 from 7 to 5 +move 2 from 5 to 6 +move 5 from 6 to 3 +move 2 from 8 to 5 +move 1 from 2 to 1 +move 10 from 6 to 3 +move 6 from 5 to 9 +move 2 from 1 to 2 +move 2 from 4 to 2 +move 1 from 2 to 4 +move 5 from 9 to 2 +move 1 from 4 to 3 +move 1 from 9 to 7 +move 1 from 6 to 1 +move 1 from 1 to 7 +move 2 from 7 to 5 +move 7 from 2 to 5 +move 6 from 5 to 1 +move 1 from 2 to 3 +move 1 from 4 to 1 +move 2 from 8 to 9 +move 8 from 1 to 3 +move 2 from 5 to 3 +move 29 from 3 to 9 +move 5 from 3 to 8 +move 6 from 8 to 5 +move 1 from 6 to 5 +move 6 from 3 to 2 +move 2 from 2 to 4 +move 1 from 1 to 7 +move 18 from 9 to 6 +move 2 from 2 to 9 +move 2 from 2 to 8 +move 13 from 6 to 8 +move 1 from 7 to 4 +move 3 from 5 to 6 +move 1 from 5 to 7 +move 1 from 7 to 4 +move 14 from 9 to 3 +move 3 from 4 to 5 +move 1 from 9 to 7 +move 14 from 3 to 2 +move 1 from 7 to 3 +move 4 from 2 to 5 +move 16 from 8 to 6 +move 11 from 6 to 9 +move 13 from 6 to 4 +move 5 from 5 to 2 +move 12 from 2 to 4 +move 19 from 4 to 3 +move 7 from 4 to 5 +move 14 from 5 to 2 +move 2 from 3 to 6 +move 3 from 9 to 5 +move 2 from 6 to 2 +move 1 from 5 to 2 +move 3 from 5 to 4 +move 3 from 4 to 1 +move 7 from 9 to 6 +move 4 from 6 to 1 +move 1 from 1 to 8 +move 3 from 6 to 9 +move 1 from 8 to 7 +move 1 from 9 to 6 +move 4 from 1 to 2 +move 1 from 7 to 2 +move 2 from 9 to 8 +move 10 from 2 to 9 +move 2 from 2 to 9 +move 11 from 3 to 7 +move 1 from 8 to 9 +move 2 from 3 to 7 +move 1 from 1 to 7 +move 10 from 2 to 4 +move 3 from 4 to 1 +move 4 from 1 to 8 +move 1 from 6 to 5 +move 6 from 7 to 9 +move 3 from 9 to 1 +move 1 from 5 to 1 +move 4 from 4 to 2 +move 5 from 2 to 1 +move 1 from 2 to 7 +move 2 from 7 to 6 +move 1 from 2 to 1 +move 2 from 9 to 1 +move 3 from 4 to 7 +move 1 from 3 to 7 +move 2 from 8 to 3 +move 2 from 6 to 5 +move 2 from 5 to 8 +move 10 from 7 to 2 +move 6 from 9 to 1 +move 1 from 7 to 3 +move 2 from 8 to 9 +move 7 from 3 to 7 +move 7 from 3 to 9 +move 1 from 8 to 9 +move 6 from 2 to 8 +move 13 from 9 to 1 +move 6 from 9 to 8 +move 2 from 2 to 7 +move 3 from 7 to 1 +move 1 from 8 to 1 +move 1 from 1 to 6 +move 16 from 1 to 4 +move 2 from 7 to 5 +move 12 from 4 to 9 +move 4 from 8 to 6 +move 2 from 5 to 1 +move 8 from 8 to 4 +move 2 from 4 to 5 +move 1 from 8 to 6 +move 4 from 6 to 8 +move 19 from 1 to 9 +move 3 from 8 to 5 +move 1 from 6 to 9 +move 2 from 2 to 1 +move 10 from 4 to 9 +move 1 from 1 to 2 +move 2 from 1 to 5 +move 4 from 7 to 9 +move 1 from 8 to 2 +move 1 from 2 to 6 +move 7 from 5 to 4 +move 11 from 9 to 8 +move 1 from 4 to 3 +move 10 from 8 to 1 +move 1 from 2 to 3 +move 29 from 9 to 3 +move 2 from 6 to 5 +move 1 from 5 to 3 +move 5 from 9 to 3 +move 1 from 8 to 9 +move 1 from 9 to 3 +move 6 from 4 to 6 +move 1 from 5 to 1 +move 1 from 6 to 3 +move 2 from 1 to 5 +move 1 from 9 to 5 +move 37 from 3 to 2 +move 3 from 6 to 2 +move 1 from 6 to 2 +move 1 from 6 to 4 +move 3 from 1 to 3 +move 2 from 1 to 6 +move 35 from 2 to 1 +move 1 from 6 to 8 +move 5 from 1 to 8 +move 7 from 1 to 6 +move 5 from 3 to 7 +move 1 from 8 to 7 +move 3 from 7 to 5 +move 4 from 2 to 9 +move 1 from 2 to 1 +move 1 from 4 to 3 +move 3 from 7 to 1 +move 1 from 3 to 6 +move 1 from 1 to 9 +move 5 from 9 to 2 +move 18 from 1 to 3 +move 6 from 1 to 8 +move 6 from 3 to 7 +move 4 from 8 to 6 +move 4 from 6 to 7 +move 9 from 7 to 8 +move 3 from 2 to 7 +move 4 from 6 to 1 +move 3 from 5 to 3 +move 3 from 2 to 5 +move 3 from 6 to 1 +move 4 from 7 to 4 +move 6 from 5 to 9 +move 3 from 1 to 9 +move 1 from 6 to 1 +move 15 from 8 to 2 +move 1 from 8 to 5 +move 3 from 4 to 8 +move 1 from 5 to 1 +move 1 from 6 to 5 +move 11 from 3 to 9 +move 12 from 2 to 3 +move 3 from 8 to 1 +move 15 from 1 to 2 +move 8 from 9 to 4 +move 8 from 4 to 9 +move 4 from 2 to 5 +move 1 from 4 to 6 +move 1 from 2 to 8 +move 1 from 6 to 7 +move 4 from 3 to 1 +move 1 from 8 to 5 +move 5 from 3 to 9 +move 14 from 9 to 2 +move 1 from 7 to 4 +move 4 from 1 to 3 +move 1 from 4 to 7 +move 8 from 3 to 7 +move 8 from 7 to 5 +move 1 from 7 to 9 +move 3 from 3 to 2 +move 7 from 9 to 8 +move 1 from 9 to 5 +move 2 from 8 to 5 +move 7 from 5 to 4 +move 4 from 9 to 2 +move 6 from 4 to 3 +move 18 from 2 to 5 +move 1 from 4 to 7 +move 15 from 5 to 4 +move 1 from 4 to 6 +move 2 from 2 to 7 +move 3 from 8 to 5 +move 1 from 7 to 3 +move 8 from 2 to 6 +move 4 from 2 to 3 +move 1 from 7 to 5 +move 3 from 4 to 6 +move 5 from 6 to 9 +move 8 from 5 to 6 +move 2 from 4 to 3 +move 7 from 4 to 2 +move 2 from 8 to 5 +move 7 from 5 to 6 +move 3 from 5 to 8 +move 1 from 8 to 9 +move 13 from 3 to 8 +move 2 from 2 to 7 +move 9 from 8 to 9 +move 6 from 8 to 5 +move 5 from 5 to 2 +move 2 from 7 to 8 +move 9 from 2 to 5 +move 1 from 7 to 5 +move 1 from 5 to 7 +move 21 from 6 to 2 +move 1 from 7 to 8 +move 3 from 8 to 9 +move 1 from 4 to 2 +move 23 from 2 to 7 +move 8 from 9 to 8 +move 20 from 7 to 4 +move 3 from 7 to 2 +move 1 from 2 to 7 +move 1 from 6 to 7 +move 3 from 5 to 4 +move 8 from 5 to 9 +move 2 from 7 to 1 +move 1 from 8 to 7 +move 4 from 2 to 4 +move 2 from 8 to 7 +move 2 from 8 to 2 +move 1 from 7 to 6 +move 3 from 9 to 7 +move 2 from 2 to 7 +move 5 from 7 to 1 +move 8 from 9 to 6 +move 15 from 4 to 3 +move 4 from 4 to 7 +move 6 from 1 to 4 +move 11 from 3 to 4 +move 8 from 6 to 1 +move 24 from 4 to 7 +move 6 from 1 to 8 +move 27 from 7 to 3 +move 2 from 7 to 8 +move 5 from 8 to 3 +move 4 from 8 to 4 +move 1 from 8 to 6 +move 1 from 6 to 9 +move 1 from 6 to 5 +move 2 from 4 to 2 +move 1 from 8 to 1 +move 1 from 5 to 2 +move 4 from 1 to 6 +move 1 from 7 to 5 +move 1 from 5 to 8 +move 1 from 8 to 7 +move 1 from 7 to 8 +move 1 from 8 to 1 +move 1 from 2 to 3 +move 2 from 4 to 8 +move 7 from 9 to 6 +move 2 from 8 to 1 +move 3 from 3 to 8 +move 3 from 1 to 8 +move 2 from 2 to 3 +move 1 from 4 to 1 +move 1 from 1 to 8 +move 5 from 8 to 3 +move 8 from 6 to 2 +move 1 from 9 to 4 +move 2 from 4 to 8 +move 2 from 8 to 3 +move 2 from 6 to 2 +move 33 from 3 to 2 +move 2 from 8 to 7 +move 1 from 6 to 1 +move 1 from 1 to 7 +move 2 from 3 to 8 +move 2 from 8 to 4 +move 1 from 4 to 8 +move 2 from 7 to 2 +move 2 from 3 to 7 +move 12 from 2 to 1 +move 1 from 8 to 4 +move 1 from 4 to 8 +move 1 from 4 to 3 +move 1 from 8 to 2 +move 3 from 7 to 2 +move 37 from 2 to 7 +move 1 from 1 to 7 +move 12 from 7 to 1 +move 13 from 1 to 7 +move 1 from 3 to 4 +move 35 from 7 to 6 +move 1 from 4 to 5 +move 3 from 7 to 4 +move 1 from 5 to 7 +move 2 from 3 to 4 +move 23 from 6 to 9 +move 3 from 1 to 5 +move 3 from 3 to 7 +move 1 from 3 to 6 +move 2 from 5 to 3 +move 23 from 9 to 8 +move 2 from 4 to 9 +move 16 from 8 to 2 +move 2 from 7 to 3 +move 1 from 5 to 8 +move 3 from 7 to 6 +move 1 from 9 to 8 +move 3 from 8 to 1 +move 1 from 9 to 1 +move 11 from 6 to 5 +move 2 from 4 to 1 +move 4 from 8 to 6 +move 16 from 2 to 3 +move 9 from 1 to 9 +move 1 from 8 to 4 +move 3 from 9 to 3 +move 1 from 1 to 4 +move 1 from 9 to 4 +move 7 from 5 to 2 +move 6 from 2 to 5 +move 1 from 8 to 6 +move 22 from 3 to 7 +move 8 from 5 to 8 +move 4 from 4 to 9 +move 2 from 1 to 8 +move 16 from 7 to 2 +move 1 from 3 to 5 +move 14 from 2 to 7 +move 2 from 2 to 4 +move 6 from 9 to 3 diff --git a/2022/5/src/main.rs b/2022/5/src/main.rs new file mode 100644 index 0000000..3da5f7a --- /dev/null +++ b/2022/5/src/main.rs @@ -0,0 +1,69 @@ +use std::str::FromStr; + +fn main() { + let input_file = common::parse_args_input_file(&mut std::env::args()); + let contents = std::fs::read_to_string(input_file).expect("Couldn't read contents of input file"); + let mut stacks = get_initial_stacks(); + let mut stacks2 = get_initial_stacks(); + for line in contents.lines() { + if line.eq("") { + continue; + } + let words: Vec<&str> = line.split(' ').collect(); + assert_eq!(words.len(), 6); + let mut count = usize::from_str(words[1]).unwrap(); + let mut part2_count = count; + let from = usize::from_str(words[3]).unwrap() - 1; + let to = usize::from_str(words[5]).unwrap() - 1; + // Part 1 - CrateMover 9000 + while count > 0 { + count -= 1; + let x = stacks[from].pop(); + if x.is_none() { + println!("Breaking operation early, source stack empty: {} items from {} to {}", count, from, to); + break; + } + else { + stacks[to].push(x.unwrap()); + } + } + // Part 2 - CrateMover 9001, now with more leather. + // println!("Moving {} from stack: {:?}", part2_count, stacks2[from]); + if part2_count > stacks2[from].len() { + part2_count = stacks2[from].len(); + } + let split_at = stacks2[from].len() - part2_count; + let mut clone = stacks2[from].clone(); + let (remainder, moved) = clone.split_at_mut(split_at); + stacks2[from] = remainder.to_vec(); + for x in moved { + stacks2[to].push(*x); + } + // println!("Source Stack '{}': {:?}", from, stacks2[from]); + // println!("Destination Stack '{}': {:?}", to, stacks2[to]); + } + let mut result = String::with_capacity(stacks.len()); + for stack in stacks { + result.push(stack[stack.len()-1]); + } + let mut result2 = String::with_capacity(stacks2.len()); + for stack in stacks2 { + result2.push(stack[stack.len()-1]); + } + println!("[PART 1] Top bins in stacks are: {}", result); + println!("[PART 2] Top bins in stacks are: {}", result2); +} + +fn get_initial_stacks() -> std::vec::Vec::> { + let mut stacks = std::vec::Vec::>::new(); + stacks.push(vec!['Z', 'J', 'G']); + stacks.push(vec!['Q', 'L', 'R', 'P', 'W', 'F', 'V', 'C']); + stacks.push(vec!['F', 'P', 'M', 'C', 'L', 'G', 'R']); + stacks.push(vec!['L', 'F', 'B', 'W', 'P', 'H', 'M']); + stacks.push(vec!['G', 'C', 'F', 'S', 'V', 'Q']); + stacks.push(vec!['W', 'H', 'J', 'Z', 'M', 'Q', 'T', 'L']); + stacks.push(vec!['H', 'F', 'S', 'B', 'V']); + stacks.push(vec!['F', 'J', 'Z', 'S']); + stacks.push(vec!['M', 'C', 'D', 'P', 'F', 'H', 'B', 'T']); + return stacks; +}