diff options
Diffstat (limited to 'src/day_6.rs')
| -rw-r--r-- | src/day_6.rs | 55 |
1 files changed, 55 insertions, 0 deletions
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::<usize>() + count.into_iter().sum::<usize>() +} + +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<String>) +{ + let initial_swarm: Vec<Fish> = 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) } +} |
