51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
use std::error::Error;
|
|
use std::fs::File;
|
|
use std::io::{self, BufRead};
|
|
use std::vec::Vec;
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let file = File::open("inputs/day6.txt")?;
|
|
let fishes: Vec<u64> = io::BufReader::new(file)
|
|
.lines()
|
|
.nth(0)
|
|
.unwrap()
|
|
.unwrap()
|
|
.split(",")
|
|
.map(|f| f.parse().unwrap())
|
|
.collect();
|
|
|
|
let mut answer1 = 0;
|
|
|
|
let mut population: Vec<u64> = Vec::new();
|
|
population.resize(9, 0);
|
|
for fish in fishes {
|
|
population[fish as usize] += 1;
|
|
}
|
|
|
|
for day in 0..256 {
|
|
let mut next_population: Vec<u64> = Vec::new();
|
|
next_population.resize(9, 0);
|
|
|
|
next_population[6] = population[0];
|
|
next_population[8] = population[0];
|
|
|
|
for i in 1..9 {
|
|
next_population[i - 1] += population[i];
|
|
}
|
|
|
|
population = next_population;
|
|
|
|
if day == 18 {
|
|
answer1 = population.iter().fold(0, |a, b| a + b);
|
|
}
|
|
}
|
|
|
|
let answer2 = population.iter().fold(0, |a, b| a + b);
|
|
|
|
println!("Answer1: {}", answer1);
|
|
|
|
println!("Answer2: {}", answer2);
|
|
|
|
Ok(())
|
|
}
|