Start AoC.

This commit is contained in:
Joe Ardent 2022-12-01 12:45:22 -08:00
parent a52a93436f
commit cc8158e35e
5 changed files with 60 additions and 0 deletions

2
2022-aoc/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/input

10
2022-aoc/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[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]
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"

40
2022-aoc/src/d1.rs Normal file
View File

@ -0,0 +1,40 @@
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
#[aoc_generator(day1)]
fn parse_input_day1(input: &str) -> Vec<i32> {
let mut cur = 0;
let mut out = Vec::new();
for line in input.lines() {
if let Ok(n) = line.parse::<i32>() {
cur += n;
} else {
out.push(cur);
cur = 0;
}
}
out
}
#[aoc_run(day1, part1)]
fn part1(calories: &[i32]) -> i32 {
unsafe { *calories.iter().max().unwrap_unchecked() }
}
#[aoc_run(day1, part2)]
fn p2(cals: &[i32]) -> i32 {
let mut cals = std::collections::BinaryHeap::from_iter(cals.iter().copied());
let mut out = 0;
for _ in 0..3 {
out += cals.pop().unwrap();
}
out
}
// much slower than using a heap
#[aoc_run(day1, part2, alt)]
fn p2a(cals: &[i32]) -> i32 {
let mut cals = cals.to_owned();
cals.sort_unstable_by(|x, y| y.cmp(x));
cals.iter().take(3).sum()
}

5
2022-aoc/src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
use aoc_runner_derive::aoc_lib;
mod d1;
aoc_lib! { year = 2022 }

3
2022-aoc/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
use aoc_runner_derive::aoc_main;
aoc_main! { lib = aoc }