diff --git a/aoc2020/src/day01.rs b/aoc2020/src/day01.rs index 18322ef..9879fa7 100644 --- a/aoc2020/src/day01.rs +++ b/aoc2020/src/day01.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use std::fmt::Write; use aoc::{err, Result}; @@ -13,29 +14,29 @@ pub fn run() -> Result { Ok(res) } -fn part1(input: &str) -> Result { +fn part1(input: &str) -> Result { let entries = input .lines() - .map(|line| line.parse::().map_err(|e| err!("{}", e))) - .collect::>>()?; + .map(|line| line.parse::().map_err(|e| err!("{}", e))) + .collect::>>()?; let (a, b) = find_2020_2_sum(&entries)?; Ok(a * b) } -fn part2(input: &str) -> Result { +fn part2(input: &str) -> Result { let entries = input .lines() - .map(|line| line.parse::().map_err(|e| err!("{}", e))) - .collect::>>()?; + .map(|line| line.parse::().map_err(|e| err!("{}", e))) + .collect::>>()?; let (a, b, c) = find_2020_3_sum(&entries)?; Ok(a * b * c) } -fn find_2020_2_sum(entries: &[u64]) -> Result<(u64, u64)> { +fn find_2020_2_sum(entries: &[i64]) -> Result<(i64, i64)> { for i in 0..entries.len() { for j in 0..entries.len() { if i == j { @@ -50,21 +51,22 @@ fn find_2020_2_sum(entries: &[u64]) -> Result<(u64, u64)> { Err(err!("couldn't find 2 elements that sum to 2020")) } -fn find_2020_3_sum(entries: &[u64]) -> Result<(u64, u64, u64)> { +fn find_2020_3_sum(entries: &[i64]) -> Result<(i64, i64, i64)> { + let set: HashSet = entries.iter().copied().collect(); + for i in 0..entries.len() { for j in 0..entries.len() { - for k in 0..entries.len() { - if i == j || j == k || k == i { - continue; - } + if i == j { + continue; + } - let a = entries[i]; - let b = entries[j]; - let c = entries[k]; + let a = entries[i]; + let b = entries[j]; - if a + b + c == 2020 { - return Ok((a, b, c)); - } + let c = 2020 - a - b; + + if set.contains(&c) { + return Ok((a, b, c)); } } } @@ -76,7 +78,7 @@ fn find_2020_3_sum(entries: &[u64]) -> Result<(u64, u64, u64)> { mod tests { use super::*; - static PROVIDED: &[u64] = &[1721, 979, 366, 299, 675, 1456]; + static PROVIDED: &[i64] = &[1721, 979, 366, 299, 675, 1456]; #[test] fn part1_provided() {