From e56f7c64ea2ed1d8e0c262fdff9cc569c3490052 Mon Sep 17 00:00:00 2001 From: george Date: Tue, 6 Dec 2022 21:26:50 +0000 Subject: [PATCH] days 5 + 6 --- day05/Cargo.lock | 7 +++++ day05/Cargo.toml | 8 +++++ day05/src/main.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ day06/Cargo.lock | 16 ++++++++++ day06/Cargo.toml | 9 ++++++ day06/src/main.rs | 17 +++++++++++ 6 files changed, 132 insertions(+) create mode 100644 day05/Cargo.lock create mode 100644 day05/Cargo.toml create mode 100644 day05/src/main.rs create mode 100644 day06/Cargo.lock create mode 100644 day06/Cargo.toml create mode 100644 day06/src/main.rs diff --git a/day05/Cargo.lock b/day05/Cargo.lock new file mode 100644 index 0000000..a443224 --- /dev/null +++ b/day05/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day05" +version = "0.1.0" diff --git a/day05/Cargo.toml b/day05/Cargo.toml new file mode 100644 index 0000000..c7d046f --- /dev/null +++ b/day05/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day05" +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/day05/src/main.rs b/day05/src/main.rs new file mode 100644 index 0000000..eec366a --- /dev/null +++ b/day05/src/main.rs @@ -0,0 +1,75 @@ +#![feature(iter_next_chunk)] +use std::collections::VecDeque; + +const OFFSET: usize = 4; + +enum MachineType { + Type9000, + Type9001, +} + +fn main() { + let f = std::fs::read_to_string("/dev/stdin").unwrap(); + let (crates, instructions) = f.split_once("\n\n").unwrap(); + let (crates, instructions) = (crates.lines(), instructions.lines()); + + let mut stacks: Vec> = + vec![VecDeque::new(); (f.lines().next().unwrap().len() / 4) + 1]; + + for crate_str in crates { + for i in (1..crate_str.len()).step_by(OFFSET) { + if let Some(crate_label) = crate_str.get(i..i + 1) { + if !crate_label.chars().next().unwrap().is_alphabetic() { + continue; + } + let vee = stacks.get_mut(i / 4).unwrap(); + vee.push_front(crate_label); + } + } + } + + let (mut reset_stacks, reset_instructions) = (stacks.clone(), instructions.clone()); + + solve(instructions, &mut stacks, MachineType::Type9000); + solve(reset_instructions, &mut reset_stacks, MachineType::Type9001); +} + +fn solve(instructions: core::str::Lines, stacks: &mut [VecDeque<&str>], machine_type: MachineType) { + for instruction in instructions { + if let Ok([amount, source, dest]) = instruction + .split_whitespace() + .filter_map(|n| n.parse::().ok()) + .next_chunk() + { + let mut temp = VecDeque::new(); + let source_stack = stacks.get_mut(source - 1).unwrap(); + for _ in 0..amount { + temp.push_front(source_stack.pop_back().unwrap()); + } + let dest_stack = stacks.get_mut(dest - 1).unwrap(); + match machine_type { + MachineType::Type9000 => { + for _ in 0..amount { + dest_stack.push_back(temp.pop_back().unwrap()); + } + } + MachineType::Type9001 => { + if amount == 1 { + dest_stack.push_back(temp.pop_back().unwrap()); + } else { + dest_stack.append(&mut temp); + } + } + } + } + } + if matches!(machine_type, MachineType::Type9000) { + print!("P1: "); + } else { + print!("P2: "); + } + for stack in stacks.iter_mut() { + print!("{}", stack.pop_back().unwrap()); + } + println!(); +} diff --git a/day06/Cargo.lock b/day06/Cargo.lock new file mode 100644 index 0000000..21b743c --- /dev/null +++ b/day06/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day06" +version = "0.1.0" +dependencies = [ + "rustc-hash", +] + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" diff --git a/day06/Cargo.toml b/day06/Cargo.toml new file mode 100644 index 0000000..33bfe02 --- /dev/null +++ b/day06/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day06" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rustc-hash = "1.1.0" diff --git a/day06/src/main.rs b/day06/src/main.rs new file mode 100644 index 0000000..1bb200a --- /dev/null +++ b/day06/src/main.rs @@ -0,0 +1,17 @@ +use rustc_hash::FxHashSet; +const PACKET_WINDOW_SIZE: usize = 4; +const MESSAGE_WINDOW_SIZE: usize = 14; + +fn main() { + let s = std::fs::read_to_string("/dev/stdin").unwrap(); + println!("P1: {}, P2: {}", find_index(&s, PACKET_WINDOW_SIZE), find_index(&s, MESSAGE_WINDOW_SIZE)); +} + +fn find_index(s: &str, window_size: usize) -> usize { + for (count, window) in s.chars().collect::>().windows(window_size).enumerate(){ + if FxHashSet::from_iter(window).len() == window_size { + return count + window_size; + } + } + 0 +}