From d0d2e58f871a29e8425d35d62af48c542da361b8 Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 1 Dec 2020 08:02:29 +0100 Subject: [PATCH] Improve 2020/01 --- 2020/01/src/main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/2020/01/src/main.rs b/2020/01/src/main.rs index bdc3b7f..129a8f3 100644 --- a/2020/01/src/main.rs +++ b/2020/01/src/main.rs @@ -1,4 +1,4 @@ -use itertools::iproduct; +use itertools::Itertools; use std::io::BufRead; fn main() { @@ -7,7 +7,9 @@ fn main() { .lines() .filter_map(|l| l.unwrap().parse().ok()) .collect(); - let p1 = iproduct!(input.iter(), input.iter()) + let p1 = input + .iter() + .tuple_combinations() .filter(|(&a, &b)| a + b == 2020) .map(|(a, b)| a * b) .next() @@ -15,7 +17,8 @@ fn main() { println!("Part 1: {}", p1); /* smol-brain, n³ solution: - let p2 = iproduct!(input.iter(), input.iter(), input.iter()) + let p2 = input + .tuple_combinations() .filter(|(&a, &b, &c)| a + b + c == 2020) .map(|(a, b, c)| a * b * c) .next() @@ -24,7 +27,7 @@ fn main() { */ // Ascended n² solution (thanks to Lypheo) let mut p2_table = vec![None; 2020]; - for (&a, &b) in iproduct!(input.iter(), input.iter()) { + for (&a, &b) in input.iter().tuple_combinations() { if a + b < 2020 { p2_table[a + b] = Some((a, b)) }