From c09ce94f6a46d6b4c0351c882caa3a67946299fa Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Tue, 1 Dec 2020 17:54:44 +0100 Subject: [PATCH] 2020: day01: linear part 1 --- aoc2020/src/day01.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/aoc2020/src/day01.rs b/aoc2020/src/day01.rs index 9879fa7..7f32745 100644 --- a/aoc2020/src/day01.rs +++ b/aoc2020/src/day01.rs @@ -37,14 +37,15 @@ fn part2(input: &str) -> Result { } fn find_2020_2_sum(entries: &[i64]) -> Result<(i64, i64)> { + let set: HashSet = entries.iter().copied().collect(); + for i in 0..entries.len() { - for j in 0..entries.len() { - if i == j { - continue; - } - if entries[i] + entries[j] == 2020 { - return Ok((entries[i], entries[j])); - } + let a = entries[i]; + + let b = 2020 - a; + + if set.contains(&b) { + return Ok((a, b)); } }