initial setup and day 1

This commit is contained in:
missing 2022-12-03 13:14:58 -06:00
commit 9ddfd522af
4 changed files with 69 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/Cargo.lock
/input

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "aoc2022"
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"

49
src/days/day1.rs Normal file
View file

@ -0,0 +1,49 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day1)]
fn day1_generator(s: &str) -> Vec<Vec<u32>> {
let mut iter = s.lines();
let mut res = Vec::new();
let mut current = Vec::new();
loop {
match iter.next() {
Some("") => {
res.push(current);
current = Vec::new();
}
Some(s) => {
current.push(s.parse::<u32>().unwrap());
}
None => {
res.push(current);
return res;
}
}
}
}
#[aoc(day1, part1)]
fn day1_part1(input: &[Vec<u32>]) -> u32 {
input.iter().map(|v| v.iter().sum()).max().unwrap()
}
#[aoc(day1, part2)]
fn day1_part2(input: &[Vec<u32>]) -> u32 {
let mut res = (0, 0, 0);
for mut item in input.iter().map(|v| v.iter().sum()) {
if res.0 < item {
std::mem::swap(&mut item, &mut res.0)
}
if res.1 < item {
std::mem::swap(&mut item, &mut res.1)
}
if res.2 < item {
std::mem::swap(&mut item, &mut res.2)
}
}
res.0 + res.1 + res.2
}

7
src/lib.rs Normal file
View file

@ -0,0 +1,7 @@
use aoc_runner_derive::aoc_lib;
mod days {
mod day1;
}
aoc_lib! { year = 2022 }