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))
    }
}