2022 day01

This commit is contained in:
nora 2024-11-10 16:43:01 +01:00
parent fd2ea0612b
commit e153ec51cd
20 changed files with 3180 additions and 82 deletions

15
2022/day01/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "day01"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true
helper.workspace = true
divan.workspace = true
[[bench]]
name = "benches"
harness = false

View file

@ -0,0 +1,3 @@
fn main() {
day01::bench();
}

2253
2022/day01/input.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

59
2022/day01/src/lib.rs Normal file
View file

@ -0,0 +1,59 @@
use helper::{parse_unwrap, Day, Variants};
pub fn main() {
helper::main::<Day01>(include_str!("../input.txt"));
}
struct Day01;
helper::define_variants! {
day => crate::Day01;
part1 {
basic => crate::part1;
}
part2 {
basic => crate::part2;
}
}
impl Day for Day01 {
fn part1() -> Variants {
part1_variants!(construct_variants)
}
fn part2() -> Variants {
part2_variants!(construct_variants)
}
}
fn part1(input: &str) -> u64 {
input
.trim()
.split("\n\n")
.map(|elf| elf.trim().split("\n").map(parse_unwrap).sum())
.max()
.unwrap()
}
fn part2(input: &str) -> u64 {
let mut all = input
.trim()
.split("\n\n")
.map(|elf| elf.trim().split("\n").map(parse_unwrap).sum())
.collect::<Vec<u64>>();
all.sort();
all[(all.len() - 3)..].iter().copied().sum::<u64>()
}
helper::tests! {
day01 Day01;
part1 {
small => 24000;
default => 70698;
}
part2 {
small => 45000;
default => 206643;
}
}
helper::benchmarks! {}

3
2022/day01/src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
day01::main();
}