1
Fork 0
advent-of-code/source/year_2020/day_01/mod.rs

40 lines
772 B
Rust
Raw Normal View History

2022-10-03 16:02:40 +00:00
use crate::prelude::*;
pub fn solution() -> Solution {
Solution::new(Day::new(1, 2020), part_1, part_2)
.with_expected(605364, 128397680)
}
const TARGET: i32 = 2020;
fn parse_lines(input: &str) -> Vec<i32> {
input
.lines()
.filter_map(|line| line.parse::<i32>().ok())
.collect()
}
fn part_1(input: &str) -> Result<String> {
Ok(
parse_lines(input)
.into_iter()
.tuple_combinations()
.find(|(a, b)| a + b == TARGET)
.map(|(a, b)| a * b)
.unwrap()
.to_string(),
)
}
fn part_2(input: &str) -> Result<String> {
Ok(
parse_lines(input)
.into_iter()
.tuple_combinations()
.find(|(a, b, c)| a + b + c == TARGET)
.map(|(a, b, c)| a * b * c)
.unwrap()
.to_string(),
)
}