AoC2021/src/bin/day6.rs

51 lines
1.1 KiB
Rust
Raw Permalink Normal View History

2021-12-07 15:23:45 +01:00
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")?;
2021-12-11 14:28:26 +01:00
let fishes: Vec<u64> = io::BufReader::new(file)
2021-12-07 15:23:45 +01:00
.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);
}
}
2021-12-11 14:28:26 +01:00
let answer2 = population.iter().fold(0, |a, b| a + b);
2021-12-07 15:23:45 +01:00
println!("Answer1: {}", answer1);
println!("Answer2: {}", answer2);
Ok(())
}