From d6725a3eec4720ddaab5b318c2acd77ffe5b5734 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 11 Dec 2020 05:07:00 +0000 Subject: [PATCH] day10-part1: small refactor --- day10/Cargo.toml | 3 +- day10/src/diff.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ day10/src/main.rs | 71 +++++++++----------------------------------- 3 files changed, 91 insertions(+), 58 deletions(-) create mode 100644 day10/src/diff.rs diff --git a/day10/Cargo.toml b/day10/Cargo.toml index d275052..e850e34 100644 --- a/day10/Cargo.toml +++ b/day10/Cargo.toml @@ -7,7 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -test=[] + +test = [] [dependencies] ad-hoc-iter = "0.2.2" diff --git a/day10/src/diff.rs b/day10/src/diff.rs new file mode 100644 index 0000000..a7b8b28 --- /dev/null +++ b/day10/src/diff.rs @@ -0,0 +1,75 @@ +use super::*; + +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +pub struct Diffs([usize; 3]); + +impl ops::Add for Diffs +{ + type Output = Self; + fn add(self, other: Self) -> Self::Output{ + Self([ + self.0[0] + other.0[0], + self.0[1] + other.0[1], + self.0[2] + other.0[2], + ]) + } +} + +impl Sum for Diffs +{ + fn sum(iter: I) -> Self + where I: Iterator + { + iter.fold(Default::default(), |x, y| x + y) + } +} + +impl ops::AddAssign for Diffs +{ + fn add_assign(&mut self, rhs: Self) { + self.0[0] += rhs.0[0]; + self.0[1] += rhs.0[1]; + self.0[2] += rhs.0[2]; + } +} + +impl Diffs +{ + #[inline] pub fn jd1(&self) -> usize + { + self.0[0] + } + #[inline] pub fn jd2(&self) -> usize + { + self.0[1] + } + #[inline] pub fn jd3(&self) -> usize + { + self.0[2] + } + + #[inline] pub fn at(&self, idx: usize) -> &usize + { + &self.0[idx] + } + #[inline] pub fn at_mut(&mut self, idx: usize) -> &mut usize + { + &mut self.0[idx] + } +} + +impl From<(usize,usize,usize)> for Diffs +{ + fn from((f0,f1,f2): (usize,usize,usize)) -> Self + { + Self([f0,f1,f2]) + } +} +impl From for (usize,usize,usize) +{ + fn from(from: Diffs) -> Self + { + (from.jd1(), from.jd2(), from.jd3()) + } +} + diff --git a/day10/src/main.rs b/day10/src/main.rs index 65f18d5..db28dc7 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -10,55 +10,18 @@ use std::{ num::NonZeroU8, ops, iter, + iter::Sum, }; use smallmap::Map; + mod input; +mod diff; +use diff::Diffs; const INPUT: &str = "input"; type Adaptors = Map; - -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] -struct Diffs([usize; 3]); - -impl ops::Add for Diffs -{ - type Output = Self; - fn add(self, other: Self) -> Self::Output{ - Self([ - self.0[0] + other.0[0], - self.0[1] + other.0[1], - self.0[2] + other.0[2], - ]) - } -} - -impl ops::AddAssign for Diffs -{ - fn add_assign(&mut self, rhs: Self) { - self.0[0] += rhs.0[0]; - self.0[1] += rhs.0[1]; - self.0[2] += rhs.0[2]; - } -} - -impl Diffs -{ - pub fn jd1(&self) -> usize - { - self.0[0] - } - pub fn jd2(&self) -> usize - { - self.0[1] - } - pub fn jd3(&self) -> usize - { - self.0[2] - } -} - fn parse_input(file: &str) -> Result, io::Error> { let file = OpenOptions::new().read(true).open(file)?; @@ -77,13 +40,13 @@ fn find_smallest(map: &Adaptors, finding: impl Into) -> (Option, Diffs) { let shim = finding + $l; debug_assert!(shim > 0); - //eprintln!("Looking up {}", shim); + match map.get(&unsafe { NonZeroU8::new_unchecked(shim) }).map(|_| shim) { Some(yes) if !set => { set = true; - diffs.0[($l-1)] += 1; + *diffs.at_mut($l-1) += 1; Some(yes) }, x => x @@ -124,21 +87,15 @@ fn main() -> Result<(), Box> map }; debug_assert_eq!(all_jolts.num_pages(), 1); // Assert we have space efficiency. - - let mut current = 0; - #[allow(unused_variables)] - let mut sum=0; - let mut whole_diffs = Diffs::default(); - loop { - if let (Some(next), diffs) = find_smallest(&all_jolts, current) { - current = next; - sum+=1; - whole_diffs += diffs; + + let diffs: Diffs = std::iter::successors(Some((0, Diffs::default())), |&(next, _)| { + if let (Some(next), d) = find_smallest(&all_jolts, next) { + Some((next, d)) } else { - break; + None } - } - - println!("{}", whole_diffs.jd1() * whole_diffs.jd3()); + }).map(|(_, d)| d).sum(); + + println!("{}", diffs.jd1() * diffs.jd3()); Ok(()) }