diff options
| author | Arne Dußin | 2021-12-08 10:05:20 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-12-08 10:05:20 +0100 |
| commit | 11b2fd042a72eb93fb0741e2dec7d0ec035178f8 (patch) | |
| tree | bf696b91bdec3785cf9d28e5abab26fd088c017a /src/day_6.rs | |
| parent | 564df712a04cdf65529e288ab9978d8196e8cd9d (diff) | |
| download | aoc2021-11b2fd042a72eb93fb0741e2dec7d0ec035178f8.tar.gz aoc2021-11b2fd042a72eb93fb0741e2dec7d0ec035178f8.zip | |
Yes, I totally didn't forget to commit
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) } +} |
