advent-of-code/2020/01/src/main.rs

23 lines
689 B
Rust
Raw Normal View History

2020-12-01 23:54:58 +01:00
#![feature(bool_to_option)]
2020-12-01 08:02:29 +01:00
use itertools::Itertools;
2020-12-01 07:16:56 +01:00
use std::io::BufRead;
fn main() {
2020-12-02 00:30:57 +01:00
let input: Vec<usize> = std::io::stdin().lock().lines().filter_map(|l| l.unwrap().parse().ok()).collect();
2020-12-01 08:02:29 +01:00
let p1 = input
.iter()
.tuple_combinations()
2020-12-01 23:54:58 +01:00
.find_map(|(&a, &b)| (a + b == 2020).then_some(a * b))
2020-12-01 07:16:56 +01:00
.unwrap();
println!("Part 1: {}", p1);
2020-12-01 23:54:58 +01:00
2020-12-01 07:16:56 +01:00
let mut p2_table = vec![None; 2020];
2020-12-01 08:02:29 +01:00
for (&a, &b) in input.iter().tuple_combinations() {
2020-12-01 07:16:56 +01:00
if a + b < 2020 {
p2_table[a + b] = Some((a, b))
}
}
2020-12-01 23:54:58 +01:00
let (a, b) = input.iter().find_map(|x| p2_table[2020 - x]).unwrap();
2020-12-01 07:16:56 +01:00
println!("Part 2: {}", a * b * (2020 - a - b));
}