This commit is contained in:
spectre 2023-12-09 12:28:24 +01:00
parent 61798c7ac4
commit 07692eb048
3 changed files with 242 additions and 4 deletions

View file

@ -1,13 +1,51 @@
use std::iter::zip;
use shared::{Solution, Answer};
pub struct Day09;
impl Solution for Day09 {
fn part_1(&self, input: &str) -> Answer {
Answer::Unimplemented
let lines = input.split("\n")
.map(|l| l.split_whitespace().map(|x| x.parse::<i64>().unwrap()).collect()
).collect::<Vec<Vec<i64>>>();
Answer::from(
lines.iter().map(solve1).sum::<i64>()
)
}
fn part_2(&self, input: &str) -> Answer {
Answer::Unimplemented
let lines = input.split("\n")
.map(|l| l.split_whitespace().map(|x| x.parse::<i64>().unwrap()).collect()
).collect::<Vec<Vec<i64>>>();
Answer::from(
lines.iter().map(solve2).sum::<i64>()
)
}
}
fn solve1(seq: &Vec<i64>) -> i64 {
if seq.iter().all(|x| *x==0) {
return 0
}
let diffs = zip(seq, seq.iter().skip(1))
.map(|(a, b)| b - a )
.collect();
return seq.last().unwrap() + solve1(&diffs)
}
fn solve2(seq: &Vec<i64>) -> i64 {
if seq.iter().all(|x| *x==0) {
return 0
}
let diffs = zip(seq, seq.iter().skip(1))
.map(|(a, b)| b - a )
.collect();
return seq.first().unwrap() - solve2(&diffs)
}