From e6b3f045f83a3e6e89b1c57d5a2a5ea383f3a923 Mon Sep 17 00:00:00 2001 From: David Lenfesty Date: Tue, 30 Nov 2021 23:05:57 -0700 Subject: [PATCH] Solved Day 1 challenges. Took longer than I hoped it would. Not really familiar with some basic Rust stuff (like file handling, etc.). Also rust-analyzer was really not being nice. It crashed on StructOpt and keeps complaining about unsafe being necessary in any and all format! statements. --- .gitignore | 3 +++ Cargo.toml | 9 +++++++++ src/day1.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/day1.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba492fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.lock +inputs/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c24f3ec --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc2021" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = "2.34" diff --git a/src/day1.rs b/src/day1.rs new file mode 100644 index 0000000..df60be6 --- /dev/null +++ b/src/day1.rs @@ -0,0 +1,47 @@ +use std::collections::VecDeque; + +/// Increments count if each sample is larger than the previous. +pub fn day1_p1(input: String) { + let mut prev = None; + let mut count: u32 = 0; + for line in input.lines() { + let cur = str::parse::(line).unwrap(); + + if let Some(prev) = prev { + if cur > prev { + count += 1; + } + } + + prev = Some(cur); + } + + println!("{}", count); +} + +/// 3-sample windowing average (no need to average, just use sum here) +/// +/// We need to start comparing when we have all of +pub fn day1_p2(input: String) { + let mut storage = VecDeque::with_capacity(5); + let mut count = 0; + + for line in input.lines() { + let cur = str::parse::(line).unwrap(); + + storage.push_back(cur); + + if storage.len() == 4 { + let prev_window: i64 = storage.iter().take(3).sum(); + let cur_window: i64 = storage.iter().skip(1).take(3).sum(); + + if cur_window > prev_window { + count += 1; + } + + storage.pop_front(); + } + } + + println!("{}", count); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..535133e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,52 @@ +use clap::{App, Arg}; + +mod day1; + +fn main() { + let matches = App::new("AOC 2021 Code") + .version("0.1") + .author("David Lenfesty (day).expect("Invalid day provided (1-25 expected)"); + + let part = matches.value_of("part").unwrap_or("1"); + let part = str::parse::(part).expect("Invalid part provided (1 or 2 expected)"); + + let input = read_input(day, part).unwrap(); + + match day { + 1 => match part { + 1 => day1::day1_p1(input), + 2 => day1::day1_p2(input), + _ => (), + }, + _ => println!("Day {} not completed yet!", day), + } +} + +// TODO do the inputs ever change inside of a day? +/// Read inputs into a string +fn read_input(day: u8, part: u8) -> std::io::Result { + std::fs::read_to_string(format!( + "inputs/day{}p{}", + day, part + )) +}