From fc6ffaf27618f6647c271a3c16a5319d7af4a4cb Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 6 Dec 2022 09:53:00 -0500 Subject: [PATCH] Complete day 5 part 1 --- day5/Cargo.lock | 7 + day5/Cargo.toml | 8 + day5/src/input.txt | 514 +++++++++++++++++++++++++++++++++++++++++++++ day5/src/main.rs | 71 +++++++ 4 files changed, 600 insertions(+) create mode 100644 day5/Cargo.lock create mode 100644 day5/Cargo.toml create mode 100644 day5/src/input.txt create mode 100644 day5/src/main.rs diff --git a/day5/Cargo.lock b/day5/Cargo.lock new file mode 100644 index 0000000..7a62bb0 --- /dev/null +++ b/day5/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day5" +version = "0.1.0" diff --git a/day5/Cargo.toml b/day5/Cargo.toml new file mode 100644 index 0000000..f9b62bf --- /dev/null +++ b/day5/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/day5/src/input.txt b/day5/src/input.txt new file mode 100644 index 0000000..506114e --- /dev/null +++ b/day5/src/input.txt @@ -0,0 +1,514 @@ + [Q] [P] [P] + [G] [V] [S] [Z] [F] + [W] [V] [F] [Z] [W] [Q] + [V] [T] [N] [J] [W] [B] [W] + [Z] [L] [V] [B] [C] [R] [N] [M] +[C] [W] [R] [H] [H] [P] [T] [M] [B] +[Q] [Q] [M] [Z] [Z] [N] [G] [G] [J] +[B] [R] [B] [C] [D] [H] [D] [C] [N] + 1 2 3 4 5 6 7 8 9 + +move 3 from 6 to 2 +move 5 from 6 to 7 +move 6 from 2 to 5 +move 1 from 9 to 7 +move 1 from 1 to 9 +move 1 from 5 to 3 +move 1 from 2 to 5 +move 3 from 4 to 5 +move 10 from 7 to 3 +move 1 from 4 to 9 +move 6 from 8 to 7 +move 4 from 7 to 8 +move 1 from 7 to 3 +move 1 from 1 to 2 +move 1 from 2 to 8 +move 1 from 9 to 1 +move 3 from 9 to 4 +move 4 from 8 to 3 +move 4 from 7 to 1 +move 4 from 4 to 6 +move 2 from 8 to 7 +move 9 from 3 to 8 +move 2 from 7 to 4 +move 3 from 4 to 9 +move 4 from 1 to 9 +move 4 from 3 to 9 +move 2 from 1 to 4 +move 1 from 4 to 6 +move 3 from 3 to 2 +move 1 from 2 to 8 +move 1 from 2 to 7 +move 3 from 6 to 2 +move 2 from 6 to 7 +move 4 from 2 to 3 +move 3 from 7 to 9 +move 2 from 5 to 6 +move 15 from 9 to 4 +move 4 from 9 to 2 +move 12 from 5 to 4 +move 9 from 8 to 5 +move 25 from 4 to 7 +move 1 from 4 to 7 +move 1 from 4 to 8 +move 2 from 2 to 5 +move 1 from 4 to 2 +move 23 from 7 to 6 +move 2 from 5 to 2 +move 22 from 6 to 8 +move 4 from 5 to 9 +move 1 from 7 to 9 +move 2 from 6 to 4 +move 2 from 4 to 7 +move 25 from 8 to 3 +move 1 from 2 to 1 +move 3 from 2 to 3 +move 1 from 6 to 8 +move 1 from 1 to 8 +move 1 from 2 to 8 +move 1 from 8 to 1 +move 4 from 5 to 7 +move 1 from 8 to 4 +move 5 from 9 to 8 +move 5 from 8 to 9 +move 1 from 8 to 5 +move 3 from 5 to 4 +move 3 from 9 to 1 +move 30 from 3 to 4 +move 3 from 1 to 4 +move 2 from 9 to 5 +move 4 from 7 to 9 +move 16 from 4 to 8 +move 6 from 3 to 9 +move 3 from 7 to 3 +move 19 from 4 to 7 +move 8 from 9 to 4 +move 1 from 1 to 9 +move 13 from 7 to 9 +move 3 from 7 to 8 +move 3 from 5 to 9 +move 4 from 8 to 3 +move 2 from 7 to 3 +move 14 from 9 to 4 +move 10 from 3 to 1 +move 12 from 4 to 8 +move 6 from 1 to 9 +move 1 from 1 to 2 +move 1 from 7 to 1 +move 6 from 9 to 3 +move 17 from 8 to 6 +move 10 from 8 to 5 +move 1 from 7 to 8 +move 1 from 9 to 5 +move 2 from 3 to 1 +move 4 from 5 to 9 +move 1 from 8 to 7 +move 6 from 9 to 7 +move 4 from 4 to 2 +move 3 from 4 to 6 +move 4 from 5 to 9 +move 4 from 9 to 3 +move 1 from 2 to 4 +move 4 from 4 to 7 +move 3 from 5 to 3 +move 1 from 4 to 5 +move 5 from 1 to 2 +move 1 from 1 to 9 +move 7 from 2 to 7 +move 1 from 5 to 7 +move 8 from 3 to 5 +move 20 from 6 to 7 +move 9 from 7 to 9 +move 2 from 2 to 9 +move 2 from 3 to 1 +move 2 from 1 to 3 +move 2 from 3 to 4 +move 2 from 4 to 6 +move 1 from 3 to 9 +move 1 from 4 to 9 +move 1 from 6 to 9 +move 2 from 5 to 8 +move 2 from 8 to 5 +move 1 from 6 to 7 +move 2 from 5 to 8 +move 6 from 9 to 5 +move 2 from 8 to 6 +move 11 from 9 to 2 +move 1 from 6 to 5 +move 11 from 2 to 5 +move 1 from 6 to 4 +move 7 from 5 to 9 +move 7 from 9 to 1 +move 1 from 4 to 9 +move 28 from 7 to 5 +move 1 from 7 to 5 +move 5 from 5 to 9 +move 5 from 9 to 3 +move 6 from 1 to 8 +move 1 from 1 to 7 +move 5 from 3 to 2 +move 1 from 7 to 8 +move 7 from 8 to 1 +move 1 from 9 to 4 +move 2 from 2 to 5 +move 22 from 5 to 3 +move 1 from 7 to 8 +move 1 from 4 to 7 +move 1 from 8 to 9 +move 1 from 9 to 4 +move 14 from 5 to 7 +move 5 from 5 to 9 +move 19 from 3 to 4 +move 1 from 2 to 9 +move 2 from 2 to 5 +move 1 from 5 to 1 +move 6 from 1 to 7 +move 2 from 7 to 6 +move 1 from 1 to 9 +move 2 from 5 to 8 +move 8 from 4 to 5 +move 3 from 4 to 7 +move 3 from 3 to 5 +move 2 from 8 to 9 +move 16 from 7 to 5 +move 9 from 4 to 6 +move 22 from 5 to 3 +move 1 from 5 to 8 +move 1 from 8 to 7 +move 10 from 3 to 4 +move 1 from 5 to 4 +move 10 from 4 to 5 +move 8 from 5 to 2 +move 5 from 2 to 7 +move 5 from 7 to 1 +move 4 from 7 to 6 +move 3 from 9 to 7 +move 2 from 2 to 3 +move 3 from 5 to 1 +move 6 from 9 to 7 +move 5 from 7 to 8 +move 6 from 1 to 5 +move 6 from 3 to 4 +move 4 from 4 to 2 +move 1 from 4 to 6 +move 5 from 8 to 7 +move 3 from 2 to 3 +move 1 from 1 to 4 +move 1 from 1 to 9 +move 2 from 2 to 1 +move 2 from 4 to 3 +move 4 from 3 to 7 +move 3 from 7 to 3 +move 13 from 6 to 1 +move 1 from 9 to 2 +move 6 from 3 to 5 +move 8 from 1 to 4 +move 1 from 2 to 7 +move 9 from 4 to 9 +move 7 from 5 to 1 +move 2 from 5 to 6 +move 1 from 1 to 4 +move 1 from 4 to 3 +move 2 from 1 to 2 +move 5 from 3 to 6 +move 2 from 6 to 1 +move 13 from 7 to 6 +move 2 from 3 to 4 +move 2 from 2 to 9 +move 2 from 7 to 8 +move 6 from 9 to 2 +move 1 from 9 to 3 +move 1 from 5 to 2 +move 7 from 1 to 2 +move 1 from 6 to 7 +move 1 from 4 to 8 +move 1 from 3 to 1 +move 1 from 7 to 8 +move 7 from 1 to 9 +move 4 from 8 to 6 +move 1 from 5 to 3 +move 9 from 9 to 5 +move 1 from 1 to 2 +move 14 from 2 to 7 +move 2 from 9 to 3 +move 13 from 5 to 3 +move 24 from 6 to 9 +move 6 from 3 to 5 +move 14 from 7 to 9 +move 1 from 4 to 1 +move 20 from 9 to 7 +move 9 from 3 to 8 +move 15 from 9 to 6 +move 1 from 5 to 8 +move 1 from 2 to 3 +move 14 from 6 to 3 +move 2 from 3 to 4 +move 2 from 3 to 6 +move 13 from 7 to 1 +move 8 from 3 to 5 +move 1 from 3 to 9 +move 8 from 5 to 4 +move 4 from 5 to 2 +move 10 from 1 to 3 +move 6 from 4 to 5 +move 4 from 5 to 1 +move 3 from 1 to 6 +move 7 from 8 to 2 +move 4 from 4 to 3 +move 13 from 3 to 6 +move 3 from 8 to 1 +move 3 from 7 to 8 +move 3 from 8 to 4 +move 1 from 4 to 2 +move 2 from 3 to 4 +move 1 from 5 to 7 +move 4 from 7 to 1 +move 2 from 3 to 5 +move 3 from 2 to 1 +move 1 from 4 to 7 +move 7 from 2 to 4 +move 2 from 4 to 3 +move 1 from 7 to 5 +move 4 from 9 to 5 +move 1 from 4 to 2 +move 3 from 2 to 9 +move 8 from 1 to 7 +move 1 from 3 to 5 +move 7 from 5 to 7 +move 10 from 6 to 4 +move 1 from 5 to 1 +move 4 from 1 to 3 +move 9 from 7 to 6 +move 3 from 1 to 8 +move 12 from 4 to 6 +move 5 from 4 to 6 +move 2 from 9 to 3 +move 3 from 8 to 7 +move 1 from 1 to 3 +move 3 from 7 to 8 +move 5 from 7 to 5 +move 1 from 7 to 5 +move 2 from 3 to 1 +move 2 from 8 to 7 +move 3 from 5 to 1 +move 1 from 9 to 7 +move 1 from 8 to 3 +move 4 from 7 to 8 +move 4 from 5 to 9 +move 4 from 1 to 7 +move 3 from 8 to 6 +move 1 from 8 to 1 +move 1 from 7 to 1 +move 1 from 5 to 8 +move 1 from 8 to 7 +move 7 from 3 to 1 +move 3 from 9 to 1 +move 1 from 9 to 3 +move 28 from 6 to 3 +move 3 from 7 to 8 +move 2 from 8 to 2 +move 1 from 2 to 7 +move 2 from 6 to 1 +move 18 from 3 to 9 +move 5 from 3 to 4 +move 2 from 7 to 4 +move 2 from 1 to 8 +move 1 from 2 to 6 +move 7 from 6 to 4 +move 4 from 4 to 3 +move 3 from 8 to 1 +move 4 from 9 to 8 +move 1 from 4 to 8 +move 9 from 1 to 6 +move 5 from 1 to 3 +move 4 from 6 to 7 +move 7 from 6 to 3 +move 5 from 8 to 1 +move 12 from 3 to 6 +move 7 from 6 to 4 +move 4 from 3 to 5 +move 5 from 6 to 7 +move 12 from 4 to 3 +move 6 from 1 to 4 +move 4 from 4 to 2 +move 14 from 9 to 8 +move 17 from 3 to 2 +move 5 from 4 to 9 +move 1 from 9 to 6 +move 5 from 2 to 1 +move 1 from 9 to 8 +move 5 from 1 to 6 +move 2 from 2 to 6 +move 12 from 2 to 4 +move 6 from 7 to 2 +move 3 from 7 to 6 +move 3 from 9 to 8 +move 5 from 4 to 7 +move 4 from 2 to 6 +move 3 from 6 to 8 +move 5 from 8 to 2 +move 7 from 6 to 8 +move 1 from 7 to 3 +move 6 from 4 to 3 +move 1 from 8 to 1 +move 1 from 5 to 7 +move 2 from 6 to 8 +move 13 from 8 to 2 +move 3 from 5 to 4 +move 1 from 1 to 2 +move 3 from 6 to 2 +move 1 from 1 to 4 +move 4 from 4 to 8 +move 8 from 3 to 1 +move 2 from 4 to 8 +move 15 from 2 to 4 +move 16 from 8 to 3 +move 1 from 8 to 6 +move 1 from 7 to 2 +move 8 from 1 to 2 +move 1 from 6 to 8 +move 6 from 3 to 1 +move 3 from 3 to 8 +move 6 from 3 to 1 +move 6 from 2 to 9 +move 2 from 1 to 4 +move 1 from 8 to 5 +move 8 from 2 to 9 +move 8 from 1 to 4 +move 3 from 8 to 6 +move 21 from 4 to 7 +move 1 from 9 to 7 +move 2 from 6 to 8 +move 1 from 5 to 1 +move 1 from 3 to 9 +move 8 from 9 to 4 +move 1 from 1 to 7 +move 1 from 1 to 4 +move 1 from 6 to 8 +move 1 from 9 to 3 +move 2 from 9 to 5 +move 2 from 5 to 3 +move 1 from 9 to 4 +move 3 from 8 to 2 +move 1 from 1 to 4 +move 4 from 4 to 9 +move 3 from 3 to 2 +move 5 from 9 to 1 +move 17 from 7 to 1 +move 1 from 9 to 1 +move 2 from 2 to 4 +move 1 from 4 to 2 +move 8 from 2 to 9 +move 5 from 4 to 5 +move 6 from 4 to 8 +move 20 from 1 to 6 +move 2 from 9 to 8 +move 1 from 2 to 9 +move 2 from 8 to 7 +move 8 from 7 to 8 +move 4 from 5 to 9 +move 14 from 8 to 7 +move 1 from 5 to 7 +move 7 from 9 to 1 +move 3 from 6 to 4 +move 3 from 9 to 7 +move 12 from 6 to 7 +move 22 from 7 to 9 +move 2 from 2 to 5 +move 10 from 1 to 7 +move 1 from 4 to 1 +move 2 from 6 to 1 +move 1 from 1 to 3 +move 2 from 4 to 8 +move 2 from 8 to 6 +move 1 from 3 to 8 +move 1 from 4 to 1 +move 2 from 5 to 3 +move 1 from 8 to 4 +move 2 from 3 to 7 +move 19 from 9 to 7 +move 1 from 1 to 4 +move 2 from 9 to 1 +move 2 from 1 to 6 +move 1 from 6 to 5 +move 42 from 7 to 8 +move 1 from 7 to 6 +move 2 from 4 to 8 +move 7 from 6 to 8 +move 2 from 1 to 5 +move 2 from 9 to 5 +move 14 from 8 to 3 +move 22 from 8 to 2 +move 3 from 5 to 6 +move 10 from 8 to 6 +move 5 from 8 to 9 +move 12 from 6 to 7 +move 2 from 5 to 1 +move 5 from 3 to 2 +move 7 from 3 to 5 +move 2 from 5 to 1 +move 2 from 3 to 7 +move 4 from 1 to 2 +move 1 from 5 to 7 +move 1 from 5 to 4 +move 1 from 6 to 2 +move 1 from 9 to 2 +move 9 from 7 to 3 +move 1 from 4 to 1 +move 3 from 7 to 5 +move 4 from 3 to 2 +move 5 from 2 to 3 +move 2 from 5 to 2 +move 34 from 2 to 9 +move 1 from 1 to 5 +move 15 from 9 to 3 +move 2 from 3 to 2 +move 1 from 5 to 4 +move 7 from 3 to 8 +move 3 from 9 to 2 +move 6 from 9 to 4 +move 5 from 9 to 3 +move 4 from 4 to 6 +move 1 from 6 to 8 +move 1 from 3 to 5 +move 6 from 3 to 2 +move 1 from 4 to 9 +move 2 from 4 to 2 +move 4 from 5 to 8 +move 1 from 5 to 6 +move 1 from 7 to 6 +move 1 from 9 to 6 +move 1 from 7 to 2 +move 12 from 8 to 7 +move 2 from 7 to 3 +move 4 from 6 to 9 +move 7 from 9 to 4 +move 9 from 3 to 9 +move 11 from 7 to 4 +move 3 from 9 to 6 +move 1 from 4 to 1 +move 15 from 4 to 3 +move 2 from 4 to 1 +move 3 from 1 to 4 +move 17 from 3 to 7 +move 4 from 3 to 7 +move 7 from 9 to 2 +move 3 from 4 to 1 +move 4 from 6 to 9 +move 1 from 9 to 6 +move 1 from 3 to 1 +move 5 from 7 to 9 +move 8 from 9 to 4 +move 1 from 1 to 6 +move 6 from 4 to 9 +move 4 from 2 to 3 +move 1 from 4 to 3 +move 1 from 4 to 9 +move 1 from 1 to 7 +move 1 from 7 to 9 +move 3 from 6 to 2 +move 9 from 2 to 3 +move 1 from 9 to 4 +move 1 from 1 to 5 +move 12 from 7 to 6 +move 4 from 9 to 8 diff --git a/day5/src/main.rs b/day5/src/main.rs new file mode 100644 index 0000000..c8d14a4 --- /dev/null +++ b/day5/src/main.rs @@ -0,0 +1,71 @@ +struct Move { + items: usize, + from: usize, + to: usize, +} + +impl From<&str> for Move { + fn from(s: &str) -> Self { + let parts: Vec<&str> = s.split_ascii_whitespace().collect(); + let items: usize = parts[1].parse().unwrap(); + let from: usize = parts[3].parse().unwrap(); + let to: usize = parts[5].parse().unwrap(); + + Move { items, from, to } + } +} + +impl Move { + fn apply(&self, layout: &mut Vec>) { + for _ in 0..self.items { + if let Some(item) = layout[self.from].pop() { + layout[self.to].push(item); + } + } + } +} + +// ---------------------------------------------------------------------------- + +fn get_initial_layout() -> Vec> { + vec![ + Vec::new(), + vec!['B', 'Q', 'C'], + vec!['R', 'Q', 'W', 'Z'], + vec!['B', 'M', 'R', 'L', 'V'], + vec!['C', 'Z', 'H', 'V', 'T', 'W'], + vec!['D', 'Z', 'H', 'B', 'N', 'V', 'G'], + vec!['H', 'N', 'P', 'C', 'J', 'F', 'V', 'Q'], + vec!['D', 'G', 'T', 'R', 'W', 'Z', 'S'], + vec!['C', 'G', 'M', 'N', 'B', 'W', 'Z', 'P'], + vec!['N', 'J', 'B', 'M', 'W', 'Q', 'F', 'P'], + ] +} + +fn get_position_string(layout: &Vec>) -> String { + let mut s = String::new(); + + for i in 1..=9usize { + if let Some(ch) = layout[i].last() { + s.push(*ch); + } + } + + s +} + +fn main() { + let file_str = include_str!("input.txt"); + let parts: Vec<&str> = file_str.split("\n\n").collect(); + let moves = parts[1]; + + let mut layout = get_initial_layout(); + + moves.lines().for_each(|line| { + Move::from(line).apply(&mut layout); + }); + + let top_crates = get_position_string(&layout); + + println!("Part 1: Top crates after moves: {}", top_crates); +}