Day 6
This commit is contained in:
parent
9bae58f92e
commit
3dea0ef914
|
@ -0,0 +1 @@
|
||||||
|
1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2
|
|
@ -0,0 +1,41 @@
|
||||||
|
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>()
|
||||||
|
);
|
||||||
|
}
|
|
@ -5,7 +5,8 @@ mod day2;
|
||||||
mod day3;
|
mod day3;
|
||||||
mod day4;
|
mod day4;
|
||||||
mod day5;
|
mod day5;
|
||||||
|
mod day6;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
day5::day5();
|
day6::day6();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue