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

78 lines
1.7 KiB
Rust
Raw Normal View History

2022-10-03 16:02:40 +00:00
use crate::prelude::*;
pub fn solution() -> Solution {
Solution::new(Day::new(9, 2020), part_1, part_2)
.with_expected(22477624, 2980044)
}
const PREAMBLE_LENGTH: usize = 25;
fn parse_numbers(input: &str) -> Vec<usize> {
input
.lines()
.map(|number| number.parse().unwrap())
.collect()
}
fn part_1(input: &str) -> Result<String> {
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())
}
fn part_2(input: &str) -> Result<String> {
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::<usize>();
if preamble_total.to_string() == result_one {
let mut preamble = preamble.collect::<Vec<&usize>>();
preamble.sort_unstable();
result_two = *preamble.first().unwrap() + *preamble.last().unwrap();
break 'outer;
}
}
}
Ok(result_two.to_string())
}