mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2022 day 1
This commit is contained in:
@@ -8,7 +8,7 @@ use criterion::BenchmarkId;
|
|||||||
use criterion::Criterion;
|
use criterion::Criterion;
|
||||||
|
|
||||||
/// Number of days we have an implementation to benchmark
|
/// Number of days we have an implementation to benchmark
|
||||||
const DAYS_IMPLEMENTED: u8 = 0;
|
const DAYS_IMPLEMENTED: u8 = 1;
|
||||||
|
|
||||||
fn read_input(day: u8) -> Vec<u8> {
|
fn read_input(day: u8) -> Vec<u8> {
|
||||||
let input_path = format!("inputs/{:02}.txt", day);
|
let input_path = format!("inputs/{:02}.txt", day);
|
||||||
|
|||||||
2259
2022/inputs/01.txt
Normal file
2259
2022/inputs/01.txt
Normal file
File diff suppressed because it is too large
Load Diff
19
2022/src/common.rs
Normal file
19
2022/src/common.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
//! Common helper utilities to all days
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use nom::Finish;
|
||||||
|
use nom::Parser;
|
||||||
|
|
||||||
|
/// Parse input from some nom parser and return as an anyhow result
|
||||||
|
///
|
||||||
|
/// This method exists as a convenience because nom's errors cannot otherwise be easily converted to
|
||||||
|
/// an anyhow error, and I don't want to keep track of custom error implementations here.
|
||||||
|
pub fn parse_input<'a, O>(
|
||||||
|
input: &'a [u8],
|
||||||
|
mut parser: impl Parser<&'a [u8], O, nom::error::Error<&'a [u8]>>,
|
||||||
|
) -> Result<O> {
|
||||||
|
match parser.parse(input).finish() {
|
||||||
|
Ok((_, value)) => Ok(value),
|
||||||
|
Err(err) => anyhow::bail!("Failed to parse at: {err:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,59 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use nom::character::complete::newline;
|
||||||
|
use nom::combinator::opt;
|
||||||
|
use nom::multi::fold_many1;
|
||||||
|
use nom::multi::separated_list0;
|
||||||
|
use nom::sequence::terminated;
|
||||||
|
use nom::IResult;
|
||||||
|
|
||||||
pub fn part1(_input: &[u8]) -> Result<String> {
|
use crate::common::parse_input;
|
||||||
todo!()
|
|
||||||
|
fn parse_elf(input: &[u8]) -> IResult<&[u8], i32> {
|
||||||
|
fold_many1(
|
||||||
|
terminated(nom::character::complete::i32, newline),
|
||||||
|
|| 0,
|
||||||
|
Add::add,
|
||||||
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> Result<String> {
|
fn parse_max(input: &[u8]) -> IResult<&[u8], i32> {
|
||||||
todo!()
|
fold_many1(terminated(parse_elf, opt(newline)), || 0, Ord::max)(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &[u8]) -> Result<String> {
|
||||||
|
let result = parse_input(input, parse_max)?.to_string();
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_elf_list(input: &[u8]) -> IResult<&[u8], Vec<i32>> {
|
||||||
|
separated_list0(newline, parse_elf)(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(input: &[u8]) -> Result<String> {
|
||||||
|
let mut elves = parse_input(input, parse_elf_list)?;
|
||||||
|
|
||||||
|
let (first, third, _) = elves.select_nth_unstable_by(2, |a, b| Ord::cmp(b, a));
|
||||||
|
|
||||||
|
let result = first[1] + first[0] + *third;
|
||||||
|
|
||||||
|
Ok(result.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SAMPLE: &[u8] = include_bytes!("samples/01.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
assert_eq!(part1(SAMPLE).unwrap(), "24000");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part2() {
|
||||||
|
assert_eq!(part2(SAMPLE).unwrap(), "45000");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
mod common;
|
||||||
mod day01;
|
mod day01;
|
||||||
mod day02;
|
mod day02;
|
||||||
mod day03;
|
mod day03;
|
||||||
|
|||||||
14
2022/src/samples/01.txt
Normal file
14
2022/src/samples/01.txt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000
|
||||||
Reference in New Issue
Block a user