Group all days under one common workspace
Should make it easier to share code across days in the future.
This commit is contained in:
16
crates/day01/Cargo.lock
generated
Normal file
16
crates/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
crates/day01/Cargo.toml
Normal file
9
crates/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
crates/day01/src/input/input.txt
Executable file
2000
crates/day01/src/input/input.txt
Executable file
File diff suppressed because it is too large
Load Diff
10
crates/day01/src/input/test.txt
Normal file
10
crates/day01/src/input/test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
||||
68
crates/day01/src/main.rs
Normal file
68
crates/day01/src/main.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
const INPUT: &str = include_str!("input/input.txt");
|
||||
|
||||
fn solve_part1(input: &str) -> Result<i32> {
|
||||
let lines = input.trim().lines();
|
||||
|
||||
let mut increases = 0;
|
||||
let mut prev_reading: Option<i32> = None;
|
||||
for line in 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: &str) -> Result<i32> {
|
||||
let lines: Vec<&str> = input.trim().lines().collect();
|
||||
|
||||
let mut increases = 0;
|
||||
let mut prev_sum: Option<i32> = None;
|
||||
for group in lines.windows(3) {
|
||||
let sum = group.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 = include_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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user