Improve 2020/01

This commit is contained in:
kageru 2020-12-01 08:02:29 +01:00
parent 1138c0c8e2
commit d0d2e58f87
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -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))
}