2022: day 01 part 1
This commit is contained in:
parent
a5fd485c10
commit
f3f3b40d47
12 changed files with 2415 additions and 6 deletions
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue