diff --git a/src/day06.rs b/src/day06.rs index b9fd7d5..57d82ea 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -1,35 +1,31 @@ use crate::Day; -pub struct Day06(Vec); +pub struct Day06([u64; 9]); impl Day for Day06 { fn init(content: String) -> Self { - Self(content.split(',').map(|x| x.parse().expect("This is not an int!")).collect::>()) + let c = content.split(',').map(|x| x.parse().expect("This is not an int!")).collect::>(); + let mut w: [u64; 9] = [0, 0, 0, 0, 0, 0, 0, 0, 0]; + for z in c { + w[z as usize] += 1; + } + Self(w) } fn part1(&self) -> String { - format!("{}", self.do_hashmap_magic(80)) + format!("{}", self.do_magic(80)) } fn part2(&self) -> String { - format!("{}", self.do_hashmap_magic(256)) + format!("{}", self.do_magic(256)) } } impl Day06 { - - fn do_hashmap_magic(&self, days: u64) -> u64 { - let x = self.0.clone(); - let mut w: [u64; 9] = [0, 0, 0, 0, 0, 0, 0, 0, 0]; - for z in x { - w[z as usize] += 1; - } - for _ in 0..days { - w = Self::foo(w); - } - w.iter().sum() + fn do_magic(&self, days: u64) -> u64 { + (0..days).fold(self.0, Self::foo).iter().sum() } - fn foo([a, b, c, d, e, f, g, h, i]: [u64; 9]) -> [u64; 9] { + fn foo([a, b, c, d, e, f, g, h, i]: [u64; 9], _: u64) -> [u64; 9] { [b, c, d, e, f, g, h + a, i, a] } }