summaryrefslogtreecommitdiff
path: root/06/src/part-2.rs
blob: c9ea49908a1254ad72315bcad8e051560f4ce652 (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
use std::io::{BufRead};

pub fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() != 2 { panic!("Need exactly 1 argument"); }
    let num_days: usize = args[1].parse().expect("Need number of days as argument");
    
    let mut stdin = std::io::stdin();
    let mut handle = stdin.lock();

    let input: String = {
        let mut buf = String::new();
        handle.read_line(&mut buf).unwrap();
        String::from(buf.trim_end())
    };

    // counts[i] is the number of fish that breed on days that are divisible by i.
    let mut counts: [usize; 7] = [0; 7];
    let mut newborn_counts: [usize; 9] = [0; 9];
    input.split(',').map(|x| x.parse::<usize>().expect("Malformed input")).for_each(|x| counts[x % 7] += 1);

    for i in 0..num_days {
        let tmp = newborn_counts[i % 9];
        newborn_counts[i % 9] += counts[i % 7];
        counts[i % 7] += tmp;
    }

    println!("After {} days there are {} fishiess", num_days, counts.into_iter().sum::<usize>() + newborn_counts.into_iter().sum::<usize>());
}