summaryrefslogtreecommitdiffstats
path: root/2021/day6/src/main.rs
blob: 378764ef30c3368ec9249237c6e29232431225ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() != 2 {
        eprintln!("Usage: {} <filename>", args[0]);
        std::process::exit(1);
    }
    let mut smart_lanternfish: [u64; 9] = [0; 9];

    let foo: String = std::fs::read_to_string(&args[1]).unwrap().parse().unwrap();
    let mut lanternfish: Vec<u8> = Vec::new();
    lanternfish.extend(foo.trim().split(",").map(|x| x.parse::<u8>().unwrap()));

    for initial_fish in &lanternfish {
        smart_lanternfish[*initial_fish as usize] += 1;
    }

    // println!("{:?}", smart_lanternfish);
    for _day in 0..256 {
        smart_lanternfish.rotate_left(1);
        smart_lanternfish[6] += smart_lanternfish[8];
        // println!(
        //     "After {day:>width$} days: {lanternfish:?}",
        //     day = _day,
        //     width = 2,
        //     lanternfish = smart_lanternfish
        // );
    }

    println!("{}", smart_lanternfish.iter().sum::<u64>());
}