From dbc711e50d67a016d85e39349f37ddf619b09418 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 5 Dec 2022 18:17:25 -0800 Subject: [PATCH] Day 5, Part 1! --- 2022/Data/day5-input.txt | 512 +++++++++++++++++++++++++++++++++++++++ 2022/aoc2022/src/day5.rs | 133 ++++++++++ 2022/aoc2022/src/main.rs | 6 + 3 files changed, 651 insertions(+) create mode 100644 2022/Data/day5-input.txt create mode 100644 2022/aoc2022/src/day5.rs diff --git a/2022/Data/day5-input.txt b/2022/Data/day5-input.txt new file mode 100644 index 0000000..7f69557 --- /dev/null +++ b/2022/Data/day5-input.txt @@ -0,0 +1,512 @@ + [M] [Z] [V] + [Z] [P] [L] [Z] [J] +[S] [D] [W] [W] [H] [Q] +[P] [V] [N] [D] [P] [C] [V] +[H] [B] [J] [V] [B] [M] [N] [P] +[V] [F] [L] [Z] [C] [S] [P] [S] [G] +[F] [J] [M] [G] [R] [R] [H] [R] [L] +[G] [G] [G] [N] [V] [V] [T] [Q] [F] + 1 2 3 4 5 6 7 8 9 + +move 6 from 9 to 3 +move 2 from 2 to 1 +move 1 from 8 to 2 +move 3 from 7 to 2 +move 7 from 6 to 9 +move 1 from 9 to 5 +move 3 from 5 to 7 +move 6 from 8 to 6 +move 1 from 7 to 8 +move 6 from 6 to 5 +move 4 from 5 to 8 +move 9 from 2 to 9 +move 1 from 2 to 3 +move 3 from 1 to 3 +move 3 from 5 to 1 +move 10 from 3 to 5 +move 4 from 4 to 6 +move 2 from 7 to 6 +move 2 from 6 to 9 +move 6 from 8 to 6 +move 1 from 4 to 3 +move 1 from 4 to 5 +move 1 from 4 to 1 +move 2 from 3 to 1 +move 1 from 3 to 7 +move 8 from 1 to 9 +move 1 from 1 to 2 +move 1 from 2 to 7 +move 6 from 6 to 3 +move 7 from 3 to 5 +move 14 from 5 to 6 +move 2 from 1 to 3 +move 5 from 5 to 8 +move 5 from 8 to 1 +move 2 from 7 to 1 +move 5 from 6 to 9 +move 8 from 9 to 3 +move 13 from 9 to 3 +move 7 from 1 to 4 +move 6 from 4 to 1 +move 22 from 3 to 1 +move 1 from 9 to 3 +move 2 from 6 to 1 +move 1 from 3 to 4 +move 7 from 9 to 8 +move 2 from 1 to 7 +move 2 from 3 to 2 +move 2 from 6 to 9 +move 2 from 7 to 8 +move 1 from 3 to 6 +move 9 from 8 to 6 +move 1 from 2 to 4 +move 8 from 1 to 2 +move 1 from 9 to 4 +move 3 from 4 to 1 +move 1 from 4 to 6 +move 10 from 6 to 5 +move 5 from 2 to 9 +move 6 from 9 to 3 +move 2 from 5 to 3 +move 2 from 9 to 7 +move 7 from 5 to 8 +move 5 from 6 to 2 +move 3 from 3 to 7 +move 3 from 3 to 5 +move 4 from 5 to 8 +move 1 from 3 to 5 +move 6 from 6 to 8 +move 1 from 5 to 7 +move 9 from 8 to 9 +move 1 from 3 to 1 +move 7 from 2 to 7 +move 9 from 7 to 6 +move 2 from 2 to 3 +move 7 from 9 to 3 +move 9 from 6 to 8 +move 7 from 3 to 4 +move 2 from 7 to 6 +move 4 from 4 to 5 +move 3 from 5 to 6 +move 2 from 7 to 4 +move 5 from 4 to 7 +move 13 from 8 to 4 +move 2 from 9 to 4 +move 2 from 8 to 7 +move 6 from 7 to 5 +move 6 from 4 to 2 +move 1 from 7 to 5 +move 3 from 2 to 7 +move 1 from 7 to 8 +move 3 from 2 to 4 +move 2 from 3 to 9 +move 2 from 7 to 2 +move 6 from 5 to 4 +move 3 from 6 to 2 +move 2 from 6 to 9 +move 5 from 2 to 9 +move 12 from 4 to 8 +move 3 from 9 to 2 +move 12 from 1 to 5 +move 4 from 4 to 6 +move 12 from 8 to 9 +move 2 from 6 to 5 +move 1 from 4 to 8 +move 1 from 4 to 1 +move 3 from 2 to 1 +move 2 from 6 to 7 +move 1 from 5 to 9 +move 2 from 1 to 4 +move 10 from 5 to 1 +move 2 from 7 to 3 +move 18 from 9 to 7 +move 8 from 7 to 2 +move 1 from 9 to 6 +move 1 from 6 to 7 +move 10 from 7 to 9 +move 1 from 4 to 2 +move 19 from 1 to 5 +move 8 from 5 to 9 +move 3 from 8 to 4 +move 2 from 5 to 2 +move 2 from 3 to 6 +move 10 from 5 to 2 +move 4 from 1 to 2 +move 2 from 9 to 2 +move 1 from 1 to 6 +move 2 from 5 to 6 +move 1 from 8 to 7 +move 1 from 5 to 8 +move 1 from 5 to 6 +move 18 from 2 to 5 +move 5 from 2 to 1 +move 6 from 5 to 8 +move 1 from 8 to 9 +move 2 from 2 to 4 +move 1 from 2 to 6 +move 2 from 7 to 6 +move 1 from 4 to 1 +move 4 from 8 to 5 +move 1 from 2 to 9 +move 2 from 8 to 3 +move 1 from 3 to 6 +move 1 from 4 to 8 +move 1 from 8 to 9 +move 10 from 5 to 7 +move 5 from 5 to 1 +move 2 from 4 to 1 +move 3 from 7 to 6 +move 12 from 1 to 4 +move 8 from 9 to 5 +move 6 from 7 to 4 +move 1 from 7 to 9 +move 4 from 4 to 3 +move 1 from 1 to 7 +move 3 from 9 to 5 +move 2 from 3 to 1 +move 1 from 7 to 6 +move 8 from 4 to 7 +move 1 from 7 to 6 +move 7 from 6 to 4 +move 2 from 1 to 3 +move 1 from 7 to 1 +move 1 from 3 to 7 +move 1 from 1 to 6 +move 4 from 9 to 3 +move 5 from 4 to 6 +move 12 from 6 to 2 +move 3 from 9 to 4 +move 8 from 2 to 6 +move 2 from 9 to 6 +move 8 from 5 to 6 +move 4 from 5 to 8 +move 14 from 6 to 3 +move 11 from 4 to 9 +move 2 from 2 to 7 +move 8 from 3 to 9 +move 11 from 3 to 6 +move 14 from 9 to 1 +move 7 from 1 to 3 +move 2 from 9 to 5 +move 2 from 2 to 8 +move 6 from 7 to 5 +move 1 from 9 to 8 +move 13 from 6 to 3 +move 4 from 6 to 8 +move 3 from 1 to 6 +move 5 from 5 to 8 +move 7 from 8 to 7 +move 2 from 1 to 8 +move 1 from 4 to 1 +move 4 from 8 to 9 +move 8 from 7 to 5 +move 1 from 8 to 1 +move 4 from 9 to 3 +move 1 from 4 to 5 +move 5 from 5 to 2 +move 1 from 8 to 9 +move 1 from 8 to 6 +move 2 from 6 to 2 +move 4 from 8 to 6 +move 4 from 1 to 8 +move 4 from 8 to 5 +move 1 from 9 to 8 +move 1 from 2 to 3 +move 4 from 6 to 1 +move 1 from 8 to 2 +move 3 from 5 to 4 +move 4 from 2 to 5 +move 1 from 7 to 9 +move 1 from 2 to 6 +move 3 from 1 to 8 +move 2 from 4 to 5 +move 2 from 6 to 1 +move 3 from 8 to 9 +move 4 from 9 to 2 +move 1 from 7 to 1 +move 1 from 6 to 7 +move 4 from 1 to 6 +move 1 from 7 to 4 +move 6 from 2 to 8 +move 2 from 4 to 8 +move 1 from 9 to 5 +move 3 from 6 to 2 +move 1 from 6 to 4 +move 7 from 3 to 5 +move 2 from 8 to 1 +move 3 from 2 to 8 +move 6 from 8 to 5 +move 17 from 5 to 3 +move 2 from 1 to 6 +move 3 from 8 to 3 +move 1 from 9 to 5 +move 11 from 5 to 2 +move 40 from 3 to 5 +move 11 from 2 to 7 +move 4 from 7 to 8 +move 1 from 8 to 9 +move 1 from 3 to 5 +move 1 from 4 to 8 +move 19 from 5 to 8 +move 7 from 7 to 8 +move 16 from 5 to 2 +move 6 from 5 to 8 +move 1 from 5 to 8 +move 1 from 9 to 4 +move 1 from 6 to 1 +move 1 from 4 to 7 +move 1 from 6 to 9 +move 1 from 1 to 7 +move 1 from 7 to 3 +move 1 from 7 to 2 +move 1 from 9 to 8 +move 1 from 3 to 4 +move 1 from 4 to 6 +move 14 from 2 to 9 +move 24 from 8 to 4 +move 8 from 8 to 3 +move 1 from 6 to 3 +move 16 from 4 to 1 +move 3 from 8 to 4 +move 3 from 3 to 8 +move 4 from 3 to 4 +move 1 from 3 to 9 +move 13 from 9 to 4 +move 16 from 1 to 8 +move 8 from 8 to 1 +move 3 from 1 to 7 +move 1 from 8 to 6 +move 1 from 3 to 8 +move 10 from 8 to 5 +move 5 from 5 to 2 +move 3 from 8 to 9 +move 1 from 8 to 9 +move 1 from 4 to 5 +move 5 from 2 to 6 +move 3 from 5 to 2 +move 1 from 6 to 1 +move 5 from 1 to 5 +move 1 from 1 to 5 +move 2 from 7 to 3 +move 2 from 3 to 2 +move 1 from 5 to 7 +move 7 from 5 to 3 +move 5 from 9 to 5 +move 2 from 7 to 9 +move 4 from 5 to 6 +move 2 from 9 to 8 +move 2 from 2 to 4 +move 5 from 3 to 5 +move 1 from 3 to 2 +move 7 from 4 to 9 +move 1 from 8 to 1 +move 1 from 2 to 1 +move 9 from 4 to 6 +move 2 from 1 to 8 +move 1 from 3 to 9 +move 2 from 8 to 6 +move 13 from 4 to 6 +move 1 from 8 to 7 +move 2 from 9 to 6 +move 3 from 5 to 7 +move 3 from 2 to 5 +move 3 from 2 to 6 +move 5 from 6 to 2 +move 4 from 2 to 5 +move 4 from 5 to 7 +move 5 from 5 to 7 +move 7 from 9 to 6 +move 6 from 7 to 2 +move 22 from 6 to 5 +move 10 from 5 to 8 +move 7 from 5 to 4 +move 8 from 8 to 5 +move 18 from 6 to 2 +move 5 from 7 to 5 +move 1 from 8 to 2 +move 6 from 5 to 1 +move 7 from 4 to 2 +move 4 from 1 to 5 +move 1 from 7 to 9 +move 1 from 8 to 6 +move 1 from 7 to 8 +move 10 from 5 to 9 +move 12 from 2 to 1 +move 8 from 5 to 2 +move 19 from 2 to 9 +move 1 from 6 to 8 +move 13 from 9 to 3 +move 8 from 1 to 2 +move 5 from 1 to 3 +move 10 from 2 to 1 +move 7 from 2 to 5 +move 3 from 5 to 7 +move 4 from 1 to 3 +move 1 from 2 to 3 +move 3 from 1 to 2 +move 1 from 8 to 6 +move 2 from 7 to 5 +move 4 from 1 to 3 +move 6 from 5 to 4 +move 2 from 2 to 1 +move 1 from 2 to 9 +move 6 from 4 to 5 +move 5 from 5 to 9 +move 1 from 6 to 8 +move 1 from 5 to 1 +move 6 from 9 to 2 +move 5 from 2 to 4 +move 3 from 1 to 6 +move 2 from 4 to 7 +move 22 from 3 to 9 +move 1 from 8 to 4 +move 2 from 4 to 3 +move 2 from 6 to 1 +move 2 from 1 to 5 +move 1 from 6 to 7 +move 1 from 7 to 4 +move 6 from 3 to 7 +move 1 from 2 to 4 +move 8 from 7 to 3 +move 1 from 4 to 5 +move 1 from 7 to 9 +move 5 from 3 to 6 +move 1 from 8 to 4 +move 4 from 3 to 2 +move 32 from 9 to 3 +move 3 from 6 to 7 +move 5 from 9 to 3 +move 1 from 9 to 7 +move 2 from 9 to 2 +move 2 from 4 to 3 +move 2 from 5 to 4 +move 5 from 3 to 2 +move 3 from 7 to 8 +move 1 from 7 to 2 +move 1 from 8 to 5 +move 1 from 3 to 4 +move 5 from 4 to 5 +move 4 from 5 to 2 +move 3 from 5 to 7 +move 1 from 7 to 5 +move 1 from 6 to 5 +move 2 from 8 to 5 +move 15 from 2 to 4 +move 3 from 5 to 6 +move 4 from 6 to 5 +move 2 from 5 to 2 +move 1 from 2 to 4 +move 25 from 3 to 9 +move 2 from 5 to 2 +move 11 from 9 to 2 +move 13 from 2 to 1 +move 4 from 4 to 7 +move 12 from 9 to 8 +move 6 from 7 to 8 +move 7 from 4 to 7 +move 7 from 7 to 8 +move 1 from 5 to 1 +move 5 from 4 to 3 +move 2 from 2 to 1 +move 2 from 9 to 5 +move 7 from 1 to 7 +move 1 from 1 to 4 +move 12 from 3 to 2 +move 1 from 3 to 9 +move 1 from 1 to 3 +move 1 from 9 to 1 +move 7 from 7 to 2 +move 1 from 4 to 7 +move 2 from 8 to 7 +move 7 from 1 to 2 +move 1 from 3 to 4 +move 26 from 2 to 1 +move 4 from 8 to 1 +move 3 from 1 to 6 +move 1 from 6 to 3 +move 1 from 6 to 9 +move 1 from 3 to 8 +move 20 from 1 to 3 +move 1 from 9 to 7 +move 4 from 7 to 1 +move 1 from 5 to 3 +move 4 from 3 to 5 +move 1 from 6 to 2 +move 6 from 3 to 2 +move 8 from 1 to 4 +move 1 from 1 to 5 +move 3 from 1 to 4 +move 7 from 2 to 4 +move 10 from 3 to 8 +move 4 from 4 to 3 +move 12 from 4 to 7 +move 3 from 3 to 1 +move 2 from 4 to 3 +move 2 from 8 to 1 +move 6 from 8 to 9 +move 5 from 9 to 6 +move 1 from 9 to 3 +move 3 from 8 to 7 +move 10 from 8 to 5 +move 4 from 8 to 7 +move 9 from 7 to 9 +move 4 from 8 to 4 +move 2 from 4 to 3 +move 3 from 1 to 7 +move 11 from 7 to 4 +move 6 from 4 to 8 +move 1 from 7 to 3 +move 4 from 5 to 1 +move 5 from 3 to 6 +move 5 from 9 to 4 +move 1 from 9 to 8 +move 10 from 4 to 8 +move 5 from 1 to 2 +move 1 from 7 to 6 +move 9 from 6 to 3 +move 7 from 8 to 7 +move 3 from 4 to 1 +move 2 from 2 to 1 +move 9 from 8 to 3 +move 10 from 5 to 8 +move 18 from 3 to 9 +move 1 from 7 to 8 +move 1 from 5 to 3 +move 4 from 8 to 3 +move 2 from 6 to 3 +move 6 from 7 to 2 +move 1 from 5 to 3 +move 1 from 1 to 9 +move 10 from 3 to 9 +move 4 from 1 to 8 +move 13 from 8 to 1 +move 3 from 1 to 8 +move 3 from 2 to 4 +move 5 from 2 to 6 +move 5 from 6 to 4 +move 28 from 9 to 2 +move 2 from 9 to 5 +move 2 from 5 to 2 +move 1 from 3 to 7 +move 2 from 1 to 4 +move 3 from 8 to 3 +move 1 from 9 to 4 +move 3 from 4 to 6 +move 2 from 3 to 7 +move 8 from 1 to 5 +move 3 from 7 to 6 +move 14 from 2 to 8 +move 1 from 9 to 1 +move 6 from 5 to 6 +move 4 from 2 to 5 +move 9 from 8 to 2 +move 4 from 8 to 4 +move 7 from 2 to 4 +move 12 from 4 to 3 +move 5 from 4 to 7 +move 5 from 7 to 4 +move 1 from 8 to 7 +move 1 from 4 to 5 +move 2 from 5 to 4 +move 1 from 5 to 8 +move 1 from 5 to 9 diff --git a/2022/aoc2022/src/day5.rs b/2022/aoc2022/src/day5.rs new file mode 100644 index 0000000..3bde6c3 --- /dev/null +++ b/2022/aoc2022/src/day5.rs @@ -0,0 +1,133 @@ +use std::fmt; +use std::io::Result; + +use crate::file::line_reader_for_file; + +#[derive(PartialEq)] +enum State { + StartingState, + Instructions +} + +#[derive(Debug)] +struct Stacks(Vec>); + +impl Stacks { + fn perform(&mut self, instruction: Instruction) -> std::result::Result<(), &'static str> { + for _ in 0..instruction.quantity { + let item = self.0[instruction.from_stack].pop().unwrap(); + self.0[instruction.to_stack].push(item); + } + Ok(()) + } + + fn tops(&self) -> impl Iterator { + self.0.iter().filter_map(|s| s.last()).map(|s| s.as_str()) + } +} + +impl fmt::Display for Stacks { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0.iter().enumerate() + .map(|(i, v)| format!("{}: {}", i, v.join(", "))) + .collect::>() + .join("\n")) + } +} + +#[derive(Debug)] +struct Instruction { + quantity: usize, + from_stack: usize, + to_stack: usize +} + +impl TryFrom<&str> for Instruction { + type Error = &'static str; + + fn try_from(s: &str) -> std::result::Result { + let quantity: usize; + let from_stack: usize; + let to_stack: usize; + + let mut split_str = s.split(" "); + + if split_str.next() != Some("move") { + return Err("Missing 'move'"); + } + + match split_str.next().map(|s| usize::from_str_radix(s, 10)) { + Some(Ok(parsed_quantity)) => quantity = parsed_quantity, + _ => return Err("Missing quantity value"), + } + + if split_str.next() != Some("from") { + return Err("Missing 'from'"); + } + + match split_str.next().map(|s| usize::from_str_radix(s, 10)) { + Some(Ok(parsed_from_stack)) => from_stack = parsed_from_stack - 1, + _ => return Err("Missing from value"), + } + + if split_str.next() != Some("to") { + return Err("Missing 'to'"); + } + + match split_str.next().map(|s| usize::from_str_radix(s, 10)) { + Some(Ok(parsed_to_stack)) => to_stack = parsed_to_stack - 1, + _ => return Err("Missing to value"), + } + + Ok(Instruction { + quantity: quantity, + from_stack: from_stack, + to_stack: to_stack, + }) + } +} + +pub fn main(filename: &str) -> Result<()> { + let mut line_reader = line_reader_for_file(filename)?.peekable(); + + let first_line = line_reader.peek().unwrap().as_ref().unwrap(); + let number_of_stacks = (first_line.len() as f32 / 4.0).ceil() as usize; + let mut stacks = Stacks(vec![vec![]; number_of_stacks]); + + let mut state = State::StartingState; + + for line in line_reader { + let line = line?; + + if line.is_empty() { + assert!(state == State::StartingState); + state = State::Instructions; + continue; + } + + match state { + State::StartingState => { + let mut chars = line.chars().peekable(); + let mut index_of_stack = 0; + while chars.peek() != None { + // Read the line in 4 character chunks. + let stack: Vec = chars.by_ref().take(4).collect(); + if stack[0] == '[' { + stacks.0[index_of_stack].insert(0, String::from(stack[1])); + } + + index_of_stack += 1; + } + }, + State::Instructions => { + let instruction = Instruction::try_from(line.as_str()).unwrap(); + let _ = stacks.perform(instruction); + }, + } + } + + println!("{}", stacks); + println!("Part 1: tops of stacks: {}", stacks.tops().collect::>().join("")); + + Ok(()) +} diff --git a/2022/aoc2022/src/main.rs b/2022/aoc2022/src/main.rs index 8da1bc2..a105ab9 100644 --- a/2022/aoc2022/src/main.rs +++ b/2022/aoc2022/src/main.rs @@ -4,6 +4,7 @@ mod day1; mod day2; mod day3; mod day4; +mod day5; mod file; fn main() { @@ -30,4 +31,9 @@ fn main() { let day4_datafile = &args[4]; day4::main(day4_datafile.as_str()) .expect("Unable to process day4 data file"); + + println!("----- Day 5 -----"); + let day5_datafile = &args[5]; + day5::main(day5_datafile.as_str()) + .expect("Unable to process day5 data file"); }