This commit is contained in:
Daniel S. 2020-12-09 16:57:29 +01:00
parent 6d14236aa4
commit fc1f74ced5
3 changed files with 52 additions and 1 deletions

View file

@ -21,7 +21,7 @@ impl Inst {
Inst::Nop { unused } => Some(Inst::Jmp { offset: *unused }), Inst::Nop { unused } => Some(Inst::Jmp { offset: *unused }),
_ => None, _ => None,
} { } {
*self=new_inst; *self = new_inst;
}; };
} }
} }

50
src/day9.rs Normal file
View file

@ -0,0 +1,50 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day9)]
pub fn input_generator(input: &str) -> Vec<usize> {
input
.lines()
.map(|l| l.trim().parse())
.collect::<Result<Vec<usize>, _>>()
.unwrap()
}
#[aoc(day9, part1)]
pub fn solve_part1(input: &Vec<usize>) -> usize {
for (n, win) in input.as_slice().windows(25).enumerate() {
let n = n + 25;
if n >= input.len() {
continue;
}
let c = input[n];
let mut valid = false;
for (i1, n1) in win.iter().enumerate() {
for (i2, n2) in win.iter().enumerate() {
if i1 == i2 {
continue;
}
if n1 + n2 == c {
valid = true;
}
}
}
if !valid {
return c;
}
}
panic!("No match found!");
}
#[aoc(day9, part2)]
pub fn solve_part2(input: &Vec<usize>) -> usize {
let inv_num = solve_part1(input);
for start in 0..input.len() {
for length in 2..(input.len() - start) {
let s: Vec<usize> = input.iter().skip(start).take(length).cloned().collect();
if s.iter().sum::<usize>() == inv_num {
return s.iter().max().unwrap() + s.iter().min().unwrap();
}
}
}
panic!("No match found!");
}

View file

@ -7,4 +7,5 @@ pub mod day5;
pub mod day6; pub mod day6;
pub mod day7; pub mod day7;
pub mod day8; pub mod day8;
pub mod day9;
aoc_lib! { year = 2020 } aoc_lib! { year = 2020 }