From 774843c3e7bbf2a9bfdd31d9d4197b623d47d0ce Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Tue, 7 Dec 2021 15:23:45 +0100 Subject: [PATCH] Solved Day 6 --- inputs/day6.txt | 1 + inputs/sample.txt | 11 +--------- src/bin/day6.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 inputs/day6.txt create mode 100644 src/bin/day6.rs diff --git a/inputs/day6.txt b/inputs/day6.txt new file mode 100644 index 0000000..aa0969e --- /dev/null +++ b/inputs/day6.txt @@ -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 diff --git a/inputs/sample.txt b/inputs/sample.txt index b258f68..55129f1 100644 --- a/inputs/sample.txt +++ b/inputs/sample.txt @@ -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 diff --git a/src/bin/day6.rs b/src/bin/day6.rs new file mode 100644 index 0000000..c2ab31d --- /dev/null +++ b/src/bin/day6.rs @@ -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> { + let file = File::open("inputs/day6.txt")?; + let mut fishes: Vec = 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 = 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 = 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(()) +}