Added year directories for advent of code

This commit is contained in:
2022-12-02 13:37:41 +01:00
parent 27d00340b9
commit 6c9525248e
31 changed files with 275 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
use std::io::{self, BufRead};
use std::path::Path;
use std::fs::File;
fn main(){
println!("Advent of Code #1!");
println!();
let path = Path::new("./1.txt");
let display = path.display();
let file = match File::open(&path) {
Err(why) => panic!("Couldn't open {}: {}", display, why),
Ok(file) => file,
};
let lines = io::BufReader::new(file).lines();
let mut n = 0;
let mut prev = 0;
let mut line_vec = Vec::new();
for line in lines {
if let Ok(l) = line{
let i: u32 = l.parse().unwrap();
if i > prev {
n += 1;
}
prev = i;
line_vec.push(i);
}
}
println!("Number of increases: {}", n - 1);
println!();
prev = 0;
n = 0;
for i in 0..(line_vec.len() - 2) {
let sum = line_vec[i] + line_vec[i+1] + line_vec[i+2];
if sum > prev {
n += 1;
}
prev = sum;
}
println!("Number of increases with shifting window: {}", n - 1);
}