aoc 2022 day 1

This commit is contained in:
Joseph Montanaro 2022-12-02 14:52:51 -08:00
parent 8c6ffc2eb9
commit c7c444bce9
4 changed files with 79 additions and 0 deletions

7
2022/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent-2022"
version = "0.1.0"

16
2022/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "advent-2022"
version = "0.1.0"
edition = "2021"
[lib]
name = "advent_2022"
path = "src/lib.rs"
[[bin]]
name = "day1"
path = "src/day1.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

44
2022/src/day1.rs Normal file
View File

@ -0,0 +1,44 @@
const DATA: &'static str = include_str!("../data/day1.txt");
// const DATA: &'static str = "1000
// 2000
// 3000
// 4000
// 5000
// 6000
// 7000
// 8000
// 9000
// 10000
// ";
fn main() {
let mut counts = DATA.trim()
.split("\n\n")
.map(|lines| {
lines.split("\n")
.map(|c| c.parse::<u64>().unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
counts.sort_by_key(|values| values.iter().sum::<u64>());
let max_total_cals: u64 = counts.last()
.unwrap()
.iter()
.sum();
let top3_total_cals: &u64 = &counts[(counts.len() - 3)..]
.iter()
.flatten()
.sum();
println!("Part 1: {max_total_cals}");
println!("Part 2: {top3_total_cals}");
}

12
2022/src/lib.rs Normal file
View File

@ -0,0 +1,12 @@
#[allow(unused_macros)]
macro_rules! get_data {
($filename: literal) => {
include_str!(concat!("../data/", $filename, ".txt"))
}
}
#[allow(dead_code)]
fn main() {
todo!();
}