Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/advent-of-code-2021
Path: blob/main/src/day06.rs
97 views
1
pub fn run() {
2
let fish_timers: Vec<usize> = include_str!("inputs/day06")
3
.split(",")
4
.map(|s| s.trim().parse().unwrap())
5
.collect();
6
7
println!("{}", simulate_fish_growth(&fish_timers, 80));
8
println!("{}", simulate_fish_growth(&fish_timers, 256));
9
}
10
11
fn simulate_fish_growth(initial_timers: &[usize], days: usize) -> u64 {
12
let mut fish_by_timer = [0 as u64; 9];
13
for &t in initial_timers {
14
fish_by_timer[t] += 1
15
}
16
17
for _ in 1..=days {
18
// All fish timers decrease by 1; fish with timer=0 become new fish with
19
// timer 8.
20
fish_by_timer.rotate_left(1);
21
// Put the fish that had timer=0 into the timer=6 bucket.
22
fish_by_timer[6] += fish_by_timer[8];
23
}
24
fish_by_timer.iter().sum()
25
}
26
27