day 09
This commit is contained in:
parent
61798c7ac4
commit
07692eb048
3 changed files with 242 additions and 4 deletions
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue