mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 22:00:31 +01:00
Compare commits
3 Commits
524527dbd1
...
2a419bb468
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a419bb468 | |||
| bf953b7980 | |||
| 033625f041 |
@@ -9,7 +9,7 @@ use criterion::Criterion;
|
|||||||
use aoc_2023::get_implementation;
|
use aoc_2023::get_implementation;
|
||||||
|
|
||||||
/// Number of days we have an implementation to benchmark
|
/// Number of days we have an implementation to benchmark
|
||||||
const DAYS_IMPLEMENTED: u8 = 1;
|
const DAYS_IMPLEMENTED: u8 = 2;
|
||||||
|
|
||||||
fn read_input(day: u8) -> std::io::Result<Vec<u8>> {
|
fn read_input(day: u8) -> std::io::Result<Vec<u8>> {
|
||||||
let input_path = format!("inputs/{day:02}.txt");
|
let input_path = format!("inputs/{day:02}.txt");
|
||||||
|
|||||||
@@ -16,6 +16,14 @@ use nom::IResult;
|
|||||||
use nom::InputLength;
|
use nom::InputLength;
|
||||||
use nom::Parser;
|
use nom::Parser;
|
||||||
|
|
||||||
|
pub fn convert_nom_error(e: nom::error::Error<&[u8]>) -> anyhow::Error {
|
||||||
|
anyhow::anyhow!(
|
||||||
|
"Parser error {:?} to parse at {}",
|
||||||
|
e.code,
|
||||||
|
String::from_utf8_lossy(e.input)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse input from some nom parser and return as an anyhow result
|
/// 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
|
/// This method exists as a convenience because nom's errors cannot otherwise be easily converted to
|
||||||
@@ -24,13 +32,7 @@ pub fn parse_input<'a, O>(
|
|||||||
input: &'a [u8],
|
input: &'a [u8],
|
||||||
mut parser: impl Parser<&'a [u8], O, nom::error::Error<&'a [u8]>>,
|
mut parser: impl Parser<&'a [u8], O, nom::error::Error<&'a [u8]>>,
|
||||||
) -> Result<O> {
|
) -> Result<O> {
|
||||||
let (unparsed, output) = parser.parse(input).finish().map_err(|e| {
|
let (unparsed, output) = parser.parse(input).finish().map_err(convert_nom_error)?;
|
||||||
anyhow::anyhow!(
|
|
||||||
"Parser error {:?} to parse at {}",
|
|
||||||
e.code,
|
|
||||||
String::from_utf8_lossy(e.input)
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if !unparsed.is_empty() {
|
if !unparsed.is_empty() {
|
||||||
Err(anyhow::anyhow!(
|
Err(anyhow::anyhow!(
|
||||||
|
|||||||
@@ -1,7 +1,98 @@
|
|||||||
pub fn part1(_input: &[u8]) -> anyhow::Result<String> {
|
use nom::branch::alt;
|
||||||
anyhow::bail!("Not implemented")
|
use nom::bytes::complete::tag;
|
||||||
|
use nom::character::complete::newline;
|
||||||
|
use nom::combinator::iterator;
|
||||||
|
use nom::combinator::opt;
|
||||||
|
use nom::combinator::value;
|
||||||
|
use nom::multi::fold_many1;
|
||||||
|
use nom::sequence::preceded;
|
||||||
|
use nom::sequence::separated_pair;
|
||||||
|
use nom::sequence::terminated;
|
||||||
|
use nom::IResult;
|
||||||
|
|
||||||
|
use crate::common::convert_nom_error;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
#[repr(usize)]
|
||||||
|
enum Color {
|
||||||
|
Red,
|
||||||
|
Green,
|
||||||
|
Blue,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
fn parse_game(i: &[u8]) -> IResult<&[u8], (u8, [u8; 3])> {
|
||||||
anyhow::bail!("Not implemented")
|
let parse_color = terminated(
|
||||||
|
separated_pair(
|
||||||
|
nom::character::complete::u8,
|
||||||
|
tag(" "),
|
||||||
|
alt((
|
||||||
|
value(Color::Red, tag("red")),
|
||||||
|
value(Color::Green, tag("green")),
|
||||||
|
value(Color::Blue, tag("blue")),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
opt(alt((tag(", "), tag("; ")))),
|
||||||
|
);
|
||||||
|
|
||||||
|
separated_pair(
|
||||||
|
preceded(tag("Game "), nom::character::complete::u8),
|
||||||
|
tag(": "),
|
||||||
|
terminated(
|
||||||
|
fold_many1(
|
||||||
|
parse_color,
|
||||||
|
|| [0u8; 3],
|
||||||
|
|mut cur, (value, color)| {
|
||||||
|
cur[color as usize] = Ord::max(cur[color as usize], value);
|
||||||
|
cur
|
||||||
|
},
|
||||||
|
),
|
||||||
|
newline,
|
||||||
|
),
|
||||||
|
)(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parts_common(input: &[u8], map: impl Fn((u8, [u8; 3])) -> u32) -> anyhow::Result<String> {
|
||||||
|
let mut game_it = iterator(input, parse_game);
|
||||||
|
|
||||||
|
let total: u32 = game_it.into_iter().map(map).sum();
|
||||||
|
|
||||||
|
game_it.finish().map_err(|e| match e {
|
||||||
|
nom::Err::Incomplete(_) => anyhow::anyhow!("unreachable"),
|
||||||
|
nom::Err::Failure(e) | nom::Err::Error(e) => convert_nom_error(e),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(total.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
||||||
|
parts_common(input, |(id, colors)| {
|
||||||
|
if colors[0] <= 12 && colors[1] <= 13 && colors[2] <= 14 {
|
||||||
|
u32::from(id)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(input: &[u8]) -> anyhow::Result<String> {
|
||||||
|
parts_common(input, |(_, colors)| {
|
||||||
|
u32::from(colors[0]) * u32::from(colors[1]) * u32::from(colors[2])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SAMPLE: &[u8] = include_bytes!("samples/02.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
assert_eq!(part1(SAMPLE).unwrap(), "8");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part2() {
|
||||||
|
assert_eq!(part2(SAMPLE).unwrap(), "2286");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
2023/src/samples/02.txt
Normal file
5
2023/src/samples/02.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||||
Reference in New Issue
Block a user