Advent 2023 Day 7, 8 & 9
This commit is contained in:
parent
57429c49e9
commit
582af3d9f3
9 changed files with 2364 additions and 0 deletions
26
advent_of_code_2023/day9/main1.rs
Normal file
26
advent_of_code_2023/day9/main1.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
fn extrapolate(sequence: &Vec<i64>) -> i64 {
|
||||
if sequence.iter().all(|num| { *num == 0 }) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut deltas: Vec<i64> = Vec::new();
|
||||
for idx in 1..sequence.len() {
|
||||
deltas.push(sequence[idx] - sequence[idx - 1]);
|
||||
}
|
||||
|
||||
sequence.last().unwrap() + extrapolate(&deltas)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sequences = read_to_string("input.txt").unwrap().lines().map(|line| {
|
||||
line.split(' ').map(|num| {
|
||||
num.parse::<i64>().unwrap()
|
||||
}).collect::<Vec<_>>()
|
||||
}).collect::<Vec<_>>();
|
||||
|
||||
println!("Sum of extrapolations: {}", sequences.iter().map(|sequence| {
|
||||
extrapolate(sequence)
|
||||
}).sum::<i64>());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue