Started on advent of code in Rust; o.d.z. on becoming a Rustacean
This commit is contained in:
48
advent_of_code/1.rs
Normal file
48
advent_of_code/1.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user