Solved Day 6

This commit is contained in:
Sebastian 2021-12-07 15:23:45 +01:00
parent cc17163b56
commit 774843c3e7
3 changed files with 53 additions and 10 deletions

1
inputs/day6.txt Normal file
View File

@ -0,0 +1 @@
2,3,1,3,4,4,1,5,2,3,1,1,4,5,5,3,5,5,4,1,2,1,1,1,1,1,1,4,1,1,1,4,1,3,1,4,1,1,4,1,3,4,5,1,1,5,3,4,3,4,1,5,1,3,1,1,1,3,5,3,2,3,1,5,2,2,1,1,4,1,1,2,2,2,2,3,2,1,2,5,4,1,1,1,5,5,3,1,3,2,2,2,5,1,5,2,4,1,1,3,3,5,2,3,1,2,1,5,1,4,3,5,2,1,5,3,4,4,5,3,1,2,4,3,4,1,3,1,1,2,5,4,3,5,3,2,1,4,1,4,4,2,3,1,1,2,1,1,3,3,3,1,1,2,2,1,1,1,5,1,5,1,4,5,1,5,2,4,3,1,1,3,2,2,1,4,3,1,1,1,3,3,3,4,5,2,3,3,1,3,1,4,1,1,1,2,5,1,4,1,2,4,5,4,1,5,1,5,5,1,5,5,2,5,5,1,4,5,1,1,3,2,5,5,5,4,3,2,5,4,1,1,2,4,4,1,1,1,3,2,1,1,2,1,2,2,3,4,5,4,1,4,5,1,1,5,5,1,4,1,4,4,1,5,3,1,4,3,5,3,1,3,1,4,2,4,5,1,4,1,2,4,1,2,5,1,1,5,1,1,3,1,1,2,3,4,2,4,3,1

View File

@ -1,10 +1 @@
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
3,4,3,1,2

51
src/bin/day6.rs Normal file
View File

@ -0,0 +1,51 @@
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 mut 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 answer2 = 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);
}
}
answer2 = population.iter().fold(0, |a, b| a + b);
println!("Answer1: {}", answer1);
println!("Answer2: {}", answer2);
Ok(())
}