diff --git a/2020/src/day1.rs b/2020/src/day1.rs index 78b1f57..2da05aa 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 @@ -45,8 +47,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 +59,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)]