add initial data
This commit is contained in:
commit
c4e841b4bc
43 files changed
+5284
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc01"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aoc01"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
+2000
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,10 @@
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
||||
@@ -0,0 +1,43 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let data = read_data("data.txt");
|
||||
let increase_count = process(&data);
|
||||
println!("Increasing values: {}", increase_count);
|
||||
let increase_count = process_windowed(&data);
|
||||
println!("Increasing values windowed: {}", increase_count);
|
||||
}
|
||||
|
||||
fn read_data(filename: &str) -> String {
|
||||
fs::read_to_string(filename).expect("Could not read file")
|
||||
}
|
||||
|
||||
fn process(data: &str) -> i32 {
|
||||
let mut last_value = i32::MAX;
|
||||
let mut increase_count = 0;
|
||||
for line in data.lines() {
|
||||
let value = line.parse::<i32>().unwrap();
|
||||
if value > last_value {
|
||||
increase_count += 1
|
||||
}
|
||||
last_value = value;
|
||||
}
|
||||
increase_count
|
||||
}
|
||||
|
||||
fn process_windowed(data: &str) -> i32 {
|
||||
let mut last_value = i32::MAX;
|
||||
let mut increase_count = 0;
|
||||
for i in 0..1998 {
|
||||
let mut value = 0;
|
||||
for j in 0..3 {
|
||||
value += data.lines().nth(i + j).unwrap().parse::<i32>().unwrap();
|
||||
}
|
||||
if value > last_value {
|
||||
increase_count += 1
|
||||
}
|
||||
last_value = value;
|
||||
}
|
||||
increase_count
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user