Day 6: Lanternfish
At a high level, this problem requires modifying a map from ints to ints in a controlled fashion. At a low level:
-
This map’s keys are actually the integers from 0 to 8 (inclusive), so it can just be stored in an array.
-
The “controlled fashion” is merely shifting most values over. In one case we have to also add to said values.
Setup
use crate::Answer;
const N_TIMERS: usize = 9;
type Timers = [usize; N_TIMERS];
fn read_input(input: &str) -> Option<Timers> {
let mut timers = [0; N_TIMERS];
let nums = input
.trim()
.split(',')
.map(|s| s.parse().ok())
.collect::<Option<Vec<usize>>>()?;
for num in nums {
timers[num] += 1;
}
Some(timers)
}
fn tick_in_place(timers: &mut Timers) {
let initial = timers[0];
for i in 0..(N_TIMERS - 1) {
timers[i] = timers[i + 1];
}
timers[8] = initial;
timers[6] += initial;
}
// [usize; 9] implements Copy
fn tick(n_times: usize, timers: &Timers) -> Timers {
let mut timers = *timers;
for _ in 0..n_times {
tick_in_place(&mut timers);
}
timers
}
fn ans_for_input(input: &str) -> Answer<usize, usize> {
let timers = read_input(input).unwrap();
(6, (pt1(&timers), pt2(&timers))).into()
}
pub fn ans() -> Answer<usize, usize> {
ans_for_input(include_str!("sample_input.txt"))
}