Solution for 2021: day 6

[?]
Dec 10, 2021, 2:05 AM
XNSA4VSTUFEDHTSTTIR2RQVPK7JH7TBJFU3VMIMDAECEKN2GBATQC

Dependencies

Change contents

  • file addition: day6.rs (----------)
    [5.16]
    fn main() {
    use std::io::BufRead;
    let mut pops: [u128; 9] = [0; 9];
    let filename = std::env::args().nth(1).expect("Expected filename");
    let file = std::io::BufReader::new(
    std::fs::File::open(<String as AsRef<std::path::Path>>::as_ref(
    &filename,
    ))
    .unwrap(),
    );
    for i in file.split(b',') {
    let i: usize = std::str::from_utf8(&i.unwrap())
    .unwrap()
    .trim()
    .parse()
    .unwrap();
    pops[i] += 1;
    }
    for g in 1..=256 {
    let mut next: [u128; 9] = [0; 9];
    for (i, p) in pops.iter().enumerate() {
    for j in advance_lanternfish(i as u8) {
    next[j as usize] += p;
    }
    }
    pops = next;
    if g == 80 || g == 256 {
    println!("Day {}: {}", g, pops.iter().sum::<u128>());
    }
    }
    }
    fn advance_lanternfish(timer: u8) -> Box<dyn Iterator<Item = u8>> {
    use std::iter::once;
    if timer == 0 {
    Box::new(once(6).chain(once(8)))
    } else {
    Box::new(once(timer - 1))
    }
    }
  • edit in 2021/day5.rs at line 136
    [2.4161][2.4161:4162]()
  • edit in 2021/Cargo.toml at line 30
    [6.1457]
    [[bin]]
    name = "day6"
    path = "day6.rs"