summaryrefslogtreecommitdiff
path: root/2021/day6/src/main.rs
blob: 46a148cb9d2159eaf4a7c5200d32a55ff4435c84 (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
34
35
36
use std::fs::File;
use std::io::{BufRead, BufReader};

fn main() -> std::io::Result<()> {
    let file = File::open("input.txt")?;
    let mut reader = BufReader::new(file);
    let mut buffer = String::new();

    reader.read_line(&mut buffer)?;

    let mut lanternfishes: Vec<_> = buffer
        .trim()
        .split(',')
        .map(|x| x.parse::<i16>().unwrap())
        .collect::<Vec<i16>>();

    for _ in 0..80 {
        let mut n = 0;
        for lanternfish in lanternfishes.iter_mut() {
            if *lanternfish == 0 {
                *lanternfish = 6;
                n += 1;
            } else {
                *lanternfish -= 1;
            }
        }

        for _ in 0..n {
            lanternfishes.push(8);
        }
    }

    println!("{}", lanternfishes.len());

    Ok(())
}