From e79048b2b7a78543d423a7039a9919e44d072053 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 4 Dec 2021 17:10:50 +0100 Subject: [PATCH 1/1] First day --- .gitignore | 10 ++++++++++ Cargo.toml | 14 +++++++++++++ README.md | 27 +++++++++++++++++++++++++ src/common.rs | 13 ++++++++++++ src/day01.rs | 35 ++++++++++++++++++++++++++++++++ src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/common.rs create mode 100644 src/day01.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5e26f12 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "advent_of_code_2021" +version = "0.1.0" +authors = ["Greg Burri "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.10" +threadpool = "1.8" +regex = "1" +num = "0.4" +num_enum = "0.5" \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a6796a --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# AdventOfCode2021 + +https://adventofcode.com/2021 + + +# Running tests + +Example for day 1 tests: + +~~~ +cargo test day01 -- --nocapture +~~~ + +All tests: + +~~~ +cargo test -- --nocapture +~~~ + + +# Running a day code + +~~~ +cargo run -- n +~~~ + +Where 'n' is a number from 1 to 25 diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..737ca8c --- /dev/null +++ b/src/common.rs @@ -0,0 +1,13 @@ +use std::fs; +use std::path::Path; +use std::str::FromStr; + +pub fn read_list_of_numbers(file: P, sep: &str) -> Vec +where + P: AsRef, + T: FromStr, + T::Err: std::fmt::Debug + +{ + fs::read_to_string(file).unwrap().split(sep).map(|line| line.trim().parse::().unwrap()).collect() +} \ No newline at end of file diff --git a/src/day01.rs b/src/day01.rs new file mode 100644 index 0000000..d4f1889 --- /dev/null +++ b/src/day01.rs @@ -0,0 +1,35 @@ +pub fn count_number_of_decreased_values(report: &[i32], window_size: usize) -> i32 { + let mut n = 0; + + let sum = |i: usize| -> i32 { + let mut s = 0; + for j in i..i+window_size { + s += report[j]; + } + s + }; + + for i in 0..report.len() - window_size { + if sum(i+1) > sum(i) { + n += 1; + } + } + n +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1() { + let sea_floor_report = [ 199, 200, 208, 210, 200, 207, 240, 269, 260, 263 ]; + assert_eq!(count_number_of_decreased_values(&sea_floor_report, 1), 7); + } + + #[test] + fn part2() { + let sea_floor_report = [ 199, 200, 208, 210, 200, 207, 240, 269, 260, 263 ]; + assert_eq!(count_number_of_decreased_values(&sea_floor_report, 3), 5); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d3592a1 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +use std::env; +use std::time::Instant; + +mod common; +mod day01; + +fn day01() -> String { + let report = common::read_list_of_numbers("data/day01.input", "\n"); + format!("part1: {}, part2: {}", day01::count_number_of_decreased_values(&report, 1), day01::count_number_of_decreased_values(&report, 3)) +} + +fn format_micros(t: u128) -> String { + if t < 10_000 { + format!("{} μs", t) + } else if t < 10_000_000u128 { + format!("{} ms", t / 1_000u128) + } else { + format!("{} s", t / 1_000_000u128) + } +} + +fn do_day(days: &[fn() -> String], day: usize) { + let now = Instant::now(); + println!("Result of day {:02}: {} (time: {})", day, days[day - 1](), format_micros(now.elapsed().as_micros())); +} + +fn main() { + println!("https://adventofcode.com/2021"); + + let days: Vec String> = vec!( + day01, + ); + + let args: Vec = env::args().skip(1).collect(); + + // No argument -> execute all day problems. + if args.is_empty() { + let now = Instant::now(); + for i in 1 ..= days.len() { + do_day(&days, i) + } + println!("Time to execute all days: {}", format_micros(now.elapsed().as_micros())); + } else { + for arg in args { + match arg.parse::() { + Ok(day) if day >= 1 && day <= days.len() => + do_day(&days, day), + Ok(day) => + println!("Unknown day: {}", day), + Err(error) => + println!("Unable to parse day number: \"{}\", error: {}", arg, error) + } + } + } +} -- 2.45.2