From 743260c752a1c127ae4b779255bd255cc575dfd7 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Wed, 2 Dec 2020 08:24:05 +0530 Subject: [PATCH] Added Day 1 Part 2 and the smaller test input as well --- small.input | 6 ++++++ src/main.rs | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 small.input diff --git a/small.input b/small.input new file mode 100644 index 0000000..e3fb011 --- /dev/null +++ b/small.input @@ -0,0 +1,6 @@ +1721 +979 +366 +299 +675 +1456 diff --git a/src/main.rs b/src/main.rs index 2e7dccc..292c4f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,12 +9,20 @@ fn main() { .split('\n') .filter(|x| !x.is_empty()) .map(|x| x.parse::().unwrap()) + .filter(|&x| x <= 2020) .collect(); - for &num in numbers.iter() { - if numbers.contains(&(2020 - num)) { - println!("{}", num * (2020 - num)); - break; + 'outer: for (i, &num1) in numbers.iter().enumerate() { + let diff = 2020 - num1; + + for (j, &num2) in numbers.iter().enumerate() { + if i == j { + continue; + } + if numbers.contains(&(diff - num2)) { + println!("{}", num1 * num2 * (diff - num2)); + break 'outer; + } } } }