2022: day 01 part 1
This commit is contained in:
parent
a5fd485c10
commit
f3f3b40d47
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -100,6 +100,22 @@ dependencies = [
|
|||
"criterion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc2022"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc2022_bench"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc2022",
|
||||
"criterion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[workspace]
|
||||
members = ["aoc20*", "aoc20*/aoc20*_bench"]
|
||||
|
||||
default-members = ["aoc2021"]
|
||||
default-members = ["aoc2022"]
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
|
18
aoc2022/Cargo.toml
Normal file
18
aoc2022/Cargo.toml
Normal file
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "aoc2022"
|
||||
version = "0.1.0"
|
||||
authors = ["Antoine Martin <antoine@alarsyo.net>"]
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
aoc = { path = "../aoc" }
|
||||
anyhow = "1.0"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "aoc2022"
|
||||
path = "src/main.rs"
|
17
aoc2022/aoc2022_bench/Cargo.toml
Normal file
17
aoc2022/aoc2022_bench/Cargo.toml
Normal file
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
name = "aoc2022_bench"
|
||||
version = "0.1.0"
|
||||
authors = ["Antoine Martin <antoine@alarsyo.net>"]
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
aoc2022 = { path = "../" }
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
|
||||
[[bench]]
|
||||
name = "aoc2022_bench"
|
||||
harness = false
|
14
aoc2022/aoc2022_bench/benches/aoc2022_bench.rs
Normal file
14
aoc2022/aoc2022_bench/benches/aoc2022_bench.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
|
||||
use aoc2022::day01;
|
||||
|
||||
fn aoc2022_all(c: &mut Criterion) {
|
||||
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
name = all_days;
|
||||
config = Criterion::default().sample_size(200);
|
||||
targets = aoc2022_all
|
||||
}
|
||||
criterion_main!(all_days);
|
2253
aoc2022/input/day01.txt
Normal file
2253
aoc2022/input/day01.txt
Normal file
File diff suppressed because it is too large
Load diff
14
aoc2022/input/day01_provided.txt
Normal file
14
aoc2022/input/day01_provided.txt
Normal file
|
@ -0,0 +1,14 @@
|
|||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
63
aoc2022/src/day01.rs
Normal file
63
aoc2022/src/day01.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use std::fmt::Write;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
const INPUT: &str = include_str!("../input/day01.txt");
|
||||
|
||||
pub fn run() -> Result<String> {
|
||||
let mut res = String::with_capacity(128);
|
||||
|
||||
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> Result<u64> {
|
||||
let inventories = input
|
||||
.split("\n\n")
|
||||
.map(str::parse::<Inventory>)
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
inventories
|
||||
.iter()
|
||||
.map(Inventory::total_calories)
|
||||
.max()
|
||||
.context("inventory list was empty")
|
||||
}
|
||||
|
||||
struct Inventory(Vec<u64>);
|
||||
|
||||
impl std::str::FromStr for Inventory {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Inventory(
|
||||
s.lines()
|
||||
.map(|line| line.parse::<u64>().map_err(anyhow::Error::new))
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Inventory {
|
||||
fn total_calories(&self) -> u64 {
|
||||
self.0.iter().sum()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const PROVIDED: &str = include_str!("../input/day01_provided.txt");
|
||||
|
||||
#[test]
|
||||
fn part1_provided() {
|
||||
assert_eq!(part1(PROVIDED).unwrap(), 24000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_real() {
|
||||
assert_eq!(part1(INPUT).unwrap(), 68923);
|
||||
}
|
||||
}
|
3
aoc2022/src/lib.rs
Normal file
3
aoc2022/src/lib.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
#![warn(clippy::explicit_iter_loop, clippy::redundant_closure_for_method_calls)]
|
||||
|
||||
pub mod day01;
|
11
aoc2022/src/main.rs
Normal file
11
aoc2022/src/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use anyhow::Result;
|
||||
|
||||
use aoc::DayFunc;
|
||||
|
||||
use aoc2022::day01;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let days: &[DayFunc] = &[day01::run];
|
||||
|
||||
aoc::run(days)
|
||||
}
|
|
@ -32,16 +32,16 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1637593665,
|
||||
"narHash": "sha256-R7jKS7A+0tZS8qD5pBr1UFcMiTdsw5bfoxgXbYsoWhM=",
|
||||
"lastModified": 1669834992,
|
||||
"narHash": "sha256-YnhZGHgb4C3Q7DSGisO/stc50jFb9F/MzHeKS4giotg=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "98747f27ecfee70c8c97b195cbb94df80a074dda",
|
||||
"rev": "596a8e828c5dfa504f91918d0fa4152db3ab5502",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixpkgs-unstable",
|
||||
"ref": "nixos-22.11",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue