Day 9, faster

This commit is contained in:
Daniel S. 2020-12-09 19:15:11 +01:00
parent fc1f74ced5
commit 2249b4b9ef

View file

@ -35,7 +35,7 @@ pub fn solve_part1(input: &Vec<usize>) -> usize {
panic!("No match found!"); panic!("No match found!");
} }
#[aoc(day9, part2)] #[aoc(day9, part2,bruteforce)]
pub fn solve_part2(input: &Vec<usize>) -> usize { pub fn solve_part2(input: &Vec<usize>) -> usize {
let inv_num = solve_part1(input); let inv_num = solve_part1(input);
for start in 0..input.len() { for start in 0..input.len() {
@ -48,3 +48,14 @@ pub fn solve_part2(input: &Vec<usize>) -> usize {
} }
panic!("No match found!"); panic!("No match found!");
} }
#[aoc(day9, part2,faster)]
pub fn solve_part2_fast(input: &Vec<usize>) -> usize {
let target_num = solve_part1(input);
for i in 2..=input.len() {
if let Some(w) = input.windows(i).find(|w| target_num==w.iter().sum()) {
return w.iter().max().unwrap()+w.iter().min().unwrap();
}
}
panic!("No match found!");
}