1
Fork 0

Initial commit & solve day 1.

This commit is contained in:
Bauke 2021-12-01 12:50:38 +01:00
commit 84add975e1
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
5 changed files with 77 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -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/

15
Cargo.toml Normal file
View File

@ -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 <me@bauke.xyz>"]
edition = "2021"
[[bin]]
name = "advent-of-code-2021"
path = "source/main.rs"
[dependencies]
color-eyre = "0.5.11"
itertools = "0.10.1"

2
rustfmt.toml Normal file
View File

@ -0,0 +1,2 @@
max_width = 80
tab_spaces = 2

37
source/day_01/mod.rs Normal file
View File

@ -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<usize> {
Ok(
parse_measurements(input)?
.into_iter()
.tuple_windows()
.filter(|(previous, next)| next > previous)
.count(),
)
}
fn part_2(input: &str) -> Result<usize> {
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<Vec<i32>> {
input
.lines()
.map(|line| line.parse::<i32>())
.collect::<Result<_, _>>()
.map_err(Into::into)
}

10
source/main.rs Normal file
View File

@ -0,0 +1,10 @@
mod day_01;
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
println!("Advent of Code 2021");
day_01::solve()?;
Ok(())
}