From bc8de05d6951ffbdf22b4e050b51daf461a4d489 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Wed, 2 Dec 2020 19:57:26 -0800 Subject: [PATCH] Add alternate implementations for day 1, and create README of results. --- 2020/README.md | 33 +++++++++++++++++++++++++++++++++ 2020/src/day1.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 2020/README.md diff --git a/2020/README.md b/2020/README.md new file mode 100644 index 0000000..8c233ed --- /dev/null +++ b/2020/README.md @@ -0,0 +1,33 @@ +# Results + +## Day 1 +``` +AOC 2020 +Day 1 - Part 1 - binary : 1006875 + generator: 12.539µs, + runner: 373ns + +Day 1 - Part 1 - linear : 1006875 + generator: 4.945µs, + runner: 7.727µs + +Day 1 - Part 1 - set : 1006875 + generator: 16.721µs, + runner: 1.288µs + +Day 1 - Part 2 : 165026160 + generator: 4.272µs, + runner: 1.225545ms +``` + +## Day 2 +``` +AOC 2020 +Day 2 - Part 1 : 640 + generator: 1.821498ms, + runner: 115.525µs + +Day 2 - Part 2 : 472 + generator: 1.52241ms, + runner: 10.459µs +``` diff --git a/2020/src/day1.rs b/2020/src/day1.rs index 78b1f57..59874ec 100644 --- a/2020/src/day1.rs +++ b/2020/src/day1.rs @@ -30,6 +30,8 @@ //! //! In your expense report, what is the product of the three entries that sum to 2020? +use std::collections::HashSet; + use aoc_runner_derive::{aoc, aoc_generator}; /// Reads text file containing one integer per line, and parses them into `Vec`. Any @@ -43,10 +45,8 @@ fn parse(input: &str) -> Vec { } /// Finds pairs of numbers in `nums` that sum to 2020. If no pairs are found, the function panics. -/// TODO(wathiede): make a version that sorts or uses a hash for finding the match to compare -/// benchmarks. -#[aoc(day1, part1)] -fn find_pair_2020(nums: &[u32]) -> u32 { +#[aoc(day1, part1, linear)] +fn find_pair_2020_linear(nums: &[u32]) -> u32 { for (idx, first) in nums.iter().enumerate() { for second in nums.iter().skip(idx + 1) { if first + second == 2020 { @@ -57,6 +57,46 @@ fn find_pair_2020(nums: &[u32]) -> u32 { panic!("Couldn't find pair"); } +#[aoc_generator(day1, part1, binary)] +fn parse_sorted(input: &str) -> Vec { + let mut nums: Vec = input + .split('\n') + .map(|line| line.parse().unwrap()) + .collect(); + nums.sort(); + nums +} + +#[aoc(day1, part1, binary)] +fn find_pair_2020_binary(nums: &[u32]) -> u32 { + for first in nums.iter() { + let diff = 2020 - first; + if nums.binary_search(&diff).is_ok() { + return first * diff; + } + } + panic!("Couldn't find pair"); +} + +#[aoc_generator(day1, part1, set)] +fn parse_set(input: &str) -> HashSet { + input + .split('\n') + .map(|line| line.parse().unwrap()) + .collect() +} + +#[aoc(day1, part1, set)] +fn find_pair_2020_set(nums: &HashSet) -> u32 { + for first in nums.iter() { + let diff = 2020 - first; + if nums.contains(&diff) { + return first * diff; + } + } + panic!("Couldn't find pair"); +} + /// Finds triple of numbers in `nums` that sum to 2020. If no triple is found, the function /// panics. #[aoc(day1, part2)]