#[derive(Copy, Clone, Debug)] struct Fish(u8); fn simulate(swarm: &[Fish], days: usize) -> usize { let mut new_count = [0; 2]; let mut count = [0; 7]; for fish in swarm { count[fish.0 as usize] += 1; } let mut day = 0; let mut birth = 0; for i in 0..days { let today = count[day] + new_count[birth]; new_count[birth] = count[day]; count[day] = today; day = (day + 1) % 7; birth = (birth + 1) % 2; } new_count.into_iter().sum::() + count.into_iter().sum::() } fn part1(swarm: &[Fish]) { println!("Solution to part 1: {}", simulate(swarm, 80)); } fn part2(swarm: &[Fish]) { println!("Solution to part 2: {}", simulate(swarm, 256)); } pub fn run(input: Vec) { let initial_swarm: Vec = input .iter() .map(|s| s.split(',').map(|s| Fish::new(s.parse().unwrap()))) .flatten() .collect(); part1(&initial_swarm); part2(&initial_swarm); } impl Fish { pub fn new(timer: u8) -> Self { Self(timer) } } impl Default for Fish { fn default() -> Self { Self(8) } }