Skip to content

Commit

Permalink
finish day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
mflinn-broad committed Dec 16, 2021
1 parent 168a58a commit 246e7a7
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/days/day14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, usize> = HashMap::new();

old_pairs.iter().for_each(|(pair, pair_count)| {
Expand Down Expand Up @@ -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);
});
}
}

0 comments on commit 246e7a7

Please sign in to comment.