Add alternate implementations for day 1, and create README of results.

This commit is contained in:
Bill Thiede 2020-12-02 19:57:26 -08:00
parent 0012d414b4
commit bc8de05d69
2 changed files with 77 additions and 4 deletions

33
2020/README.md Normal file
View File

@ -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
```

View File

@ -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<u32>`. Any
@ -43,10 +45,8 @@ fn parse(input: &str) -> Vec<u32> {
}
/// 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<u32> {
let mut nums: Vec<u32> = 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<u32> {
input
.split('\n')
.map(|line| line.parse().unwrap())
.collect()
}
#[aoc(day1, part1, set)]
fn find_pair_2020_set(nums: &HashSet<u32>) -> 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)]