suspiciously straightforward
This commit is contained in:
parent
2269a7e086
commit
d77c4ed0c9
56
2021/src/day6.rs
Normal file
56
2021/src/day6.rs
Normal 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))
|
||||||
|
}
|
@ -4,14 +4,14 @@ use color_eyre::eyre;
|
|||||||
|
|
||||||
mod lib;
|
mod lib;
|
||||||
use lib::load;
|
use lib::load;
|
||||||
mod day5;
|
mod day6;
|
||||||
|
|
||||||
|
|
||||||
fn main() -> eyre::Result<()> {
|
fn main() -> eyre::Result<()> {
|
||||||
let data = load("data/05.txt")?;
|
let data = load("data/06.txt")?;
|
||||||
|
|
||||||
let start = Instant::now();
|
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 (dur, unit) = format_ns(start.elapsed().as_nanos());
|
||||||
|
|
||||||
let precision = 2.0 - dur.log10().floor();
|
let precision = 2.0 - dur.log10().floor();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user