From 6882c3177727dfa43683a7016ca41ddb10988528 Mon Sep 17 00:00:00 2001 From: Ty Date: Fri, 8 Dec 2023 22:49:11 -0700 Subject: [PATCH] Day 9 shenanigans --- src/day9/mod.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++- 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/day9/mod.rs diff --git a/src/day9/mod.rs b/src/day9/mod.rs new file mode 100644 index 0000000..52d3817 --- /dev/null +++ b/src/day9/mod.rs @@ -0,0 +1,94 @@ +use std::collections::HashMap; + +use crate::utils::Day; + +#[derive(Debug, Default)] +pub struct Day9 { + pub input: String, +} + +impl Day for Day9 { + fn part1(&mut self) -> String { + let sequences = self.input.lines().map(|l| l.split_ascii_whitespace().map(|n| n.parse::().unwrap()).collect::>()); + let mut acc = 0i128; + + for sequence in sequences { + let mut i = 1usize; + let len = sequence.len(); + let mut sequence_map = HashMap::>::new(); + + sequence_map.insert(0, sequence.into_iter().rev().collect()); + + loop { + let mut new = Vec::::with_capacity(len-i); + let mut past: Option = None; + for num in sequence_map.get(&(i-1)).unwrap() { + if let Some(past_unwrapped) = past { + new.push(past_unwrapped - num); + } + past = Some(*num); + } + + let done = new.iter().all(|n| n == &0); + if done { new.push(0); } + sequence_map.insert(i, new); + + if done { break } + else { i += 1; } + } + + for j in (0..=i).rev() { + let diff = *sequence_map.get(&j).unwrap().first().unwrap(); + let Some(modify_arr) = sequence_map.get_mut(&(j-1)) else { break }; + + modify_arr.insert(0, *modify_arr.first().unwrap() + diff); + } + + acc += sequence_map[&0][0] as i128; + } + + acc.to_string() + } + + fn part2(&mut self) -> String { + let sequences = self.input.lines().map(|l| l.split_ascii_whitespace().map(|n| n.parse::().unwrap()).collect::>()); + let mut acc = 0i128; + + for sequence in sequences { + let mut i = 1usize; + let len = sequence.len(); + let mut sequence_map = HashMap::>::new(); + + sequence_map.insert(0, sequence.into_iter().rev().collect()); + + loop { + let mut new = Vec::::with_capacity(len-i); + let mut past: Option = None; + for num in sequence_map.get(&(i-1)).unwrap() { + if let Some(past_unwrapped) = past { + new.push(past_unwrapped - num); + } + past = Some(*num); + } + + let done = new.iter().all(|n| n == &0); + if done { new.push(0); } + sequence_map.insert(i, new); + + if done { break } + else { i += 1; } + } + + for j in (0..=i).rev() { + let diff = *sequence_map.get(&j).unwrap().last().unwrap(); + let Some(modify_arr) = sequence_map.get_mut(&(j-1)) else { break }; + + modify_arr.push(*modify_arr.last().unwrap() - diff); + } + + acc += *sequence_map[&0].last().unwrap() as i128; + } + + acc.to_string() + } +} diff --git a/src/main.rs b/src/main.rs index 4f04373..3574aa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::{ use clap::{Parser, Subcommand, ValueEnum}; use crate::{ - day1::Day1, day2::Day2, day3::Day3, day4::Day4, day5::Day5, day6::Day6, day7::Day7, day8::Day8, utils::Day, + day1::Day1, day2::Day2, day3::Day3, day4::Day4, day5::Day5, day6::Day6, day7::Day7, day8::Day8, day9::Day9, utils::Day, }; pub mod day1; @@ -18,6 +18,7 @@ pub mod day5; pub mod day6; pub mod day7; pub mod day8; +pub mod day9; pub mod fetcher; pub mod utils; @@ -118,6 +119,7 @@ async fn main() { hands: vec![], }), 8 => Box::new(Day8 { input }), + 9 => Box::new(Day9 { input }), _ => panic!("Invalid day #"), }; let start = std::time::Instant::now(); @@ -177,6 +179,7 @@ async fn main() { hands: vec![], }), 8 => Box::new(Day8 { input }), + 9 => Box::new(Day9 { input }), _ => panic!("Invalid day #"), }; let mut timings = Vec::::new();