summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock16
-rw-r--r--Cargo.toml7
-rw-r--r--rustfmt.toml11
-rw-r--r--src/day_1.rs47
-rw-r--r--src/main.rs52
6 files changed, 135 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3ceb93d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+/input
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..73a62f1
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,16 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aoc2021"
+version = "0.1.0"
+dependencies = [
+ "argparse",
+]
+
+[[package]]
+name = "argparse"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f8ebf5827e4ac4fd5946560e6a99776ea73b596d80898f357007317a7141e47"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..e09bbf2
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "aoc2021"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+argparse = "*"
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..b7e6339
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1,11 @@
+brace_style = "AlwaysNextLine"
+condense_wildcard_suffixes = true
+control_brace_style = "ClosingNextLine"
+fn_single_line = true
+format_strings = true
+imports_granularity = "Module"
+match_block_trailing_comma = true
+newline_style = "Unix"
+group_imports = "StdExternalCrate"
+struct_field_align_threshold = 15
+wrap_comments = true \ No newline at end of file
diff --git a/src/day_1.rs b/src/day_1.rs
new file mode 100644
index 0000000..6632baa
--- /dev/null
+++ b/src/day_1.rs
@@ -0,0 +1,47 @@
+pub fn run(input: Vec<String>)
+{
+ // Convert inputs to unsigned integer numbers
+ let input: Vec<u32> = input
+ .into_iter()
+ .map(|s| {
+ s.parse()
+ .expect("All lines are required to be unsigned integers")
+ })
+ .collect();
+
+ part1(&input);
+ part2(&input);
+}
+
+fn part1(input: &[u32])
+{
+ let mut last_depth = u32::MAX;
+ let num_down_slopes = input
+ .iter()
+ .filter(|&depth| {
+ let going_deeper = *depth > last_depth;
+ last_depth = *depth;
+
+ going_deeper
+ })
+ .count();
+
+ println!("Solution to part 1: {}", num_down_slopes);
+}
+
+fn part2(input: &[u32])
+{
+ let mut last_aliased_depth = u32::MAX;
+ let num_aliased_down_slopes = input
+ .windows(3)
+ .filter(|&depths| {
+ let aliased_depth: u32 = depths.iter().sum();
+ let going_deeper = aliased_depth > last_aliased_depth;
+ last_aliased_depth = aliased_depth;
+
+ going_deeper
+ })
+ .count();
+
+ println!("Solution to part 2: {}", num_aliased_down_slopes);
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..c45571e
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,52 @@
+mod day_1;
+
+use std::fs::File;
+use std::io::{BufRead, BufReader};
+use std::path::PathBuf;
+use std::str::FromStr;
+
+use argparse::{ArgumentParser, Store};
+
+fn read_input(day: u8) -> Vec<String>
+{
+ let file_name = PathBuf::from_str("input")
+ .unwrap()
+ .join(format!("day_{}.txt", day));
+ let input_file =
+ File::open(&file_name).expect(&format!("Unable to open input file {:?}", file_name));
+ let reader = BufReader::new(input_file);
+
+ reader
+ .lines()
+ .map(|r| r.expect("Unable to read line in input file"))
+ .collect()
+}
+
+fn run(day: u8, input: Vec<String>)
+{
+ match day {
+ 1 => day_1::run(input),
+ o => panic!("Day {} is not implemented (yet)", o),
+ }
+}
+
+fn main()
+{
+ let mut day: u8 = 1;
+ {
+ let mut ap = ArgumentParser::new();
+ ap.set_description("Run advent of code 2015 solvers");
+
+ ap.refer(&mut day).add_option(
+ &["-d", "--day"],
+ Store,
+ "The day of the month of which the solver should be run",
+ );
+
+ ap.parse_args_or_exit();
+ }
+
+ println!("Attempting to solve day {}", day);
+ let input = read_input(day);
+ run(day, input);
+}