Initial commit & solve day 1.
This commit is contained in:
commit
84add975e1
|
@ -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/
|
|
@ -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"
|
|
@ -0,0 +1,2 @@
|
||||||
|
max_width = 80
|
||||||
|
tab_spaces = 2
|
|
@ -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)
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
mod day_01;
|
||||||
|
|
||||||
|
fn main() -> color_eyre::Result<()> {
|
||||||
|
color_eyre::install()?;
|
||||||
|
|
||||||
|
println!("Advent of Code 2021");
|
||||||
|
day_01::solve()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue