From d7d1a1d840dc0a71a644110078b9fd2e3787d7a6 Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Mon, 6 Dec 2021 09:12:49 +0100 Subject: [PATCH] Add Day 6 --- input/day06 | 1 + src/day05.rs | 2 +- src/day06.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 +++- 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 input/day06 create mode 100644 src/day06.rs diff --git a/input/day06 b/input/day06 new file mode 100644 index 0000000..46b468a --- /dev/null +++ b/input/day06 @@ -0,0 +1 @@ +3,3,5,1,1,3,4,2,3,4,3,1,1,3,3,1,5,4,4,1,4,1,1,1,3,3,2,3,3,4,2,5,1,4,1,2,2,4,2,5,1,2,2,1,1,1,1,4,5,4,3,1,4,4,4,5,1,1,4,3,4,2,1,1,1,1,5,2,1,4,2,4,2,5,5,5,3,3,5,4,5,1,1,5,5,5,2,1,3,1,1,2,2,2,2,1,1,2,1,5,1,2,1,2,5,5,2,1,1,4,2,1,4,2,1,1,1,4,2,5,1,5,1,1,3,1,4,3,1,3,2,1,3,1,4,1,2,1,5,1,2,1,4,4,1,3,1,1,1,1,1,5,2,1,5,5,5,3,3,1,2,4,3,2,2,2,2,2,4,3,4,4,4,1,2,2,3,1,1,4,1,1,1,2,1,4,2,1,2,1,1,2,1,5,1,1,3,1,4,3,2,1,1,1,5,4,1,2,5,2,2,1,1,1,1,2,3,3,2,5,1,2,1,2,3,4,3,2,1,1,2,4,3,3,1,1,2,5,1,3,3,4,2,3,1,2,1,4,3,2,2,1,1,2,1,4,2,4,1,4,1,4,4,1,4,4,5,4,1,1,1,3,1,1,1,4,3,5,1,1,1,3,4,1,1,4,3,1,4,1,1,5,1,2,2,5,5,2,1,5 \ No newline at end of file diff --git a/src/day05.rs b/src/day05.rs index ec7033b..d915df9 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -37,7 +37,7 @@ impl Day for Day05 { } } -fn covered_twice(vents: &Vec) -> usize { +fn covered_twice(vents: &[Path]) -> usize { let mut m = HashMap::new(); for vent in vents { for cover in &vent.covers { diff --git a/src/day06.rs b/src/day06.rs new file mode 100644 index 0000000..8cbeac4 --- /dev/null +++ b/src/day06.rs @@ -0,0 +1,82 @@ +use std::collections::HashMap; +use crate::Day; + +pub struct Day06(Vec); + +impl Day for Day06 { + fn init(content: String) -> Self { + Self(content.split(',').map(|x| x.parse().expect("This is not an int!")).collect::>()) + } + + fn part1(&self) -> String { + format!("{}", self.do_hashmap_magic(80)) + } + + fn part2(&self) -> String { + format!("{}", self.do_hashmap_magic(256)) + } +} + +impl Day06 { + fn do_hashmap_magic(&self, days: u64) -> u64 { + let x = self.0.clone(); + let mut hm = HashMap::::new(); + for z in x { + let e = hm.entry(z).or_insert(0); + *e += 1; + } + for _ in 0..days { + let mut to_add = 0; + for num in 0..=8 { + if num == 0 { + to_add = *hm.get(&0).unwrap_or(&0); + } else { + let entry = { *hm.get(&num).unwrap_or(&0) }; + let prev = hm.entry(num - 1).or_insert(0); + *prev = entry; + } + } + { + let en = hm.entry(8).or_insert(0); + *en = to_add; + } + { + let en = hm.entry(6).or_insert(0); + *en += to_add; + } + } + hm.values().sum::() + } +} + +#[cfg(test)] +mod tests { + use crate::day06::Day06; + use crate::day::Day; + + const INPUT: &str = r"3,4,3,1,2"; + + #[test] + fn part1_test() { + let d = Day06::init(INPUT.to_string()); + assert_eq!("5934", d.part1()); + } + + #[test] + fn part2_test() { + let d = Day06::init(INPUT.to_string()); + assert_eq!("26984457539", d.part2()); + } + + #[test] + fn part1_real() { + let d = Day06::init(crate::load_input("06")); + assert_eq!("362740", d.part1()); + } + + #[test] + fn part2_real() { + let d = Day06::init(crate::load_input("06")); + assert_eq!("1644874076764", d.part2()); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5258c4f..1f08bc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use crate::day02::Day02; use crate::day03::Day03; use crate::day04::Day04; use crate::day05::Day05; +use crate::day06::Day06; use crate::day::Day; mod day; @@ -14,6 +15,7 @@ mod day02; mod day03; mod day04; mod day05; +mod day06; fn load_input(day: &str) -> String { read_to_string(format!("./input/day{}", day)).expect("Could not load input") @@ -52,7 +54,8 @@ fn main() { Box::new(Day02::init(load_input("02"))), Box::new(Day03::init(load_input("03"))), Box::new(Day04::init(load_input("04"))), - Box::new(Day05::init(load_input("05")))]; + Box::new(Day05::init(load_input("05"))), + Box::new(Day06::init(load_input("06")))]; let _verbosity = matches.occurrences_of("v"); if matches.is_present("all") {