Group all days under one common workspace
Should make it easier to share code across days in the future.
This commit is contained in:
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