From 11b2fd042a72eb93fb0741e2dec7d0ec035178f8 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 8 Dec 2021 10:05:20 +0100 Subject: Yes, I totally didn't forget to commit --- src/day_6.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/day_6.rs (limited to 'src/day_6.rs') diff --git a/src/day_6.rs b/src/day_6.rs new file mode 100644 index 0000000..c9d7d86 --- /dev/null +++ b/src/day_6.rs @@ -0,0 +1,55 @@ +#[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) } +} -- cgit v1.2.3-70-g09d2