This commit is contained in:
parent
f2f63bbc47
commit
0c4593f8fe
28
src/day06.rs
28
src/day06.rs
|
@ -1,35 +1,31 @@
|
|||
use crate::Day;
|
||||
|
||||
pub struct Day06(Vec<u8>);
|
||||
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::<Vec<_>>())
|
||||
let c = content.split(',').map(|x| x.parse().expect("This is not an int!")).collect::<Vec<usize>>();
|
||||
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]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue