//! Day 09 of 2020. use crate::prelude::*; /// Get the solution for day 09 of 2020. pub fn solution() -> Solution { Solution::new(Day::new(9, 2020), part_1, part_2) .with_expected(22477624, 2980044) } /// The amount of numbers to consider as the preamble. const PREAMBLE_LENGTH: usize = 25; /// Parse the puzzle input into a list of numbers. fn parse_numbers(input: &str) -> Vec { input .lines() .map(|number| number.parse().unwrap()) .collect() } /// The logic to solve part one. fn part_1(input: &str) -> Result { let numbers = parse_numbers(input); let mut result_one = 0; for (index, number) in numbers.iter().enumerate() { if index < PREAMBLE_LENGTH { continue; } let preamble = { let mut set = HashSet::new(); let _numbers = numbers .iter() .skip(index - PREAMBLE_LENGTH) .take(PREAMBLE_LENGTH); for a in _numbers.clone() { for b in _numbers.clone() { set.insert(a + b); } } set }; if !preamble.contains(number) { result_one = *number; break; } } Ok(result_one.to_string()) } /// The logic to solve part two. fn part_2(input: &str) -> Result { let numbers = parse_numbers(input); let result_one = part_1(input)?; let mut result_two = 0; 'outer: for preamble_length in 2..numbers.len() { for (index, _) in numbers.iter().enumerate() { if index < preamble_length { continue; } let preamble = numbers .iter() .skip(index - preamble_length) .take(preamble_length); let preamble_total = preamble.clone().sum::(); if preamble_total.to_string() == result_one { let mut preamble = preamble.collect::>(); preamble.sort_unstable(); result_two = *preamble.first().unwrap() + *preamble.last().unwrap(); break 'outer; } } } Ok(result_two.to_string()) }