Advent of Code 2023 - Day 09
I'ts that time of year again, the Advent of Code! I'm completing the challenges this year using Rust 🦀 Day 9 was smooth sailing!
I'll be posting them here, just to document how I'm solving each challenge, and I highly encourage you to check them out before reading my solutions!
Here's the Day 9 Challenge
BTW: Complete Part 1 to have access to Part 2 😉
Solution for Part 1
use std::fs::File;
use std::io::{Read, Result};
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_file_path = "path_to_file";
let mut curr_sum = 0;
let file = file_as_string(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
for line in file.split("\r\n") {
let mut histories : Vec<Vec<i64>> = vec![];
let history_vec = line.split(' ').map(|i| i64::from_str(i).unwrap()).collect::<Vec<i64>>();
histories.push(history_vec);
while !histories.last().unwrap().iter().all(|i| i == &0) {
let last_row = histories.last().unwrap();
let mut new_row: Vec<i64> = vec![];
for window in last_row.windows(2) {
new_row.push(window[1] - window[0]);
}
histories.push(new_row);
}
histories.reverse();
let mut results : Vec<i64> = vec![0];
for (idx, _) in histories.as_slice().iter().enumerate() {
if histories.len() == idx + 1 {
continue
}
let last_el = results.last().unwrap();
let last_el_next_row = histories.get(idx + 1).unwrap().last().unwrap();
results.push(last_el + last_el_next_row);
}
curr_sum += results.last().unwrap();
}
println!("Sum of all last extrapolated values: {}", curr_sum);
}
fn file_as_string<P: AsRef<Path>>(filename: P) -> Result<String> {
let mut file = File::open(filename)?;
let mut file_as_str = String::new();
File::read_to_string(&mut file, &mut file_as_str)?;
Ok(file_as_str)
}
Solution for Part 2
use std::fs::File;
use std::io::{Read, Result};
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_file_path = "path_to_input_file";
let mut curr_sum = 0;
let file = file_as_string(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
for line in file.split("\r\n") {
let mut histories : Vec<Vec<i64>> = vec![];
let history_vec = line.split(' ').map(|i| i64::from_str(i).unwrap()).collect::<Vec<i64>>();
histories.push(history_vec);
while !histories.last().unwrap().iter().all(|i| i == &0) {
let last_row = histories.last().unwrap();
let mut new_row: Vec<i64> = vec![];
for window in last_row.windows(2) {
new_row.push(window[1] - window[0]);
}
histories.push(new_row);
}
histories.reverse();
let mut results : Vec<i64> = vec![0];
for (idx, _) in histories.as_slice().iter().enumerate() {
if histories.len() == idx + 1 {
continue
}
let last_el = results.last().unwrap();
// Only lines that actually differ are these 2 :P
let last_el_next_row = histories.get(idx + 1).unwrap().first().unwrap();
results.push(last_el_next_row - last_el);
}
curr_sum += results.last().unwrap();
}
println!("Sum of all last extrapolated values: {}", curr_sum);
}
fn file_as_string<P: AsRef<Path>>(filename: P) -> Result<String> {
let mut file = File::open(filename)?;
let mut file_as_str = String::new();
File::read_to_string(&mut file, &mut file_as_str)?;
Ok(file_as_str)
}