From cb974c7daa5e19b4f781b35eaf0679076bdcf739 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 6 Dec 2021 20:55:11 +0200 Subject: [PATCH] feat: solve day 6, part 1 & 2 --- input/6.txt | 1 + src/day6.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 3 files changed, 83 insertions(+) create mode 100644 input/6.txt create mode 100644 src/day6.rs diff --git a/input/6.txt b/input/6.txt new file mode 100644 index 0000000..87141dd --- /dev/null +++ b/input/6.txt @@ -0,0 +1 @@ +3,1,4,2,1,1,1,1,1,1,1,4,1,4,1,2,1,1,2,1,3,4,5,1,1,4,1,3,3,1,1,1,1,3,3,1,3,3,1,5,5,1,1,3,1,1,2,1,1,1,3,1,4,3,2,1,4,3,3,1,1,1,1,5,1,4,1,1,1,4,1,4,4,1,5,1,1,4,5,1,1,2,1,1,1,4,1,2,1,1,1,1,1,1,5,1,3,1,1,4,4,1,1,5,1,2,1,1,1,1,5,1,3,1,1,1,2,2,1,4,1,3,1,4,1,2,1,1,1,1,1,3,2,5,4,4,1,3,2,1,4,1,3,1,1,1,2,1,1,5,1,2,1,1,1,2,1,4,3,1,1,1,4,1,1,1,1,1,2,2,1,1,5,1,1,3,1,2,5,5,1,4,1,1,1,1,1,2,1,1,1,1,4,5,1,1,1,1,1,1,1,1,1,3,4,4,1,1,4,1,3,4,1,5,4,2,5,1,2,1,1,1,1,1,1,4,3,2,1,1,3,2,5,2,5,5,1,3,1,2,1,1,1,1,1,1,1,1,1,3,1,1,1,3,1,4,1,4,2,1,3,4,1,1,1,2,3,1,1,1,4,1,2,5,1,2,1,5,1,1,2,1,2,1,1,1,1,4,3,4,1,5,5,4,1,1,5,2,1,3 diff --git a/src/day6.rs b/src/day6.rs new file mode 100644 index 0000000..efb13ce --- /dev/null +++ b/src/day6.rs @@ -0,0 +1,81 @@ +use std::num::ParseIntError; + +pub fn parse_input(input: &str) -> Result, ParseIntError> { + input.trim_end().split_terminator(',') + .map(|s| s.parse()) + .collect() +} + +fn simulate_step(fishes: &mut Vec) { + for i in 0..fishes.len() { + if fishes[i] == 0 { + fishes[i] = 7; + fishes.push(9); + } + } + + for i in 0..fishes.len() { + if fishes[i] > 0 { + fishes[i] -= 1; + } + } +} + +pub fn part1(input: &[i32]) -> u32 { + let mut fishes = input.to_vec(); + + for _ in 0..80 { + simulate_step(&mut fishes) + } + + fishes.len() as u32 +} + +// Instead of storing each fishes cycle as individual values group them up +// by there cycles. Because it dosen't matter where the fish is in the list. +// So just make an array of size 9 for the 9 possible fish cycle timers. +// And one extra group, for accounting for the delay that when the timer is 0, +// they produce a new fish only on the next turn. +pub fn part2(input: &[i32]) -> u64 { + let mut groups: [u64; 10] = [0; 10]; + + for fish in input.iter() { + groups[*fish as usize + 1] += 1; + } + + for _ in 0..256 { + for i in 1..10 { + groups[i-1] += groups[i]; + groups[i] = 0; + } + groups[7] += groups[0]; + groups[9] += groups[0]; + groups[0] = 0; + } + + let mut count: u64 = 0; + for amount in groups.iter() { + count += amount; + } + count +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_example() { + let input = [3,4,3,1,2]; + let result = part1(&input); + assert_eq!(result, 5934); + } + + #[test] + fn part2_example() { + let input = [3,4,3,1,2]; + let result = part2(&input); + assert_eq!(result, 26984457539 as u64); + } +} + diff --git a/src/main.rs b/src/main.rs index 859d542..01f2a92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod day2; mod day3; mod day4; mod day5; +mod day6; use std::{env, process}; use std::fs::File;