Changed advent_of_code rust stuff to be good cargo packages instead
This commit is contained in:
2000
advent_of_code/d1/1.txt
Normal file
2000
advent_of_code/d1/1.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
advent_of_code/d1/Cargo.lock
generated
Normal file
7
advent_of_code/d1/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "d1"
|
||||
version = "0.1.0"
|
||||
8
advent_of_code/d1/Cargo.toml
Normal file
8
advent_of_code/d1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "d1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
48
advent_of_code/d1/src/main.rs
Normal file
48
advent_of_code/d1/src/main.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