mirror of
https://github.com/Noratrieb/advent-of-code.git
synced 2026-01-16 18:45:02 +01:00
day 9 part 1
This commit is contained in:
parent
8e51f43402
commit
3ace5075d5
10 changed files with 324 additions and 2 deletions
73
2023/day09/src/lib.rs
Normal file
73
2023/day09/src/lib.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use helper::{Day, Variants};
|
||||
|
||||
pub fn main() {
|
||||
helper::main::<Day09>(include_str!("../input.txt"));
|
||||
}
|
||||
|
||||
struct Day09;
|
||||
|
||||
helper::define_variants! {
|
||||
day => crate::Day09;
|
||||
part1 {
|
||||
basic => crate::part1;
|
||||
}
|
||||
part2 {
|
||||
basic => crate::part2;
|
||||
}
|
||||
}
|
||||
|
||||
impl Day for Day09 {
|
||||
fn part1() -> Variants {
|
||||
part1_variants!(construct_variants)
|
||||
}
|
||||
|
||||
fn part2() -> Variants {
|
||||
part2_variants!(construct_variants)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> impl Iterator<Item = Vec<i64>> + '_ {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| line.split_ascii_whitespace().map(|s| s.parse().unwrap()).collect())
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> u64 {
|
||||
parse(input)
|
||||
.map(|mut values| {
|
||||
let mut last_values = vec![*values.last().unwrap()];
|
||||
|
||||
let mut derive = values.clone();
|
||||
|
||||
while !derive.iter().all(|&n| n == 0) {
|
||||
values.clear();
|
||||
values.extend(derive.windows(2).map(|s| s[1] - s[0]));
|
||||
|
||||
last_values.push(*values.last().unwrap());
|
||||
|
||||
let tmp = derive;
|
||||
derive = values;
|
||||
values = tmp;
|
||||
}
|
||||
|
||||
last_values.into_iter().sum::<i64>()
|
||||
})
|
||||
.sum::<i64>() as u64
|
||||
}
|
||||
|
||||
fn part2(_input: &str) -> u64 {
|
||||
0
|
||||
}
|
||||
|
||||
helper::tests! {
|
||||
day09 Day09;
|
||||
part1 {
|
||||
small => 0;
|
||||
default => 0;
|
||||
}
|
||||
part2 {
|
||||
small => 0;
|
||||
default => 0;
|
||||
}
|
||||
}
|
||||
helper::benchmarks! {}
|
||||
3
2023/day09/src/main.rs
Normal file
3
2023/day09/src/main.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
day09::main();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue