Completed day 1
This commit is contained in:
parent
b619e7e776
commit
7c4b0ead30
16
day01/Cargo.lock
generated
Normal file
16
day01/Cargo.lock
generated
Normal file
@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"
|
||||
|
||||
[[package]]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
]
|
9
day01/Cargo.toml
Normal file
9
day01/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
2000
day01/input/input.txt
Executable file
2000
day01/input/input.txt
Executable file
File diff suppressed because it is too large
Load Diff
10
day01/input/test.txt
Normal file
10
day01/input/test.txt
Normal file
@ -0,0 +1,10 @@
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
76
day01/src/main.rs
Normal file
76
day01/src/main.rs
Normal file
@ -0,0 +1,76 @@
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
const INPUT: &str = "input/input.txt";
|
||||
|
||||
fn solve_part1(input_path: &str) -> Result<i32> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut increases = 0;
|
||||
let mut prev_reading: Option<i32> = None;
|
||||
for line in reader.lines() {
|
||||
let reading = line?.parse()?;
|
||||
if let Some(prev) = prev_reading {
|
||||
if reading > prev {
|
||||
increases += 1;
|
||||
}
|
||||
}
|
||||
prev_reading = Some(reading);
|
||||
}
|
||||
|
||||
Ok(increases)
|
||||
}
|
||||
|
||||
fn solve_part2(input_path: &str) -> Result<i32> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut increases = 0;
|
||||
let mut prev_sum: Option<i32> = None;
|
||||
let all_lines = reader
|
||||
.lines()
|
||||
.collect::<Result<Vec<String>, std::io::Error>>()?;
|
||||
for lines in all_lines.windows(3) {
|
||||
let sum = lines.iter().map(|s| s.parse::<i32>().unwrap()).sum();
|
||||
if let Some(prev) = prev_sum {
|
||||
if sum > prev {
|
||||
increases += 1;
|
||||
}
|
||||
}
|
||||
prev_sum = Some(sum);
|
||||
}
|
||||
|
||||
Ok(increases)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut now = Instant::now();
|
||||
println!("Part 1: {}", solve_part1(INPUT).unwrap());
|
||||
println!("(elapsed: {:?})", now.elapsed());
|
||||
now = Instant::now();
|
||||
println!("");
|
||||
println!("Part 2: {}", solve_part2(INPUT).unwrap());
|
||||
println!("(elapsed: {:?})", now.elapsed());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "input/test.txt";
|
||||
|
||||
#[test]
|
||||
fn solves_part1() {
|
||||
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 5);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user