advent-of-code/2020/src/bin/day09.rs

86 lines
1.6 KiB
Rust
Raw Normal View History

2020-12-09 08:57:54 +01:00
#![feature(test)]
extern crate test;
2020-12-10 14:44:42 +01:00
use aoc2020::common::*;
2020-12-09 08:57:54 +01:00
2020-12-09 10:45:38 +01:00
use itertools::{Itertools, MinMaxResult};
2020-12-09 08:57:54 +01:00
fn read_input() -> String {
2020-12-10 14:44:42 +01:00
read_file(9)
2020-12-09 10:45:38 +01:00
}
fn parse_input(raw: &str) -> Vec<usize> {
raw.lines().map(|l| l.parse().unwrap()).collect()
}
fn part1(input: &[usize], window_size: usize) -> usize {
*input
.windows(window_size + 1)
.find_map(|xs| {
xs.last()
.filter(|last| !xs[..window_size].iter().tuple_combinations().any(|(a, b)| a + b == **last))
})
.unwrap()
2020-12-09 08:57:54 +01:00
}
2020-12-09 10:45:38 +01:00
fn part2(input: &[usize], bad_num: usize) -> usize {
(0..input.len()).find_map(|i| sum_up_to(&input[i..], bad_num)).unwrap()
}
fn sum_up_to(input: &[usize], n: usize) -> Option<usize> {
let mut acc = input[0];
for x in 1..input.len() {
acc += input[x];
if acc == n {
return match input[..x].iter().minmax() {
MinMaxResult::MinMax(min, max) => Some(min + max),
_ => unreachable!(),
};
}
if acc > n {
return None;
}
}
None
2020-12-09 08:57:54 +01:00
}
fn main() {
let input = parse_input(&read_input());
2020-12-09 10:45:38 +01:00
let p1 = part1(&input, 25);
println!("Part 1: {}", p1);
println!("Part 2: {}", part2(&input, p1));
2020-12-09 08:57:54 +01:00
}
#[cfg(test)]
mod tests {
use super::*;
use test::black_box;
2020-12-10 16:41:54 +01:00
use aoc2020::*;
use paste::paste;
2020-12-09 08:57:54 +01:00
2020-12-09 10:45:38 +01:00
const TEST_INPUT: &str = "35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576";
2020-12-10 16:41:54 +01:00
test!(part1(5) == 127);
test!(part2(127) == 62);
bench!(part1(25) == 393911906);
bench!(part2(393911906) == 59341885);
2020-12-09 08:57:54 +01:00
}