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

47 lines
996 B
Rust
Raw Normal View History

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