diff --git a/Cargo.lock b/Cargo.lock index 8448329..e021c80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,3 +17,7 @@ version = "0.1.0" [[package]] name = "day_4_rust" version = "0.1.0" + +[[package]] +name = "day_5_rust" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index bc34f13..5bc89fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ members = [ "day_1/rust", "day_2/rust", "day_3/rust", - "day_4/rust" + "day_4/rust", + "day_5/rust" ] \ No newline at end of file diff --git a/day_5/input.txt b/day_5/input.txt new file mode 100644 index 0000000..5862b1c --- /dev/null +++ b/day_5/input.txt @@ -0,0 +1,512 @@ +[T] [Q] [S] +[R] [M] [L] [V] [G] +[D] [V] [V] [Q] [N] [C] +[H] [T] [S] [C] [V] [D] [Z] +[Q] [J] [D] [M] [Z] [C] [M] [F] +[N] [B] [H] [N] [B] [W] [N] [J] [M] +[P] [G] [R] [Z] [Z] [C] [Z] [G] [P] +[B] [W] [N] [P] [D] [V] [G] [L] [T] + 1 2 3 4 5 6 7 8 9 + +move 5 from 4 to 9 +move 3 from 5 to 1 +move 12 from 9 to 6 +move 1 from 6 to 9 +move 3 from 2 to 8 +move 6 from 3 to 9 +move 2 from 2 to 9 +move 2 from 3 to 5 +move 9 from 8 to 1 +move 1 from 6 to 9 +move 1 from 8 to 3 +move 14 from 1 to 2 +move 8 from 2 to 6 +move 2 from 2 to 7 +move 2 from 5 to 8 +move 5 from 2 to 6 +move 9 from 7 to 8 +move 1 from 9 to 8 +move 5 from 6 to 9 +move 1 from 3 to 8 +move 1 from 7 to 5 +move 1 from 1 to 5 +move 4 from 1 to 7 +move 15 from 6 to 1 +move 4 from 7 to 6 +move 2 from 5 to 7 +move 9 from 8 to 7 +move 13 from 1 to 3 +move 8 from 6 to 9 +move 1 from 6 to 8 +move 1 from 7 to 5 +move 2 from 1 to 3 +move 4 from 7 to 1 +move 13 from 3 to 6 +move 2 from 1 to 3 +move 1 from 5 to 8 +move 2 from 3 to 4 +move 5 from 7 to 1 +move 4 from 1 to 9 +move 2 from 4 to 5 +move 4 from 6 to 2 +move 3 from 2 to 5 +move 6 from 8 to 1 +move 7 from 6 to 7 +move 1 from 3 to 5 +move 1 from 2 to 4 +move 8 from 1 to 8 +move 4 from 6 to 2 +move 3 from 5 to 3 +move 1 from 4 to 3 +move 2 from 1 to 3 +move 8 from 8 to 5 +move 2 from 3 to 8 +move 4 from 5 to 3 +move 1 from 9 to 2 +move 1 from 8 to 3 +move 1 from 2 to 1 +move 15 from 9 to 3 +move 6 from 7 to 5 +move 1 from 7 to 3 +move 2 from 2 to 8 +move 6 from 9 to 4 +move 22 from 3 to 6 +move 3 from 8 to 6 +move 1 from 1 to 2 +move 2 from 9 to 8 +move 6 from 4 to 7 +move 6 from 7 to 2 +move 16 from 6 to 9 +move 8 from 2 to 1 +move 4 from 6 to 1 +move 2 from 3 to 4 +move 9 from 5 to 4 +move 1 from 7 to 9 +move 1 from 6 to 2 +move 3 from 5 to 7 +move 16 from 9 to 4 +move 2 from 7 to 1 +move 4 from 6 to 3 +move 1 from 9 to 5 +move 1 from 9 to 7 +move 1 from 7 to 6 +move 1 from 7 to 9 +move 2 from 9 to 2 +move 1 from 6 to 1 +move 2 from 8 to 1 +move 11 from 4 to 2 +move 9 from 2 to 6 +move 9 from 6 to 1 +move 15 from 4 to 6 +move 1 from 4 to 2 +move 1 from 5 to 3 +move 6 from 6 to 4 +move 3 from 2 to 1 +move 2 from 4 to 6 +move 3 from 6 to 2 +move 7 from 6 to 2 +move 1 from 4 to 7 +move 1 from 7 to 2 +move 5 from 3 to 6 +move 1 from 5 to 4 +move 1 from 4 to 5 +move 8 from 1 to 6 +move 1 from 4 to 8 +move 12 from 6 to 1 +move 1 from 3 to 4 +move 1 from 4 to 1 +move 1 from 3 to 4 +move 2 from 6 to 5 +move 31 from 1 to 7 +move 2 from 5 to 7 +move 1 from 8 to 2 +move 1 from 5 to 8 +move 1 from 8 to 6 +move 3 from 4 to 9 +move 3 from 9 to 4 +move 2 from 4 to 3 +move 2 from 1 to 6 +move 2 from 3 to 8 +move 1 from 4 to 9 +move 4 from 2 to 9 +move 17 from 7 to 8 +move 3 from 8 to 2 +move 2 from 9 to 4 +move 4 from 2 to 5 +move 1 from 1 to 4 +move 1 from 9 to 3 +move 8 from 8 to 4 +move 1 from 9 to 4 +move 4 from 8 to 3 +move 8 from 2 to 5 +move 2 from 2 to 3 +move 1 from 2 to 1 +move 1 from 8 to 4 +move 2 from 8 to 1 +move 1 from 7 to 2 +move 1 from 8 to 6 +move 3 from 4 to 5 +move 8 from 4 to 7 +move 1 from 2 to 8 +move 1 from 8 to 1 +move 2 from 4 to 7 +move 8 from 5 to 9 +move 7 from 5 to 2 +move 6 from 3 to 1 +move 6 from 1 to 2 +move 9 from 9 to 4 +move 5 from 7 to 4 +move 2 from 1 to 2 +move 9 from 4 to 2 +move 3 from 6 to 2 +move 1 from 6 to 8 +move 1 from 8 to 9 +move 1 from 3 to 5 +move 6 from 7 to 5 +move 4 from 4 to 2 +move 19 from 2 to 3 +move 1 from 4 to 6 +move 7 from 7 to 5 +move 2 from 1 to 8 +move 12 from 3 to 4 +move 3 from 4 to 1 +move 1 from 6 to 3 +move 8 from 5 to 9 +move 3 from 9 to 7 +move 6 from 4 to 3 +move 3 from 1 to 2 +move 13 from 3 to 7 +move 3 from 4 to 6 +move 4 from 9 to 4 +move 14 from 7 to 8 +move 3 from 5 to 2 +move 3 from 2 to 6 +move 1 from 6 to 2 +move 1 from 3 to 9 +move 4 from 4 to 6 +move 11 from 2 to 7 +move 2 from 9 to 6 +move 3 from 5 to 6 +move 1 from 9 to 7 +move 14 from 6 to 5 +move 1 from 5 to 1 +move 4 from 5 to 8 +move 2 from 5 to 6 +move 4 from 2 to 5 +move 1 from 2 to 9 +move 14 from 8 to 5 +move 2 from 8 to 4 +move 3 from 8 to 7 +move 5 from 5 to 4 +move 13 from 5 to 7 +move 5 from 7 to 6 +move 31 from 7 to 9 +move 7 from 6 to 7 +move 6 from 5 to 7 +move 1 from 8 to 9 +move 1 from 5 to 3 +move 1 from 3 to 5 +move 1 from 1 to 8 +move 6 from 4 to 3 +move 1 from 8 to 5 +move 1 from 4 to 1 +move 33 from 9 to 3 +move 13 from 7 to 1 +move 29 from 3 to 2 +move 3 from 3 to 8 +move 1 from 5 to 2 +move 20 from 2 to 6 +move 19 from 6 to 4 +move 1 from 7 to 4 +move 5 from 1 to 7 +move 1 from 8 to 7 +move 2 from 8 to 5 +move 10 from 2 to 8 +move 6 from 3 to 9 +move 4 from 7 to 1 +move 1 from 3 to 5 +move 1 from 1 to 2 +move 1 from 7 to 6 +move 1 from 2 to 8 +move 1 from 8 to 7 +move 4 from 9 to 7 +move 2 from 5 to 2 +move 1 from 8 to 5 +move 1 from 8 to 6 +move 7 from 8 to 3 +move 2 from 9 to 4 +move 3 from 5 to 1 +move 2 from 2 to 5 +move 5 from 7 to 8 +move 10 from 4 to 1 +move 5 from 8 to 5 +move 10 from 1 to 3 +move 2 from 6 to 4 +move 1 from 7 to 3 +move 1 from 8 to 1 +move 3 from 5 to 8 +move 12 from 4 to 7 +move 3 from 5 to 3 +move 16 from 1 to 7 +move 2 from 3 to 7 +move 1 from 5 to 6 +move 3 from 8 to 4 +move 1 from 4 to 7 +move 1 from 6 to 3 +move 14 from 3 to 1 +move 5 from 3 to 8 +move 1 from 3 to 5 +move 1 from 7 to 6 +move 1 from 6 to 2 +move 13 from 7 to 2 +move 1 from 5 to 3 +move 3 from 4 to 2 +move 1 from 3 to 5 +move 3 from 8 to 9 +move 2 from 8 to 9 +move 1 from 6 to 4 +move 5 from 2 to 4 +move 3 from 2 to 5 +move 7 from 7 to 3 +move 7 from 4 to 7 +move 5 from 3 to 7 +move 8 from 2 to 3 +move 5 from 9 to 5 +move 11 from 1 to 9 +move 4 from 3 to 1 +move 1 from 2 to 7 +move 4 from 1 to 7 +move 22 from 7 to 3 +move 5 from 3 to 4 +move 1 from 7 to 1 +move 1 from 1 to 4 +move 3 from 4 to 6 +move 3 from 1 to 3 +move 2 from 6 to 1 +move 2 from 4 to 9 +move 13 from 9 to 1 +move 1 from 6 to 5 +move 4 from 7 to 1 +move 3 from 1 to 6 +move 19 from 3 to 9 +move 5 from 3 to 1 +move 18 from 9 to 8 +move 1 from 9 to 3 +move 11 from 1 to 7 +move 1 from 4 to 5 +move 13 from 8 to 1 +move 7 from 5 to 8 +move 7 from 8 to 5 +move 3 from 6 to 5 +move 2 from 3 to 9 +move 1 from 3 to 7 +move 5 from 5 to 2 +move 10 from 1 to 5 +move 9 from 7 to 9 +move 11 from 5 to 2 +move 2 from 8 to 4 +move 1 from 4 to 3 +move 2 from 7 to 3 +move 1 from 7 to 4 +move 3 from 8 to 3 +move 8 from 5 to 2 +move 2 from 3 to 8 +move 4 from 3 to 8 +move 6 from 2 to 6 +move 5 from 1 to 8 +move 8 from 2 to 7 +move 2 from 4 to 7 +move 9 from 2 to 9 +move 4 from 7 to 8 +move 5 from 1 to 8 +move 3 from 7 to 4 +move 1 from 8 to 3 +move 3 from 7 to 2 +move 3 from 1 to 9 +move 1 from 4 to 9 +move 1 from 6 to 3 +move 18 from 8 to 5 +move 1 from 8 to 2 +move 2 from 4 to 9 +move 3 from 2 to 1 +move 2 from 2 to 3 +move 24 from 9 to 8 +move 3 from 3 to 7 +move 15 from 8 to 2 +move 12 from 2 to 5 +move 1 from 7 to 4 +move 1 from 3 to 1 +move 28 from 5 to 4 +move 1 from 7 to 9 +move 2 from 2 to 1 +move 4 from 6 to 3 +move 1 from 5 to 3 +move 1 from 5 to 9 +move 1 from 2 to 6 +move 5 from 3 to 5 +move 8 from 4 to 2 +move 2 from 6 to 2 +move 1 from 7 to 3 +move 4 from 2 to 8 +move 3 from 1 to 2 +move 5 from 2 to 5 +move 3 from 5 to 4 +move 2 from 1 to 5 +move 2 from 2 to 1 +move 4 from 9 to 2 +move 7 from 8 to 9 +move 1 from 3 to 1 +move 1 from 1 to 7 +move 2 from 8 to 3 +move 4 from 9 to 3 +move 9 from 5 to 7 +move 3 from 3 to 5 +move 1 from 5 to 3 +move 7 from 7 to 9 +move 1 from 7 to 9 +move 1 from 5 to 9 +move 1 from 5 to 1 +move 1 from 8 to 5 +move 9 from 9 to 1 +move 2 from 7 to 2 +move 1 from 5 to 6 +move 4 from 3 to 2 +move 11 from 2 to 4 +move 1 from 8 to 4 +move 1 from 8 to 2 +move 1 from 2 to 8 +move 1 from 6 to 5 +move 1 from 8 to 6 +move 6 from 1 to 7 +move 1 from 5 to 6 +move 1 from 6 to 5 +move 3 from 9 to 8 +move 3 from 8 to 1 +move 3 from 7 to 8 +move 1 from 6 to 9 +move 1 from 2 to 4 +move 1 from 9 to 7 +move 2 from 7 to 9 +move 10 from 1 to 6 +move 2 from 9 to 3 +move 1 from 5 to 7 +move 3 from 7 to 5 +move 3 from 5 to 3 +move 4 from 6 to 3 +move 18 from 4 to 2 +move 3 from 4 to 1 +move 1 from 1 to 3 +move 2 from 1 to 2 +move 8 from 2 to 9 +move 1 from 4 to 7 +move 1 from 7 to 1 +move 3 from 9 to 2 +move 3 from 8 to 6 +move 1 from 4 to 9 +move 7 from 2 to 8 +move 7 from 6 to 7 +move 3 from 9 to 2 +move 3 from 2 to 5 +move 6 from 4 to 6 +move 2 from 5 to 6 +move 3 from 3 to 6 +move 6 from 6 to 3 +move 5 from 7 to 5 +move 2 from 4 to 8 +move 5 from 5 to 2 +move 1 from 7 to 2 +move 4 from 6 to 4 +move 1 from 7 to 8 +move 1 from 6 to 4 +move 1 from 5 to 7 +move 1 from 3 to 4 +move 1 from 6 to 4 +move 2 from 9 to 1 +move 3 from 1 to 3 +move 1 from 3 to 1 +move 9 from 2 to 1 +move 8 from 1 to 5 +move 1 from 7 to 1 +move 1 from 9 to 1 +move 4 from 5 to 7 +move 4 from 7 to 5 +move 1 from 1 to 9 +move 5 from 2 to 4 +move 1 from 9 to 6 +move 8 from 8 to 9 +move 18 from 4 to 9 +move 3 from 5 to 4 +move 2 from 6 to 5 +move 1 from 8 to 5 +move 17 from 9 to 6 +move 2 from 8 to 1 +move 1 from 4 to 6 +move 8 from 6 to 3 +move 1 from 1 to 8 +move 5 from 5 to 3 +move 1 from 1 to 7 +move 1 from 8 to 6 +move 2 from 4 to 5 +move 6 from 9 to 4 +move 1 from 7 to 5 +move 7 from 6 to 8 +move 2 from 6 to 5 +move 6 from 8 to 3 +move 1 from 9 to 6 +move 2 from 9 to 5 +move 1 from 3 to 1 +move 1 from 8 to 6 +move 7 from 5 to 6 +move 7 from 6 to 7 +move 5 from 4 to 9 +move 1 from 4 to 5 +move 2 from 9 to 6 +move 3 from 1 to 7 +move 5 from 6 to 8 +move 1 from 1 to 5 +move 21 from 3 to 6 +move 3 from 7 to 2 +move 2 from 9 to 3 +move 1 from 9 to 7 +move 5 from 5 to 7 +move 7 from 6 to 7 +move 14 from 7 to 1 +move 3 from 2 to 8 +move 12 from 1 to 4 +move 5 from 7 to 6 +move 1 from 7 to 4 +move 8 from 8 to 3 +move 8 from 3 to 5 +move 6 from 5 to 6 +move 1 from 5 to 3 +move 2 from 1 to 8 +move 2 from 8 to 3 +move 10 from 3 to 7 +move 8 from 4 to 3 +move 3 from 4 to 9 +move 3 from 9 to 2 +move 1 from 2 to 5 +move 2 from 2 to 9 +move 13 from 3 to 1 +move 1 from 4 to 1 +move 2 from 1 to 7 +move 1 from 5 to 8 +move 1 from 9 to 6 +move 1 from 9 to 2 +move 1 from 4 to 9 +move 8 from 6 to 2 +move 1 from 9 to 5 +move 1 from 2 to 8 +move 1 from 5 to 9 +move 2 from 2 to 3 +move 12 from 6 to 8 +move 1 from 3 to 7 +move 8 from 8 to 4 +move 1 from 9 to 1 +move 13 from 1 to 3 +move 2 from 4 to 5 +move 12 from 7 to 2 +move 1 from 5 to 8 +move 3 from 3 to 8 +move 2 from 4 to 1 +move 1 from 1 to 9 \ No newline at end of file diff --git a/day_5/rust/Cargo.toml b/day_5/rust/Cargo.toml new file mode 100644 index 0000000..afb1066 --- /dev/null +++ b/day_5/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day_5_rust" +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/day_5/rust/src/crates_iter.rs b/day_5/rust/src/crates_iter.rs new file mode 100644 index 0000000..e570179 --- /dev/null +++ b/day_5/rust/src/crates_iter.rs @@ -0,0 +1,51 @@ +use std::str::Chars; + +pub struct CratesLineIterator<'a> { + iter: Chars<'a>, + stack_index: u32, +} + +impl<'a> CratesLineIterator<'a> { + fn skip_by(&mut self, n: usize) -> Result<(), usize> { + for i in 0..n { + self.iter.next().ok_or(i)?; + } + Ok(()) + } + + pub fn new(string: &'a str) -> Self { + Self { + iter: string.chars(), + stack_index: 0, + } + } +} + +impl Iterator for CratesLineIterator<'_> { + type Item = (usize, char); + + fn next(&mut self) -> Option { + if let Err(_) = self.skip_by(1) { + return None; + } + let optional_char = self.iter.next(); + + if let Err(size) = self.skip_by(2) { + if size != 1 { + return None; + } + } + + return match optional_char { + Some(' ') => { + self.stack_index += 1; + return self.next(); + } + Some(c) => { + self.stack_index += 1; + Some((self.stack_index as usize, c)) + } + None => return None, + }; + } +} diff --git a/day_5/rust/src/main.rs b/day_5/rust/src/main.rs new file mode 100644 index 0000000..5f31b29 --- /dev/null +++ b/day_5/rust/src/main.rs @@ -0,0 +1,111 @@ +mod crates_iter; + +use crates_iter::CratesLineIterator; + +#[derive(Debug)] +struct Procuedure { + pub move_from: usize, + pub move_to: usize, + pub crates_count: u32, +} + +#[derive(Debug)] +struct SupplyStacks { + stacks: Vec>, +} + +impl SupplyStacks { + pub fn new(string_stacks: &str) -> Self { + let mut stacks_of_cartes = Self { stacks: Vec::new() }; + stacks_of_cartes.init_stacks(string_stacks); + + return stacks_of_cartes; + } + + fn init_stacks(&mut self, string_stacks: &str) { + let mut stacks_iter = string_stacks.lines().rev(); + + for char in stacks_iter.next().unwrap().chars() { + if char == ' ' { + continue; + } + self.stacks.push(Vec::new()); + } + + for line in stacks_iter { + for (index, char) in CratesLineIterator::new(line) { + self.stacks[index - 1].push(char); + } + } + } + + pub fn apply_procedure(&mut self, procedure: Procuedure, save_order: bool) { + if procedure.move_from == procedure.move_to { + return; + } + + let from_length = self.stacks[procedure.move_from - 1].len(); + + let moved_elements: Vec; + + if save_order { + moved_elements = self.stacks[procedure.move_from - 1] + .drain((from_length - procedure.crates_count as usize)..) + .rev() + .collect::>(); + } else { + moved_elements = self.stacks[procedure.move_from - 1] + .drain((from_length - procedure.crates_count as usize)..) + .collect::>(); + } + + let to_index = procedure.move_to - 1; + + for char in moved_elements { + self.stacks[to_index].push(char); + } + } + + pub fn get_highest_elements(&self) -> Vec { + let mut result: Vec = Vec::new(); + + for stack in &self.stacks { + result.push(stack[stack.len() - 1]) + } + + return result; + } +} + +fn main() { + let data = include_str!("../../input.txt"); + + let (string_stacks, procedures_str) = match data.split_once("\n\n") { + Some((a, b)) => (a, b), + _ => panic!(), + }; + + let mut stacks = SupplyStacks::new(string_stacks); + + let procedures = procedures_str + .lines() + .map(|line| { + let items = line.split(" ").collect::>(); + + return Procuedure { + crates_count: items[1].parse::().unwrap(), + move_from: items[3].parse::().unwrap(), + move_to: items[5].parse::().unwrap(), + }; + }) + .collect::>(); + + for proc in procedures { + stacks.apply_procedure(proc, true); + } + + let highest_vec = stacks.get_highest_elements(); + let highest_str = highest_vec.iter().collect::(); + + println!("{highest_str}"); +}