From 246e7a7b72b29f2190d07996a6807c161b5f6b4a Mon Sep 17 00:00:00 2001 From: mflinn-broad Date: Thu, 16 Dec 2021 18:15:57 -0500 Subject: [PATCH] finish day 14 --- src/days/day14.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/days/day14.rs b/src/days/day14.rs index dc407ca..2f5ad96 100644 --- a/src/days/day14.rs +++ b/src/days/day14.rs @@ -10,12 +10,21 @@ type Input = ( pub fn run() { let raw_input = util::read_input("inputs/day14.txt").unwrap(); let input = process(&raw_input); - println!("Part 1: {}", part_1(input)); + println!("Part 1: {}", part_1(input.clone())); + println!("Part 2: {}", part_2(input)); } fn part_1(input: Input) -> usize { + polymer_strength(input, 10) +} + +fn part_2(input: Input) -> usize { + polymer_strength(input, 40) +} + +fn polymer_strength(input: Input, steps: usize) -> usize { let (mappings, mut singles, pairs) = input; - (0..10).fold(pairs, |old_pairs, _| { + (0..steps).fold(pairs, |old_pairs, _| { let mut new_pairs: HashMap = HashMap::new(); old_pairs.iter().for_each(|(pair, pair_count)| { @@ -68,3 +77,28 @@ fn process(input: &str) -> Input { (mappings, singles, pairs) } + +#[cfg(test)] +mod tests { + use super::*; + extern crate test; + use test::Bencher; + + #[bench] + fn bench_part_1(b: &mut Bencher) { + let raw_input = util::read_input("inputs/day14.txt").unwrap(); + b.iter(|| { + let input = process(&raw_input); + part_1(input); + }); + } + + #[bench] + fn bench_part_2(b: &mut Bencher) { + let raw_input = util::read_input("inputs/day14.txt").unwrap(); + b.iter(|| { + let input = process(&raw_input); + part_2(input); + }); + } +}