Day 9, faster
This commit is contained in:
parent
fc1f74ced5
commit
2249b4b9ef
1 changed files with 12 additions and 1 deletions
13
src/day9.rs
13
src/day9.rs
|
@ -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!");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue