Day 9 shenanigans
This commit is contained in:
parent
d33a6fb539
commit
6882c31777
2 changed files with 98 additions and 1 deletions
94
src/day9/mod.rs
Normal file
94
src/day9/mod.rs
Normal file
|
@ -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::<isize>().unwrap()).collect::<Vec<_>>());
|
||||||
|
let mut acc = 0i128;
|
||||||
|
|
||||||
|
for sequence in sequences {
|
||||||
|
let mut i = 1usize;
|
||||||
|
let len = sequence.len();
|
||||||
|
let mut sequence_map = HashMap::<usize, Vec::<isize>>::new();
|
||||||
|
|
||||||
|
sequence_map.insert(0, sequence.into_iter().rev().collect());
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut new = Vec::<isize>::with_capacity(len-i);
|
||||||
|
let mut past: Option<isize> = 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::<isize>().unwrap()).collect::<Vec<_>>());
|
||||||
|
let mut acc = 0i128;
|
||||||
|
|
||||||
|
for sequence in sequences {
|
||||||
|
let mut i = 1usize;
|
||||||
|
let len = sequence.len();
|
||||||
|
let mut sequence_map = HashMap::<usize, Vec::<isize>>::new();
|
||||||
|
|
||||||
|
sequence_map.insert(0, sequence.into_iter().rev().collect());
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut new = Vec::<isize>::with_capacity(len-i);
|
||||||
|
let mut past: Option<isize> = 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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
|
|
||||||
use crate::{
|
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;
|
pub mod day1;
|
||||||
|
@ -18,6 +18,7 @@ pub mod day5;
|
||||||
pub mod day6;
|
pub mod day6;
|
||||||
pub mod day7;
|
pub mod day7;
|
||||||
pub mod day8;
|
pub mod day8;
|
||||||
|
pub mod day9;
|
||||||
pub mod fetcher;
|
pub mod fetcher;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
|
@ -118,6 +119,7 @@ async fn main() {
|
||||||
hands: vec![],
|
hands: vec![],
|
||||||
}),
|
}),
|
||||||
8 => Box::new(Day8 { input }),
|
8 => Box::new(Day8 { input }),
|
||||||
|
9 => Box::new(Day9 { input }),
|
||||||
_ => panic!("Invalid day #"),
|
_ => panic!("Invalid day #"),
|
||||||
};
|
};
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
@ -177,6 +179,7 @@ async fn main() {
|
||||||
hands: vec![],
|
hands: vec![],
|
||||||
}),
|
}),
|
||||||
8 => Box::new(Day8 { input }),
|
8 => Box::new(Day8 { input }),
|
||||||
|
9 => Box::new(Day9 { input }),
|
||||||
_ => panic!("Invalid day #"),
|
_ => panic!("Invalid day #"),
|
||||||
};
|
};
|
||||||
let mut timings = Vec::<Duration>::new();
|
let mut timings = Vec::<Duration>::new();
|
||||||
|
|
Loading…
Reference in a new issue