42 lines
1.0 KiB
Rust
42 lines
1.0 KiB
Rust
|
pub fn day6() {
|
||
|
// let input = "3,4,3,1,2";
|
||
|
let input = include_str!("../input/day6.txt");
|
||
|
|
||
|
let initial_fishes = input
|
||
|
.trim()
|
||
|
.split(",")
|
||
|
.map(|s| s.parse::<usize>().unwrap())
|
||
|
.collect::<Vec<_>>();
|
||
|
|
||
|
count_fishes_after(&initial_fishes, 18);
|
||
|
count_fishes_after(&initial_fishes, 80);
|
||
|
count_fishes_after(&initial_fishes, 256);
|
||
|
}
|
||
|
|
||
|
fn count_fishes_after(initial_fishes: &[usize], days: u32) {
|
||
|
let mut fish_batches = [0_u64; 9];
|
||
|
|
||
|
for f in initial_fishes {
|
||
|
fish_batches[*f] += 1;
|
||
|
}
|
||
|
|
||
|
for _ in 1..=days {
|
||
|
let old_batches = fish_batches;
|
||
|
fish_batches = [0; 9];
|
||
|
|
||
|
for counter in 0..=7 {
|
||
|
fish_batches[counter] = old_batches[counter + 1];
|
||
|
}
|
||
|
|
||
|
// each fish previously at 0 spawns a new fish with counter 8 and resets its counter to 6
|
||
|
fish_batches[8] = old_batches[0];
|
||
|
fish_batches[6] += old_batches[0];
|
||
|
}
|
||
|
|
||
|
println!(
|
||
|
"total fishes after {} days: {}",
|
||
|
days,
|
||
|
fish_batches.iter().sum::<u64>()
|
||
|
);
|
||
|
}
|