From 324d5ab02ba40b33adfc24deae495713eb4416fe Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Mon, 5 Dec 2022 20:26:50 +0100 Subject: [PATCH] [TASK] Implemented day 5 --- .run/run-day-5.run.xml | 19 ++ Cargo.lock | 7 + Cargo.toml | 3 +- input/day5.txt | 513 +++++++++++++++++++++++++++++++++++++++++ input/day5_example.txt | 9 + src/day5.rs | 120 ++++++++++ src/dayX.rs | 4 +- src/main.rs | 5 +- 8 files changed, 676 insertions(+), 4 deletions(-) create mode 100644 .run/run-day-5.run.xml create mode 100644 input/day5.txt create mode 100644 input/day5_example.txt create mode 100644 src/day5.rs diff --git a/.run/run-day-5.run.xml b/.run/run-day-5.run.xml new file mode 100644 index 0000000..3766da0 --- /dev/null +++ b/.run/run-day-5.run.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 51b788e..01d5af2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ version = "0.1.0" dependencies = [ "lazy_static", "regex", + "rustc-hash", ] [[package]] @@ -47,3 +48,9 @@ name = "regex-syntax" version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" diff --git a/Cargo.toml b/Cargo.toml index e76b11f..1c83c59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] regex = "1" -lazy_static = "1.4.0" \ No newline at end of file +lazy_static = "1.4.0" +rustc-hash = "1.1.0" \ No newline at end of file diff --git a/input/day5.txt b/input/day5.txt new file mode 100644 index 0000000..f0f2014 --- /dev/null +++ b/input/day5.txt @@ -0,0 +1,513 @@ +[H] [Z] [J] +[L] [W] [B] [G] [R] +[R] [G] [S] [J] [H] [Q] +[F] [N] [T] [J] [P] [R] [F] +[B] [C] [M] [R] [Q] [F] [G] [P] +[C] [D] [F] [D] [D] [D] [T] [M] [G] +[J] [C] [J] [J] [C] [L] [Z] [V] [B] +[M] [Z] [H] [P] [N] [W] [P] [L] [C] + 1 2 3 4 5 6 7 8 9 + +move 3 from 2 to 1 +move 8 from 6 to 4 +move 4 from 8 to 2 +move 3 from 1 to 9 +move 1 from 2 to 4 +move 3 from 7 to 5 +move 3 from 9 to 2 +move 3 from 3 to 5 +move 1 from 5 to 1 +move 5 from 1 to 8 +move 2 from 1 to 8 +move 3 from 7 to 3 +move 1 from 8 to 9 +move 6 from 9 to 8 +move 3 from 8 to 7 +move 7 from 8 to 9 +move 2 from 5 to 9 +move 2 from 2 to 9 +move 3 from 3 to 7 +move 2 from 8 to 3 +move 7 from 4 to 8 +move 3 from 4 to 1 +move 4 from 8 to 6 +move 4 from 6 to 1 +move 8 from 1 to 2 +move 1 from 1 to 4 +move 3 from 5 to 1 +move 8 from 9 to 8 +move 4 from 3 to 1 +move 5 from 5 to 3 +move 2 from 7 to 1 +move 1 from 7 to 4 +move 1 from 7 to 2 +move 3 from 3 to 5 +move 3 from 9 to 1 +move 9 from 8 to 1 +move 2 from 9 to 7 +move 1 from 8 to 5 +move 4 from 5 to 3 +move 1 from 3 to 4 +move 1 from 9 to 6 +move 1 from 6 to 9 +move 7 from 4 to 9 +move 1 from 7 to 3 +move 1 from 8 to 2 +move 8 from 2 to 1 +move 4 from 3 to 5 +move 2 from 9 to 6 +move 2 from 6 to 2 +move 2 from 4 to 9 +move 8 from 9 to 2 +move 3 from 7 to 9 +move 1 from 3 to 5 +move 2 from 3 to 8 +move 9 from 2 to 1 +move 1 from 8 to 7 +move 4 from 2 to 9 +move 4 from 5 to 6 +move 1 from 8 to 9 +move 27 from 1 to 2 +move 1 from 6 to 4 +move 3 from 6 to 4 +move 7 from 9 to 8 +move 4 from 4 to 1 +move 9 from 2 to 6 +move 2 from 1 to 9 +move 6 from 1 to 3 +move 1 from 5 to 3 +move 3 from 3 to 5 +move 3 from 5 to 3 +move 3 from 3 to 1 +move 4 from 6 to 7 +move 3 from 9 to 2 +move 1 from 6 to 4 +move 4 from 3 to 5 +move 3 from 6 to 5 +move 1 from 6 to 2 +move 15 from 2 to 3 +move 5 from 5 to 9 +move 13 from 3 to 9 +move 2 from 5 to 7 +move 1 from 4 to 2 +move 3 from 3 to 7 +move 11 from 2 to 7 +move 7 from 9 to 5 +move 3 from 5 to 7 +move 6 from 8 to 9 +move 4 from 1 to 2 +move 6 from 1 to 6 +move 3 from 5 to 1 +move 1 from 8 to 2 +move 4 from 2 to 9 +move 1 from 5 to 7 +move 6 from 7 to 6 +move 18 from 7 to 5 +move 1 from 7 to 1 +move 8 from 9 to 5 +move 1 from 2 to 6 +move 15 from 5 to 6 +move 6 from 5 to 3 +move 4 from 3 to 6 +move 26 from 6 to 5 +move 2 from 1 to 7 +move 4 from 5 to 9 +move 8 from 5 to 7 +move 3 from 7 to 9 +move 14 from 9 to 8 +move 7 from 5 to 2 +move 4 from 2 to 1 +move 5 from 1 to 9 +move 12 from 5 to 3 +move 5 from 8 to 5 +move 14 from 3 to 2 +move 1 from 5 to 2 +move 10 from 2 to 6 +move 7 from 9 to 6 +move 6 from 8 to 6 +move 1 from 2 to 7 +move 2 from 9 to 7 +move 2 from 8 to 6 +move 6 from 2 to 7 +move 1 from 1 to 8 +move 15 from 6 to 2 +move 1 from 6 to 9 +move 1 from 5 to 9 +move 1 from 9 to 6 +move 2 from 2 to 4 +move 3 from 9 to 5 +move 5 from 5 to 3 +move 3 from 3 to 6 +move 6 from 2 to 7 +move 1 from 5 to 9 +move 8 from 6 to 9 +move 2 from 6 to 7 +move 3 from 2 to 4 +move 9 from 6 to 7 +move 17 from 7 to 5 +move 1 from 8 to 4 +move 7 from 9 to 3 +move 12 from 5 to 8 +move 3 from 5 to 2 +move 4 from 7 to 8 +move 2 from 5 to 7 +move 1 from 7 to 9 +move 8 from 3 to 7 +move 17 from 7 to 5 +move 3 from 2 to 5 +move 1 from 3 to 6 +move 10 from 5 to 4 +move 5 from 2 to 7 +move 1 from 4 to 2 +move 3 from 9 to 8 +move 7 from 7 to 2 +move 5 from 5 to 1 +move 14 from 4 to 9 +move 3 from 9 to 8 +move 1 from 6 to 9 +move 2 from 1 to 4 +move 2 from 8 to 5 +move 16 from 8 to 6 +move 1 from 6 to 2 +move 11 from 9 to 2 +move 2 from 7 to 5 +move 1 from 1 to 6 +move 11 from 2 to 9 +move 4 from 2 to 8 +move 9 from 5 to 3 +move 1 from 4 to 2 +move 2 from 1 to 8 +move 1 from 2 to 9 +move 2 from 4 to 3 +move 8 from 6 to 9 +move 16 from 9 to 3 +move 16 from 3 to 2 +move 17 from 2 to 6 +move 1 from 9 to 3 +move 1 from 2 to 5 +move 1 from 9 to 4 +move 3 from 2 to 8 +move 1 from 9 to 1 +move 1 from 9 to 6 +move 7 from 3 to 1 +move 5 from 3 to 5 +move 3 from 8 to 3 +move 2 from 3 to 4 +move 6 from 8 to 4 +move 7 from 6 to 4 +move 3 from 6 to 7 +move 3 from 8 to 9 +move 3 from 5 to 2 +move 3 from 1 to 3 +move 1 from 4 to 8 +move 3 from 5 to 1 +move 13 from 4 to 7 +move 14 from 6 to 7 +move 6 from 1 to 9 +move 3 from 9 to 6 +move 1 from 8 to 7 +move 1 from 8 to 7 +move 20 from 7 to 3 +move 1 from 8 to 9 +move 1 from 1 to 9 +move 1 from 1 to 5 +move 1 from 4 to 6 +move 14 from 3 to 9 +move 1 from 2 to 6 +move 3 from 7 to 6 +move 6 from 3 to 2 +move 1 from 3 to 8 +move 2 from 7 to 3 +move 7 from 6 to 3 +move 12 from 3 to 1 +move 1 from 8 to 2 +move 1 from 4 to 9 +move 1 from 5 to 6 +move 1 from 6 to 4 +move 1 from 4 to 2 +move 2 from 2 to 3 +move 16 from 9 to 7 +move 3 from 6 to 7 +move 6 from 9 to 4 +move 4 from 4 to 7 +move 6 from 1 to 8 +move 2 from 3 to 6 +move 3 from 1 to 9 +move 3 from 2 to 3 +move 3 from 3 to 8 +move 5 from 2 to 8 +move 2 from 7 to 8 +move 3 from 1 to 5 +move 1 from 4 to 3 +move 2 from 9 to 8 +move 1 from 6 to 8 +move 2 from 9 to 1 +move 15 from 7 to 1 +move 1 from 6 to 5 +move 10 from 1 to 5 +move 1 from 4 to 1 +move 2 from 1 to 6 +move 9 from 7 to 8 +move 27 from 8 to 3 +move 1 from 6 to 1 +move 1 from 8 to 5 +move 5 from 5 to 6 +move 12 from 3 to 1 +move 3 from 7 to 1 +move 7 from 5 to 1 +move 1 from 6 to 4 +move 3 from 6 to 9 +move 1 from 4 to 2 +move 2 from 6 to 5 +move 1 from 7 to 6 +move 1 from 9 to 2 +move 2 from 5 to 6 +move 2 from 6 to 5 +move 3 from 1 to 3 +move 19 from 3 to 1 +move 2 from 2 to 9 +move 42 from 1 to 7 +move 4 from 9 to 7 +move 1 from 6 to 8 +move 1 from 8 to 5 +move 2 from 1 to 9 +move 3 from 5 to 7 +move 27 from 7 to 4 +move 1 from 1 to 4 +move 3 from 9 to 2 +move 18 from 4 to 9 +move 2 from 5 to 3 +move 1 from 7 to 1 +move 2 from 3 to 4 +move 8 from 7 to 5 +move 15 from 9 to 3 +move 1 from 9 to 7 +move 3 from 7 to 2 +move 2 from 7 to 2 +move 2 from 5 to 3 +move 1 from 1 to 5 +move 1 from 9 to 1 +move 1 from 3 to 1 +move 1 from 4 to 3 +move 8 from 7 to 3 +move 8 from 2 to 4 +move 1 from 9 to 6 +move 23 from 3 to 9 +move 1 from 9 to 6 +move 2 from 6 to 8 +move 1 from 8 to 6 +move 1 from 5 to 3 +move 7 from 4 to 8 +move 7 from 5 to 7 +move 2 from 8 to 3 +move 1 from 1 to 8 +move 3 from 7 to 4 +move 5 from 4 to 3 +move 1 from 1 to 8 +move 3 from 3 to 1 +move 8 from 9 to 7 +move 3 from 8 to 4 +move 1 from 6 to 2 +move 5 from 8 to 7 +move 6 from 3 to 1 +move 1 from 2 to 9 +move 7 from 7 to 9 +move 4 from 1 to 9 +move 2 from 4 to 2 +move 1 from 4 to 9 +move 1 from 1 to 6 +move 8 from 4 to 8 +move 4 from 1 to 5 +move 3 from 5 to 2 +move 2 from 2 to 5 +move 2 from 5 to 6 +move 1 from 3 to 7 +move 2 from 6 to 4 +move 1 from 5 to 7 +move 1 from 6 to 9 +move 1 from 4 to 1 +move 6 from 9 to 2 +move 8 from 9 to 7 +move 4 from 7 to 3 +move 4 from 8 to 3 +move 3 from 8 to 3 +move 8 from 3 to 5 +move 1 from 1 to 7 +move 11 from 9 to 7 +move 5 from 2 to 7 +move 1 from 8 to 1 +move 3 from 2 to 3 +move 1 from 1 to 4 +move 1 from 2 to 5 +move 20 from 7 to 8 +move 7 from 7 to 9 +move 4 from 4 to 7 +move 3 from 9 to 4 +move 5 from 7 to 4 +move 7 from 4 to 7 +move 4 from 9 to 2 +move 1 from 4 to 3 +move 4 from 3 to 5 +move 2 from 5 to 8 +move 4 from 5 to 2 +move 5 from 2 to 6 +move 2 from 6 to 3 +move 22 from 8 to 5 +move 13 from 7 to 9 +move 11 from 9 to 3 +move 2 from 6 to 8 +move 7 from 3 to 1 +move 18 from 5 to 2 +move 1 from 6 to 4 +move 1 from 4 to 9 +move 2 from 8 to 5 +move 2 from 9 to 1 +move 9 from 3 to 1 +move 4 from 5 to 6 +move 2 from 6 to 7 +move 3 from 9 to 5 +move 10 from 5 to 8 +move 6 from 8 to 7 +move 3 from 8 to 1 +move 6 from 2 to 3 +move 1 from 9 to 6 +move 5 from 3 to 4 +move 4 from 1 to 4 +move 17 from 1 to 5 +move 12 from 2 to 7 +move 1 from 3 to 6 +move 16 from 5 to 8 +move 3 from 5 to 6 +move 9 from 8 to 3 +move 8 from 8 to 4 +move 7 from 4 to 1 +move 5 from 1 to 4 +move 4 from 3 to 7 +move 14 from 7 to 3 +move 6 from 4 to 8 +move 9 from 7 to 4 +move 5 from 6 to 1 +move 1 from 7 to 1 +move 1 from 6 to 7 +move 16 from 4 to 5 +move 1 from 4 to 2 +move 1 from 7 to 5 +move 2 from 1 to 7 +move 2 from 7 to 4 +move 4 from 1 to 6 +move 13 from 5 to 6 +move 5 from 6 to 3 +move 22 from 3 to 2 +move 1 from 4 to 7 +move 4 from 5 to 4 +move 1 from 7 to 6 +move 5 from 8 to 5 +move 2 from 3 to 1 +move 13 from 6 to 1 +move 6 from 1 to 4 +move 1 from 8 to 1 +move 6 from 1 to 4 +move 1 from 5 to 4 +move 7 from 4 to 7 +move 3 from 1 to 5 +move 2 from 5 to 7 +move 5 from 5 to 1 +move 8 from 7 to 4 +move 1 from 6 to 4 +move 1 from 7 to 4 +move 9 from 2 to 7 +move 8 from 7 to 6 +move 5 from 6 to 4 +move 1 from 7 to 4 +move 2 from 4 to 9 +move 2 from 6 to 1 +move 8 from 2 to 6 +move 9 from 1 to 8 +move 9 from 6 to 2 +move 1 from 1 to 8 +move 6 from 8 to 4 +move 2 from 9 to 7 +move 2 from 7 to 9 +move 15 from 2 to 8 +move 18 from 4 to 2 +move 14 from 4 to 5 +move 10 from 2 to 4 +move 9 from 2 to 6 +move 1 from 9 to 3 +move 1 from 3 to 1 +move 6 from 5 to 8 +move 3 from 4 to 9 +move 2 from 2 to 1 +move 1 from 1 to 6 +move 3 from 9 to 7 +move 22 from 8 to 6 +move 1 from 8 to 9 +move 2 from 1 to 5 +move 5 from 5 to 4 +move 2 from 5 to 8 +move 2 from 8 to 7 +move 1 from 9 to 7 +move 1 from 5 to 8 +move 1 from 9 to 8 +move 15 from 6 to 4 +move 2 from 5 to 2 +move 11 from 4 to 6 +move 5 from 4 to 1 +move 5 from 4 to 2 +move 2 from 1 to 5 +move 6 from 2 to 8 +move 11 from 6 to 3 +move 12 from 6 to 8 +move 1 from 3 to 9 +move 3 from 3 to 2 +move 6 from 4 to 2 +move 2 from 5 to 8 +move 5 from 7 to 2 +move 11 from 8 to 4 +move 1 from 7 to 4 +move 1 from 9 to 6 +move 7 from 2 to 1 +move 3 from 6 to 5 +move 2 from 5 to 3 +move 1 from 5 to 9 +move 3 from 4 to 9 +move 4 from 9 to 1 +move 4 from 3 to 6 +move 3 from 4 to 8 +move 3 from 8 to 9 +move 2 from 8 to 2 +move 9 from 8 to 7 +move 2 from 3 to 1 +move 2 from 3 to 2 +move 1 from 3 to 6 +move 2 from 9 to 1 +move 8 from 7 to 5 +move 7 from 2 to 7 +move 2 from 8 to 9 +move 4 from 6 to 5 +move 13 from 1 to 5 +move 4 from 1 to 8 +move 3 from 9 to 3 +move 12 from 5 to 9 +move 3 from 8 to 9 +move 1 from 8 to 4 +move 3 from 2 to 7 +move 3 from 3 to 7 +move 1 from 9 to 2 +move 4 from 6 to 4 +move 6 from 5 to 6 +move 2 from 7 to 3 +move 2 from 2 to 1 +move 5 from 6 to 5 +move 1 from 1 to 7 +move 9 from 5 to 4 +move 10 from 9 to 6 +move 1 from 2 to 6 +move 12 from 7 to 6 +move 1 from 7 to 4 +move 23 from 6 to 1 +move 10 from 4 to 3 +move 16 from 1 to 5 +move 5 from 1 to 2 +move 6 from 3 to 7 +move 5 from 4 to 8 \ No newline at end of file diff --git a/input/day5_example.txt b/input/day5_example.txt new file mode 100644 index 0000000..c526f15 --- /dev/null +++ b/input/day5_example.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 \ No newline at end of file diff --git a/src/day5.rs b/src/day5.rs new file mode 100644 index 0000000..848adaa --- /dev/null +++ b/src/day5.rs @@ -0,0 +1,120 @@ +use rustc_hash::FxHashMap; +use crate::day_solver::DaySolver; + +use super::util; + +#[derive(Debug, Copy, Clone)] +struct StackInstruction { + amount: u8, + from: u8, + to: u8 +} + +impl StackInstruction { + fn parse(str: &str) -> StackInstruction { + let mut split = str.split_whitespace() + .filter(|s| s.chars().all(|c| c.is_numeric())); + return StackInstruction { + amount: split.next().unwrap().parse().unwrap(), + from: split.next().unwrap().parse().unwrap(), + to: split.next().unwrap().parse().unwrap() + } + } +} + +#[derive(Debug, Clone)] +pub struct Day5 { + stacks: Vec>, + stack_ids_rev: FxHashMap, + instructions: Vec +} + +impl Day5 { + fn execute_instruction_part1(&mut self, instr: &StackInstruction) { + for _ in 0..instr.amount { + let moving_crate = self.stacks[self.stack_ids_rev[&instr.from]].pop().unwrap(); + self.stacks[self.stack_ids_rev[&instr.to]].push(moving_crate); + } + } + + fn execute_instructions_part1(&self) -> Day5 { + + let mut updated_day = self.clone(); + for instr in &self.instructions { + updated_day.execute_instruction_part1(instr); + } + updated_day + } + + fn execute_instruction_part2(&mut self, instr: &StackInstruction) { + + let from_stack = &self.stacks[self.stack_ids_rev[&instr.from]]; + let from_stack_remaining = from_stack.len() - instr.amount as usize; + let to_move: Vec = from_stack.iter().skip(from_stack_remaining).cloned().collect(); + self.stacks[self.stack_ids_rev[&instr.to]].extend(to_move); + self.stacks[self.stack_ids_rev[&instr.from]].truncate(from_stack_remaining); + } + + fn execute_instructions_part2(&self) -> Day5 { + + let mut updated_day = self.clone(); + for instr in &self.instructions { + updated_day.execute_instruction_part2(instr); + } + updated_day + } +} + +impl Day5 { + + pub fn create() -> Self { + // let lines = util::read_file("input/day5_example.txt"); + let lines = util::read_file("input/day5.txt"); + + // Put the input into the day struct + let mut line_split = lines.split(|s| s.is_empty()); + let stack_lines = line_split.next().unwrap(); + let instruction_lines = line_split.next().unwrap(); + + let stack_id_line = stack_lines.last().unwrap(); + let stack_id_strs: Vec<&str> = stack_id_line.split_whitespace().collect(); + let mut stack_id_idxs = Vec::new(); + let mut last_idx = 0; + for stack_id_str in &stack_id_strs { + let stack_id_idx = stack_id_line[last_idx..].find(stack_id_str).unwrap(); + stack_id_idxs.push(last_idx + stack_id_idx); + last_idx = stack_id_idx + 1; + } + let stack_ids: Vec = stack_id_strs.iter().map(|s| s.parse::().unwrap()).collect(); + + let mut stacks: Vec> = vec![Vec::with_capacity(stack_lines.len() - 1); stack_id_idxs.len()]; + for stack_line in stack_lines.iter().rev().skip(1) { + stack_id_idxs.iter().enumerate() + .for_each(|(i, stack_id_idx)| if let Some(c) = stack_line.chars().nth(stack_id_idx.to_owned()).filter(|c| !c.is_whitespace()) { stacks[i].push(c) }) + } + + return Day5 { + stacks, + stack_ids_rev: FxHashMap::from_iter(stack_ids.iter().enumerate().map(|(k, v)| (v.clone(), k))), + instructions: instruction_lines.iter().map(|s| StackInstruction::parse(s)).collect() + }; + } +} + +impl DaySolver for Day5 { + + + fn solve_part1(&mut self) -> String { + let updated_day = self.execute_instructions_part1(); + return updated_day.stacks.iter().filter_map(|s| s.last()) + .cloned() + .collect::() + } + + fn solve_part2(&mut self) -> String { + let updated_day = self.execute_instructions_part2(); + return updated_day.stacks.iter().filter_map(|s| s.last()) + .cloned() + .collect::() + } +} diff --git a/src/dayX.rs b/src/dayX.rs index d5a4c5b..9fda065 100644 --- a/src/dayX.rs +++ b/src/dayX.rs @@ -8,8 +8,8 @@ pub struct DayX { impl DayX { pub fn create() -> Self { - // let lines = util::read_file("input/dayX_example.txt"); - let lines = util::read_file("input/dayX.txt"); + let lines = util::read_file("input/dayX_example.txt"); + // let lines = util::read_file("input/dayX.txt"); // Put the input into the day struct return DayX {} diff --git a/src/main.rs b/src/main.rs index 0687ba4..6d69075 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use crate::day1::Day1; use crate::day2::Day2; use crate::day3::Day3; use crate::day4::Day4; +use crate::day5::Day5; use crate::day_solver::DaySolver; mod util; @@ -11,8 +12,9 @@ mod day_solver; mod day2; mod day3; mod day4; +mod day5; -const MAX_DAY: u8 = 4; +const MAX_DAY: u8 = 5; const DEFAULT_BENCHMARK_AMOUNT: u32 = 100; fn main() { @@ -80,6 +82,7 @@ fn build_day_solver(day: u8) -> Option> { 2 => Some(Box::new(Day2::create())), 3 => Some(Box::new(Day3::create())), 4 => Some(Box::new(Day4::create())), + 5 => Some(Box::new(Day5::create())), _ => None } }