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

47 lines
996 B
Rust

//! Day 01 of 2020.
use crate::prelude::*;
/// Get the solution for day 01 of 2020.
pub fn solution() -> Solution {
Solution::new(Day::new(1, 2020), part_1, part_2)
.with_expected(605364, 128397680)
}
/// The target sum that two entries need to add up to.
const TARGET: i32 = 2020;
/// Parse the input lines into integers.
fn parse_lines(input: &str) -> Vec<i32> {
input
.lines()
.filter_map(|line| line.parse::<i32>().ok())
.collect()
}
/// The logic to solve part one.
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(),
)
}
/// The logic to solve part two.
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(),
)
}