diff --git a/2022/input/day05.txt b/2022/input/day05.txt new file mode 100755 index 0000000..42c96a4 --- /dev/null +++ b/2022/input/day05.txt @@ -0,0 +1,512 @@ + [B] [L] [J] + [B] [Q] [R] [D] [T] + [G] [H] [H] [M] [N] [F] + [J] [N] [D] [F] [J] [H] [B] + [Q] [F] [W] [S] [V] [N] [F] [N] +[W] [N] [H] [M] [L] [B] [R] [T] [Q] +[L] [T] [C] [R] [R] [J] [W] [Z] [L] +[S] [J] [S] [T] [T] [M] [D] [B] [H] + 1 2 3 4 5 6 7 8 9 + +move 5 from 4 to 5 +move 2 from 5 to 8 +move 2 from 9 to 1 +move 2 from 9 to 1 +move 1 from 5 to 3 +move 10 from 5 to 8 +move 1 from 4 to 7 +move 1 from 1 to 2 +move 5 from 3 to 7 +move 1 from 2 to 8 +move 21 from 8 to 5 +move 13 from 5 to 7 +move 2 from 9 to 4 +move 1 from 7 to 4 +move 5 from 1 to 4 +move 1 from 5 to 7 +move 2 from 2 to 7 +move 1 from 3 to 2 +move 1 from 1 to 6 +move 7 from 5 to 9 +move 16 from 7 to 4 +move 7 from 9 to 3 +move 1 from 7 to 5 +move 1 from 3 to 8 +move 3 from 2 to 7 +move 1 from 8 to 9 +move 3 from 3 to 6 +move 21 from 4 to 9 +move 1 from 5 to 7 +move 4 from 4 to 9 +move 8 from 6 to 3 +move 6 from 7 to 1 +move 12 from 9 to 8 +move 6 from 7 to 2 +move 3 from 6 to 5 +move 1 from 6 to 9 +move 4 from 8 to 6 +move 3 from 8 to 5 +move 4 from 1 to 8 +move 4 from 6 to 1 +move 2 from 1 to 3 +move 1 from 5 to 8 +move 2 from 2 to 8 +move 5 from 8 to 3 +move 4 from 2 to 7 +move 5 from 8 to 1 +move 2 from 1 to 7 +move 1 from 8 to 2 +move 2 from 1 to 7 +move 11 from 9 to 2 +move 1 from 8 to 5 +move 2 from 9 to 4 +move 3 from 9 to 5 +move 2 from 5 to 1 +move 6 from 5 to 8 +move 2 from 4 to 2 +move 1 from 5 to 6 +move 7 from 1 to 8 +move 2 from 2 to 7 +move 13 from 8 to 1 +move 16 from 3 to 1 +move 3 from 2 to 1 +move 12 from 7 to 6 +move 15 from 1 to 8 +move 2 from 3 to 8 +move 16 from 1 to 2 +move 24 from 2 to 8 +move 1 from 1 to 5 +move 1 from 5 to 8 +move 3 from 6 to 7 +move 26 from 8 to 3 +move 20 from 3 to 9 +move 1 from 2 to 9 +move 16 from 9 to 3 +move 14 from 3 to 1 +move 13 from 1 to 6 +move 3 from 3 to 4 +move 3 from 9 to 4 +move 1 from 7 to 8 +move 5 from 8 to 2 +move 8 from 8 to 5 +move 18 from 6 to 1 +move 4 from 8 to 5 +move 6 from 4 to 1 +move 2 from 2 to 5 +move 5 from 3 to 8 +move 5 from 8 to 7 +move 2 from 5 to 8 +move 5 from 5 to 4 +move 3 from 2 to 8 +move 22 from 1 to 2 +move 1 from 1 to 2 +move 5 from 8 to 2 +move 2 from 5 to 2 +move 1 from 1 to 6 +move 5 from 5 to 2 +move 1 from 9 to 8 +move 5 from 4 to 1 +move 6 from 6 to 9 +move 3 from 1 to 9 +move 1 from 1 to 7 +move 8 from 9 to 6 +move 6 from 7 to 1 +move 5 from 6 to 5 +move 27 from 2 to 1 +move 4 from 5 to 7 +move 9 from 1 to 5 +move 1 from 9 to 1 +move 3 from 6 to 2 +move 9 from 2 to 1 +move 2 from 7 to 2 +move 1 from 8 to 7 +move 10 from 5 to 9 +move 1 from 9 to 7 +move 25 from 1 to 8 +move 6 from 7 to 4 +move 11 from 1 to 7 +move 3 from 8 to 1 +move 3 from 2 to 6 +move 3 from 8 to 9 +move 11 from 8 to 6 +move 1 from 2 to 6 +move 12 from 6 to 4 +move 13 from 4 to 5 +move 1 from 6 to 1 +move 3 from 7 to 5 +move 5 from 8 to 7 +move 1 from 7 to 1 +move 5 from 1 to 6 +move 3 from 6 to 4 +move 3 from 8 to 6 +move 2 from 5 to 2 +move 12 from 5 to 9 +move 5 from 6 to 2 +move 2 from 5 to 9 +move 6 from 4 to 9 +move 11 from 7 to 3 +move 1 from 2 to 5 +move 1 from 7 to 8 +move 1 from 5 to 7 +move 1 from 7 to 1 +move 1 from 8 to 1 +move 2 from 4 to 7 +move 2 from 6 to 8 +move 5 from 3 to 6 +move 2 from 7 to 2 +move 2 from 2 to 9 +move 1 from 2 to 9 +move 1 from 1 to 6 +move 35 from 9 to 7 +move 2 from 8 to 7 +move 3 from 3 to 8 +move 5 from 2 to 4 +move 3 from 3 to 7 +move 2 from 4 to 7 +move 4 from 6 to 5 +move 4 from 5 to 9 +move 3 from 4 to 5 +move 1 from 8 to 3 +move 4 from 9 to 8 +move 1 from 9 to 6 +move 38 from 7 to 2 +move 1 from 3 to 5 +move 1 from 1 to 7 +move 4 from 7 to 3 +move 3 from 6 to 1 +move 22 from 2 to 7 +move 1 from 5 to 8 +move 7 from 8 to 4 +move 8 from 2 to 8 +move 3 from 5 to 1 +move 4 from 3 to 9 +move 1 from 8 to 3 +move 1 from 3 to 7 +move 2 from 2 to 3 +move 5 from 8 to 9 +move 3 from 9 to 1 +move 2 from 1 to 7 +move 6 from 2 to 3 +move 6 from 3 to 1 +move 2 from 3 to 6 +move 1 from 6 to 1 +move 14 from 7 to 2 +move 4 from 1 to 6 +move 8 from 1 to 3 +move 4 from 3 to 6 +move 3 from 9 to 5 +move 1 from 8 to 6 +move 1 from 8 to 4 +move 9 from 7 to 1 +move 8 from 2 to 4 +move 4 from 2 to 9 +move 2 from 2 to 1 +move 3 from 5 to 8 +move 1 from 8 to 6 +move 1 from 7 to 8 +move 1 from 6 to 5 +move 3 from 9 to 5 +move 2 from 9 to 5 +move 4 from 3 to 9 +move 3 from 6 to 3 +move 3 from 6 to 9 +move 9 from 4 to 1 +move 1 from 9 to 8 +move 3 from 3 to 6 +move 2 from 7 to 4 +move 4 from 8 to 5 +move 7 from 5 to 6 +move 19 from 1 to 9 +move 5 from 9 to 3 +move 2 from 1 to 6 +move 1 from 4 to 6 +move 4 from 3 to 2 +move 21 from 9 to 7 +move 1 from 1 to 2 +move 1 from 9 to 1 +move 1 from 1 to 8 +move 16 from 7 to 6 +move 24 from 6 to 5 +move 7 from 4 to 5 +move 1 from 8 to 3 +move 2 from 2 to 8 +move 31 from 5 to 8 +move 1 from 4 to 6 +move 2 from 6 to 9 +move 1 from 7 to 4 +move 3 from 7 to 9 +move 1 from 4 to 8 +move 2 from 3 to 5 +move 1 from 2 to 3 +move 1 from 3 to 7 +move 1 from 7 to 9 +move 24 from 8 to 6 +move 1 from 8 to 1 +move 30 from 6 to 1 +move 2 from 5 to 2 +move 1 from 6 to 9 +move 6 from 9 to 7 +move 1 from 6 to 4 +move 1 from 4 to 6 +move 23 from 1 to 3 +move 21 from 3 to 4 +move 4 from 2 to 6 +move 3 from 6 to 1 +move 1 from 5 to 1 +move 4 from 1 to 9 +move 3 from 9 to 6 +move 8 from 1 to 6 +move 4 from 8 to 5 +move 2 from 7 to 5 +move 7 from 4 to 3 +move 3 from 4 to 9 +move 9 from 3 to 9 +move 1 from 7 to 6 +move 6 from 5 to 8 +move 14 from 6 to 2 +move 4 from 8 to 4 +move 7 from 4 to 5 +move 1 from 7 to 9 +move 6 from 4 to 3 +move 13 from 2 to 6 +move 5 from 3 to 7 +move 1 from 3 to 8 +move 1 from 8 to 2 +move 4 from 8 to 3 +move 6 from 6 to 4 +move 2 from 2 to 8 +move 5 from 4 to 7 +move 3 from 7 to 5 +move 1 from 7 to 9 +move 2 from 3 to 9 +move 3 from 7 to 3 +move 1 from 7 to 9 +move 1 from 7 to 9 +move 3 from 4 to 1 +move 6 from 6 to 1 +move 2 from 7 to 5 +move 1 from 3 to 5 +move 11 from 9 to 4 +move 9 from 4 to 5 +move 3 from 3 to 4 +move 1 from 3 to 9 +move 2 from 8 to 1 +move 9 from 1 to 8 +move 22 from 5 to 8 +move 2 from 1 to 3 +move 3 from 4 to 6 +move 14 from 8 to 9 +move 1 from 3 to 9 +move 19 from 9 to 3 +move 3 from 9 to 4 +move 2 from 7 to 2 +move 1 from 4 to 6 +move 1 from 3 to 8 +move 8 from 3 to 1 +move 2 from 9 to 6 +move 1 from 2 to 5 +move 3 from 4 to 9 +move 1 from 2 to 3 +move 20 from 8 to 3 +move 4 from 9 to 5 +move 1 from 4 to 2 +move 26 from 3 to 5 +move 1 from 8 to 3 +move 8 from 1 to 4 +move 1 from 3 to 7 +move 1 from 2 to 1 +move 1 from 1 to 6 +move 1 from 6 to 7 +move 4 from 5 to 3 +move 3 from 4 to 2 +move 5 from 5 to 3 +move 2 from 2 to 6 +move 3 from 3 to 5 +move 2 from 4 to 8 +move 5 from 3 to 9 +move 5 from 9 to 8 +move 19 from 5 to 9 +move 1 from 5 to 2 +move 2 from 7 to 1 +move 1 from 1 to 7 +move 1 from 7 to 4 +move 13 from 9 to 3 +move 8 from 6 to 2 +move 10 from 3 to 5 +move 14 from 5 to 4 +move 7 from 8 to 4 +move 1 from 6 to 2 +move 6 from 3 to 8 +move 4 from 9 to 7 +move 2 from 9 to 8 +move 1 from 7 to 1 +move 3 from 2 to 7 +move 1 from 5 to 3 +move 7 from 8 to 6 +move 5 from 6 to 2 +move 8 from 4 to 5 +move 3 from 5 to 8 +move 3 from 8 to 6 +move 5 from 7 to 9 +move 5 from 3 to 6 +move 1 from 9 to 4 +move 17 from 4 to 7 +move 1 from 8 to 1 +move 12 from 7 to 8 +move 3 from 1 to 4 +move 2 from 4 to 6 +move 8 from 6 to 1 +move 4 from 6 to 3 +move 1 from 7 to 8 +move 5 from 5 to 8 +move 4 from 7 to 1 +move 3 from 2 to 6 +move 2 from 5 to 1 +move 6 from 1 to 6 +move 4 from 3 to 5 +move 4 from 5 to 3 +move 1 from 4 to 8 +move 3 from 3 to 2 +move 17 from 8 to 4 +move 6 from 6 to 3 +move 14 from 4 to 9 +move 1 from 3 to 8 +move 1 from 7 to 4 +move 3 from 8 to 3 +move 5 from 2 to 5 +move 6 from 1 to 7 +move 2 from 6 to 4 +move 4 from 5 to 7 +move 1 from 1 to 5 +move 1 from 6 to 3 +move 10 from 7 to 4 +move 1 from 5 to 4 +move 1 from 2 to 3 +move 15 from 4 to 5 +move 3 from 3 to 1 +move 6 from 2 to 6 +move 1 from 2 to 3 +move 2 from 4 to 7 +move 2 from 7 to 8 +move 1 from 4 to 2 +move 2 from 1 to 7 +move 1 from 7 to 2 +move 12 from 9 to 1 +move 4 from 9 to 5 +move 4 from 6 to 2 +move 1 from 7 to 3 +move 6 from 2 to 4 +move 1 from 8 to 5 +move 2 from 4 to 2 +move 11 from 1 to 7 +move 3 from 1 to 4 +move 17 from 5 to 6 +move 15 from 6 to 4 +move 1 from 8 to 9 +move 10 from 4 to 1 +move 1 from 3 to 9 +move 2 from 6 to 5 +move 1 from 2 to 6 +move 4 from 5 to 6 +move 4 from 1 to 2 +move 6 from 6 to 7 +move 2 from 2 to 6 +move 9 from 4 to 9 +move 6 from 1 to 2 +move 3 from 4 to 1 +move 10 from 9 to 8 +move 4 from 2 to 1 +move 1 from 1 to 2 +move 5 from 8 to 6 +move 1 from 2 to 7 +move 1 from 9 to 4 +move 2 from 6 to 9 +move 13 from 7 to 2 +move 5 from 7 to 5 +move 2 from 5 to 2 +move 1 from 4 to 5 +move 4 from 8 to 4 +move 17 from 2 to 6 +move 3 from 4 to 6 +move 2 from 9 to 1 +move 7 from 6 to 8 +move 1 from 5 to 2 +move 1 from 4 to 1 +move 2 from 9 to 4 +move 1 from 3 to 9 +move 4 from 3 to 7 +move 2 from 8 to 5 +move 3 from 7 to 5 +move 10 from 5 to 8 +move 2 from 2 to 4 +move 6 from 1 to 2 +move 4 from 6 to 3 +move 8 from 2 to 6 +move 1 from 7 to 4 +move 5 from 4 to 5 +move 7 from 6 to 7 +move 5 from 3 to 5 +move 5 from 5 to 2 +move 4 from 8 to 1 +move 6 from 1 to 6 +move 3 from 3 to 2 +move 22 from 6 to 2 +move 1 from 9 to 7 +move 8 from 8 to 6 +move 1 from 7 to 6 +move 2 from 5 to 7 +move 4 from 8 to 5 +move 7 from 6 to 7 +move 2 from 6 to 4 +move 14 from 2 to 1 +move 7 from 1 to 3 +move 12 from 7 to 3 +move 1 from 4 to 3 +move 2 from 5 to 8 +move 2 from 8 to 1 +move 1 from 4 to 3 +move 6 from 2 to 9 +move 6 from 9 to 2 +move 2 from 2 to 7 +move 6 from 7 to 5 +move 13 from 3 to 5 +move 5 from 2 to 6 +move 5 from 6 to 1 +move 2 from 3 to 6 +move 1 from 6 to 5 +move 1 from 6 to 1 +move 3 from 1 to 9 +move 6 from 2 to 7 +move 1 from 2 to 3 +move 24 from 5 to 2 +move 7 from 3 to 7 +move 13 from 7 to 9 +move 4 from 1 to 9 +move 4 from 1 to 6 +move 1 from 5 to 6 +move 16 from 9 to 5 +move 1 from 6 to 4 +move 1 from 5 to 2 +move 5 from 1 to 3 +move 11 from 2 to 1 +move 4 from 9 to 6 +move 1 from 4 to 7 +move 2 from 3 to 4 +move 6 from 6 to 9 +move 1 from 1 to 3 +move 2 from 9 to 4 +move 1 from 7 to 9 +move 4 from 2 to 9 +move 8 from 9 to 2 +move 3 from 3 to 2 +move 1 from 9 to 4 +move 5 from 1 to 7 +move 1 from 4 to 8 +move 2 from 1 to 9 +move 1 from 8 to 7 +move 6 from 5 to 3 +move 1 from 5 to 1 +move 5 from 2 to 3 +move 4 from 1 to 5 +move 4 from 7 to 1 +move 8 from 5 to 8 diff --git a/2022/src/day03.rs b/2022/src/day03.rs index 371246b..26bba9a 100644 --- a/2022/src/day03.rs +++ b/2022/src/day03.rs @@ -31,22 +31,22 @@ pub fn execute() -> Result<(), io::Error> { score2 += get_score(&intersect); } - utils::print_step(1, score as u64); - utils::print_step(2, score2 as u64); + utils::print_step(1, &score); + utils::print_step(2, &score2); Ok(()) } -fn get_score(intersect: &HashSet) -> u32 { +fn get_score(intersect: &HashSet) -> u64 { if intersect.len() > 1 { panic!("More than one overlapping item."); } if let Some(item) = intersect.iter().next() { if *item > 0x60 { - return (*item - 0x60) as u32; + return (*item - 0x60) as u64; } - return ((*item - 0x40) + 26) as u32; + return ((*item - 0x40) + 26) as u64; } panic!("No items in intersection."); diff --git a/2022/src/day04.rs b/2022/src/day04.rs index 7d62234..23a2dd2 100644 --- a/2022/src/day04.rs +++ b/2022/src/day04.rs @@ -29,8 +29,8 @@ pub fn execute() -> Result<(), io::Error> { } } - utils::print_step(1, subsets); - utils::print_step(2, overlaps); + utils::print_step(1, &subsets); + utils::print_step(2, &overlaps); Ok(()) } diff --git a/2022/src/day05.rs b/2022/src/day05.rs new file mode 100644 index 0000000..3ebbcfd --- /dev/null +++ b/2022/src/day05.rs @@ -0,0 +1,65 @@ +use std::io; +use std::vec::Vec; + +use crate::utils; + +pub fn execute() -> Result<(), io::Error> { + let mut stacks1: [Vec; 9] = Default::default(); + let mut stacks2: [Vec; 9] = Default::default(); + + for line in utils::read_lines("input/day05.txt")? { + let text = line?; + + match text { + x if x.contains("[") => { + initialize_stacks(&mut stacks1, &x); + initialize_stacks(&mut stacks2, &x); + }, + x if x.contains("move") => { + move_stacks(&mut stacks1, &x, 1); + move_stacks(&mut stacks2, &x, 2); + }, + _ => {}, + }; + } + + utils::print_step(1, &stacks1.iter().map(|x| x.last().unwrap()).collect::()); + utils::print_step(1, &stacks2.iter().map(|x| x.last().unwrap()).collect::()); + + Ok(()) +} + +fn initialize_stacks(stacks: &mut [Vec], text: &str) { + let mut iter = text.chars(); + iter.next(); // discard the first value + for (i, stack) in iter.step_by(4).zip(stacks) { + if i.is_alphabetic() { stack.insert(0, i); } + } +} + +fn move_stacks(stacks: &mut [Vec], text: &str, step: u8) { + let fields: Vec<_> = text.split(' ').collect(); + let count = fields[1].parse::().unwrap(); + let start = fields[3].parse::().unwrap(); + let end = fields[5].parse::().unwrap(); + let mut crane = vec![]; + + for _ in 0..count { + let item = stacks[start-1].pop().unwrap(); + match step { + 1 => stacks[end-1].push(item), + 2 => crane.push(item), + _ => panic!("Bad step provided to function."), + } + } + + // if we're doing step 1, we're done + if crane.is_empty() { + return; + } + + // step 2 logic + while let Some(item) = crane.pop() { + stacks[end-1].push(item); + } +} diff --git a/2022/src/main.rs b/2022/src/main.rs index 1152448..0574dd6 100644 --- a/2022/src/main.rs +++ b/2022/src/main.rs @@ -2,13 +2,13 @@ use std::env; use std::io; mod utils; -const LATEST: u8 = 4; +const LATEST: u8 = 5; mod day01; mod day02; mod day03; mod day04; -// mod day05; +mod day05; // mod day06; // mod day07; // mod day08; @@ -37,7 +37,7 @@ fn main() -> io::Result<()> { 2 => day02::execute(), 3 => day03::execute(), 4 => day04::execute(), - // 5 => day05::execute(), + 5 => day05::execute(), // 6 => day06::execute(), // 7 => day07::execute(), // 8 => day08::execute(), diff --git a/2022/src/utils.rs b/2022/src/utils.rs index dc1fa5e..e20cdd1 100644 --- a/2022/src/utils.rs +++ b/2022/src/utils.rs @@ -1,12 +1,13 @@ use std::io; use std::io::prelude::*; use std::fs::File; +use std::fmt::Display; pub fn read_lines(filename: &str) -> io::Result>> { let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } -pub fn print_step(step: i8, value: u64) { +pub fn print_step(step: i8, value: &T) { println!("Step {step} solution: {value}"); }