Day 1 part 2, don't iterate over the whole list for the 3rd pass.

This commit is contained in:
Bill Thiede 2020-12-02 09:12:07 -08:00
parent bd6ca419e4
commit 72996ba5dc

View File

@ -69,8 +69,8 @@ fn find_pair_2020(nums: &Vec<u32>) -> Option<(u32, u32)> {
/// Finds triple of numbers in `nums` that sum to 2020. If no triple is found, `None` is returned.
fn find_triple_2020(nums: &Vec<u32>) -> Option<(u32, u32, u32)> {
for (idx1, first) in nums.iter().enumerate() {
for second in nums.iter().skip(idx1 + 1) {
for third in nums.iter() {
for (idx2, second) in nums.iter().enumerate().skip(idx1 + 1) {
for third in nums.iter().skip(idx2 + 1) {
if first + second + third == 2020 {
return Some((*first, *second, *third));
}