suspiciously straightforward

This commit is contained in:
Joseph Montanaro 2021-12-08 22:20:01 -08:00
parent 2269a7e086
commit d77c4ed0c9
2 changed files with 59 additions and 3 deletions

56
2021/src/day6.rs Normal file
View File

@ -0,0 +1,56 @@
use color_eyre::eyre;
struct Ocean {
fish: [usize; 9],
}
impl Ocean {
fn from(ages: &[usize]) -> Self {
let mut ocean = Ocean {fish: [0; 9]};
for n in ages {
ocean.fish[*n] += 1;
}
ocean
}
fn step(&mut self) {
let mut new = [0; 9];
// 0 is a special case
new[8] += self.fish[0]; // newly-created fish
new[6] += self.fish[0]; // existing fish with timers reset
for i in 1..9 {
new[i - 1] += self.fish[i];
}
self.fish = new;
}
fn sum(&self) -> usize {
self.fish.iter().sum()
}
fn step_by(&mut self, n: usize) {
for _ in 0..n {
self.step();
}
}
}
pub fn run(data: &str) -> eyre::Result<(usize, usize)> {
let mut ages = Vec::new();
for a in data.trim().split(',') {
ages.push(a.parse::<usize>()?);
}
let mut ocean = Ocean::from(&ages);
ocean.step_by(80);
let one = ocean.sum();
ocean.step_by(256 - 80);
let two = ocean.sum();
Ok((one, two))
}

View File

@ -4,14 +4,14 @@ use color_eyre::eyre;
mod lib;
use lib::load;
mod day5;
mod day6;
fn main() -> eyre::Result<()> {
let data = load("data/05.txt")?;
let data = load("data/06.txt")?;
let start = Instant::now();
let (one, two) = day5::run(&data)?;
let (one, two) = day6::run(&data)?;
let (dur, unit) = format_ns(start.elapsed().as_nanos());
let precision = 2.0 - dur.log10().floor();