Initial commit: Day 1 & 2

main
Charlotte Som 2021-12-02 11:56:34 +00:00
commit 68cc03828c
8 changed files with 133 additions and 0 deletions

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

25
Cargo.lock generated Normal file
View File

@ -0,0 +1,25 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc"
version = "0.1.0"
dependencies = [
"itertools",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "itertools"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
dependencies = [
"either",
]

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "aoc"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.10.1"

2
inputs/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

29
src/day01.rs Normal file
View File

@ -0,0 +1,29 @@
use itertools::Itertools;
const INPUT: &str = include_str!("../inputs/01");
fn input_numbers() -> impl Iterator<Item = usize> {
INPUT.lines().filter_map(|s| s.parse::<usize>().ok())
}
fn part_1() -> usize {
input_numbers()
.tuple_windows()
.filter(|(a, b)| b > a)
.count()
}
fn part_2() -> usize {
input_numbers()
.tuple_windows()
.map(|(a, b, c)| a + b + c)
.tuple_windows()
.filter(|(a, b)| b > a)
.count()
}
pub fn main() {
println!("Day 1:");
println!(" Part 1: {}", part_1());
println!(" Part 2: {}", part_2());
}

51
src/day02.rs Normal file
View File

@ -0,0 +1,51 @@
const INPUT: &str = include_str!("../inputs/02");
fn get_instructions() -> impl Iterator<Item = (&'static str, isize)> {
INPUT
.lines()
.filter_map(|s| s.split_once(' '))
.filter_map(|(op, n)| n.parse::<isize>().ok().map(|n| (op, n)))
}
fn part_1() -> isize {
let x: isize = get_instructions()
.filter(|(op, _)| *op == "forward")
.map(|(_, n)| n)
.sum();
let y: isize = get_instructions()
.filter_map(|(op, n)| match op {
"up" => Some(-n),
"down" => Some(n),
_ => None,
})
.sum();
x * y
}
fn part_2() -> isize {
let mut x = 0isize;
let mut depth = 0isize;
let mut aim = 0isize;
for (op, n) in get_instructions() {
match op {
"forward" => {
x += n;
depth += n * aim;
}
"down" => aim += n,
"up" => aim -= n,
_ => unreachable!(),
}
}
x * depth
}
pub fn main() {
println!("Day 2:");
println!(" Part 1: {}", part_1());
println!(" Part 2: {}", part_2());
}

7
src/main.rs Normal file
View File

@ -0,0 +1,7 @@
mod day01;
mod day02;
fn main() {
day01::main();
day02::main();
}