AOC Day 6

mail@pastecode.io avatar
unknown
rust
2 years ago
1.3 kB
3
Indexable
Never
use std::collections::HashMap;
use std::io::{self, BufRead};
use std::fs::File;

fn main() {
    let file = File::open("./input.txt").unwrap();
    let lines = io::BufReader::new(file).lines();

    let mut str_lines: Vec<String> = Vec::new();

    for line in lines {
        if let Ok(input) = line {
            str_lines.push(input);
        }
    }

    let max_life :i32 = 9;
    let mut fish_map : HashMap<i32, u64> = HashMap::new();

    //Init the map
    for i in 0..max_life{
        fish_map.insert(i, 0);
    }

    //Initial state
    for line in str_lines{
        for token in line.split(','){
            let idx:i32 = token.parse().unwrap();
            *fish_map.get_mut(&idx).unwrap() += 1;
        }
    }

    let numitr = 256;

    for _iteration in 0..numitr{
        let new_count = *fish_map.get(&0).unwrap();

        for i in 0..max_life - 1 {
            *fish_map.get_mut(&i).unwrap() = *fish_map.get_mut(&(i+1)).unwrap();
        }

        *fish_map.get_mut(&6).unwrap() += new_count;
        *fish_map.get_mut(&8).unwrap() = new_count;
    }

    //Sum fish
    let mut sum : u64 = 0;
    for i in 0..max_life{
        sum += fish_map.get(&i).unwrap();
    }

    println!("Num fish {}", sum);
}