From 84add975e1e99efc89f3eba0d3150d84e1d3e243 Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 1 Dec 2021 12:50:38 +0100 Subject: [PATCH] Initial commit & solve day 1. --- .gitignore | 13 +++++++++++++ Cargo.toml | 15 +++++++++++++++ rustfmt.toml | 2 ++ source/day_01/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ source/main.rs | 10 ++++++++++ 5 files changed, 77 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 rustfmt.toml create mode 100644 source/day_01/mod.rs create mode 100644 source/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ccb3f43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# 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 + +# Advent of Code input data +/data/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c295b2b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +# https://doc.rust-lang.org/cargo/reference/manifest.html + +[package] +name = "advent-of-code-2021" +version = "0.1.0" +authors = ["Bauke "] +edition = "2021" + +[[bin]] +name = "advent-of-code-2021" +path = "source/main.rs" + +[dependencies] +color-eyre = "0.5.11" +itertools = "0.10.1" diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..4c1eefa --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 80 +tab_spaces = 2 diff --git a/source/day_01/mod.rs b/source/day_01/mod.rs new file mode 100644 index 0000000..6048b8a --- /dev/null +++ b/source/day_01/mod.rs @@ -0,0 +1,37 @@ +use color_eyre::Result; +use itertools::Itertools; + +pub fn solve() -> Result<()> { + let input_data = include_str!("../../data/day_01.txt").trim(); + println!("Day 01 Part 1: {}", part_1(input_data)?); + println!("Day 01 Part 2: {}", part_2(input_data)?); + Ok(()) +} + +fn part_1(input: &str) -> Result { + Ok( + parse_measurements(input)? + .into_iter() + .tuple_windows() + .filter(|(previous, next)| next > previous) + .count(), + ) +} + +fn part_2(input: &str) -> Result { + Ok( + parse_measurements(input)? + .into_iter() + .tuple_windows() + .filter(|(a, b, c, d)| b + c + d > a + b + c) + .count(), + ) +} + +fn parse_measurements(input: &str) -> Result> { + input + .lines() + .map(|line| line.parse::()) + .collect::>() + .map_err(Into::into) +} diff --git a/source/main.rs b/source/main.rs new file mode 100644 index 0000000..8d42540 --- /dev/null +++ b/source/main.rs @@ -0,0 +1,10 @@ +mod day_01; + +fn main() -> color_eyre::Result<()> { + color_eyre::install()?; + + println!("Advent of Code 2021"); + day_01::solve()?; + + Ok(()) +}