diff --git a/rust/day6/src/main.rs b/rust/day6/src/main.rs index efb585c..058e2d7 100644 --- a/rust/day6/src/main.rs +++ b/rust/day6/src/main.rs @@ -1,5 +1,6 @@ use std::fs; use std::io::BufRead; +use std::collections::HashMap; static PATH: &'static str = "../../inputs/input-day6"; @@ -41,15 +42,50 @@ fn part1() { println!("total: {}", x.len()); } -fn part2() { - let mut x = load_file().unwrap(); +fn get_total(v: &HashMap) -> u64 { + let mut total = 0; - for i in 0..256 { - run_day(&mut x); - println!("day: {} fish: {}", i+1, x.len()); + for x in v { + total += x.1; } - println!("total: {}", x.len()); + return total; +} + +fn run_day_2(v: &HashMap) -> HashMap { + let mut state = HashMap::new(); + for i in (0..=8).rev() { + if v.contains_key(&i) { + if let Some(cnt) = v.get(&i) { + if i == 0 { + let c = state.entry(6).or_insert(0); + *c += cnt; + state.insert(8, *cnt); + } else { + state.insert(i - 1, *cnt); + } + } + } + } + + return state; +} + +fn part2() { + let x = load_file().unwrap(); + + let mut fish = HashMap::::new(); + + for val in x { + let n = fish.entry(val).or_insert(0); + *n += 1; + } + + for _ in 0..256 { + fish = run_day_2(&fish); + } + + println!("total: {}", get_total(&fish)); } fn main() {