mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 22:00:31 +01:00
Compare commits
4 Commits
2ae2d6baa8
...
2022/day-2
| Author | SHA1 | Date | |
|---|---|---|---|
| 561fd2f07c | |||
| 256d351f8e | |||
| 48594a75e6 | |||
| 85a51b13c1 |
@@ -8,7 +8,7 @@ use criterion::BenchmarkId;
|
||||
use criterion::Criterion;
|
||||
|
||||
/// Number of days we have an implementation to benchmark
|
||||
const DAYS_IMPLEMENTED: u8 = 0;
|
||||
const DAYS_IMPLEMENTED: u8 = 2;
|
||||
|
||||
fn read_input(day: u8) -> Vec<u8> {
|
||||
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
2500
2022/inputs/02.txt
Normal file
2500
2022/inputs/02.txt
Normal file
File diff suppressed because it is too large
Load Diff
94
2022/src/common.rs
Normal file
94
2022/src/common.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
//! Common helper utilities to all days
|
||||
|
||||
use anyhow::Result;
|
||||
use nom::error::ErrorKind;
|
||||
use nom::error::ParseError;
|
||||
use nom::Finish;
|
||||
use nom::IResult;
|
||||
use nom::InputLength;
|
||||
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> {
|
||||
let (unparsed, output) = parser.parse(input).finish().map_err(|e| {
|
||||
anyhow::anyhow!(
|
||||
"Parser error {:?} to parse at {}",
|
||||
e.code,
|
||||
String::from_utf8_lossy(e.input)
|
||||
)
|
||||
})?;
|
||||
|
||||
if !unparsed.is_empty() {
|
||||
Err(anyhow::anyhow!(
|
||||
"Not all input consumed: {}",
|
||||
String::from_utf8_lossy(unparsed)
|
||||
))
|
||||
} else {
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies a parser iteratively and reduces the results using the given function. Fails if the
|
||||
/// embedded parser doesn't return at least one result.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - `f`: the function to apply
|
||||
/// - `g`: the function that combines the result o `f` with previous results
|
||||
///
|
||||
/// This implementation is based on [`nom::multi::fold_many1`] with minor differences. If
|
||||
/// successful, this should probably be upstreamed.
|
||||
pub fn reduce_many1<I, O, E, F>(
|
||||
mut f: F,
|
||||
mut g: impl FnMut(O, O) -> O,
|
||||
) -> impl FnMut(I) -> IResult<I, O, E>
|
||||
where
|
||||
I: Clone + InputLength,
|
||||
E: ParseError<I>,
|
||||
F: Parser<I, O, E>,
|
||||
{
|
||||
// Cannot delegate to fold_many0 because that would make the function FnOnce rather than FnMut,
|
||||
// since it would transfer ownership of the embedded parser to fold_many0.
|
||||
move |i: I| {
|
||||
let _i = i.clone();
|
||||
match f.parse(_i) {
|
||||
Err(nom::Err::Error(_)) => {
|
||||
Err(nom::Err::Error(E::from_error_kind(i, ErrorKind::Many1)))
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
Ok((i1, mut acc)) => {
|
||||
let mut input = i1;
|
||||
|
||||
loop {
|
||||
let _input = input.clone();
|
||||
let len = input.input_len();
|
||||
match f.parse(_input) {
|
||||
Err(nom::Err::Error(_)) => {
|
||||
break;
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
Ok((i, o)) => {
|
||||
// infinite loop check: the parser must always consume
|
||||
if i.input_len() == len {
|
||||
return Err(nom::Err::Failure(E::from_error_kind(
|
||||
i,
|
||||
ErrorKind::Many1,
|
||||
)));
|
||||
}
|
||||
|
||||
acc = g(acc, o);
|
||||
input = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((input, acc))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,55 @@
|
||||
use std::ops::Add;
|
||||
|
||||
use anyhow::Result;
|
||||
use nom::character::complete::newline;
|
||||
use nom::combinator::opt;
|
||||
use nom::multi::separated_list0;
|
||||
use nom::sequence::terminated;
|
||||
use nom::IResult;
|
||||
|
||||
pub fn part1(_input: &[u8]) -> Result<String> {
|
||||
todo!()
|
||||
use crate::common::parse_input;
|
||||
use crate::common::reduce_many1;
|
||||
|
||||
fn parse_elf(input: &[u8]) -> IResult<&[u8], i32> {
|
||||
reduce_many1(terminated(nom::character::complete::i32, newline), Add::add)(input)
|
||||
}
|
||||
|
||||
pub fn part2(_input: &[u8]) -> Result<String> {
|
||||
todo!()
|
||||
fn parse_max(input: &[u8]) -> IResult<&[u8], i32> {
|
||||
reduce_many1(terminated(parse_elf, opt(newline)), 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,9 +1,129 @@
|
||||
use std::ops::Add;
|
||||
|
||||
use anyhow::Result;
|
||||
use nom::character::complete::newline;
|
||||
use nom::combinator::map;
|
||||
use nom::combinator::map_res;
|
||||
use nom::sequence::separated_pair;
|
||||
use nom::sequence::terminated;
|
||||
use nom::IResult;
|
||||
|
||||
pub fn part1(_input: &[u8]) -> Result<String> {
|
||||
todo!()
|
||||
use crate::common::parse_input;
|
||||
use crate::common::reduce_many1;
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
enum Rps {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
pub fn part2(_input: &[u8]) -> Result<String> {
|
||||
todo!()
|
||||
impl Rps {
|
||||
/// Score we get by playing this move
|
||||
fn score(self) -> u32 {
|
||||
match self {
|
||||
Rps::Rock => 1,
|
||||
Rps::Paper => 2,
|
||||
Rps::Scissors => 3,
|
||||
}
|
||||
}
|
||||
|
||||
/// Score we get from the result from playing given other
|
||||
fn score_against(self, other: Self) -> u32 {
|
||||
match (self, other) {
|
||||
(a, b) if a == b => 3,
|
||||
(Rps::Rock, Rps::Paper) | (Rps::Paper, Rps::Scissors) | (Rps::Scissors, Rps::Rock) => 0,
|
||||
_ => 6,
|
||||
}
|
||||
}
|
||||
|
||||
/// Score if the result is according to the instruction
|
||||
fn score_result(self) -> u32 {
|
||||
match self {
|
||||
Rps::Rock => 0, // Rock is lose
|
||||
Rps::Paper => 3, // Paper is draw
|
||||
Rps::Scissors => 6, // Scissors is win
|
||||
}
|
||||
}
|
||||
|
||||
/// Move we need to achieve the result indicated by self
|
||||
fn needed(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Rps::Paper, other) => other,
|
||||
(Rps::Rock, Rps::Rock) => Rps::Scissors,
|
||||
(Rps::Rock, Rps::Paper) => Rps::Rock,
|
||||
(Rps::Rock, Rps::Scissors) => Rps::Paper,
|
||||
(Rps::Scissors, Rps::Rock) => Rps::Paper,
|
||||
(Rps::Scissors, Rps::Paper) => Rps::Scissors,
|
||||
(Rps::Scissors, Rps::Scissors) => Rps::Rock,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for Rps {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
b'A' | b'X' => Ok(Rps::Rock),
|
||||
b'B' | b'Y' => Ok(Rps::Paper),
|
||||
b'C' | b'Z' => Ok(Rps::Scissors),
|
||||
_ => Err(anyhow::anyhow!("Invalid RPS: {value}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_line(input: &[u8]) -> IResult<&[u8], (Rps, Rps)> {
|
||||
fn parse_rps(input: &[u8]) -> IResult<&[u8], Rps> {
|
||||
// Note: alpha1 also sort of works but is significantly slower
|
||||
map_res(nom::bytes::complete::take(1usize), |v: &[u8]| {
|
||||
Rps::try_from(v[0])
|
||||
})(input)
|
||||
}
|
||||
|
||||
terminated(
|
||||
separated_pair(parse_rps, nom::character::complete::char(' '), parse_rps),
|
||||
newline,
|
||||
)(input)
|
||||
}
|
||||
|
||||
pub fn part1(input: &[u8]) -> Result<String> {
|
||||
parse_input(
|
||||
input,
|
||||
reduce_many1(
|
||||
map(parse_line, |(them, us)| us.score() + us.score_against(them)),
|
||||
Add::add,
|
||||
),
|
||||
)
|
||||
.map(|sum| sum.to_string())
|
||||
}
|
||||
|
||||
pub fn part2(input: &[u8]) -> Result<String> {
|
||||
parse_input(
|
||||
input,
|
||||
reduce_many1(
|
||||
map(parse_line, |(them, us)| {
|
||||
us.score_result() + us.needed(them).score()
|
||||
}),
|
||||
Add::add,
|
||||
),
|
||||
)
|
||||
.map(|sum| sum.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("samples/02.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
assert_eq!(part1(SAMPLE).unwrap(), "15")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
assert_eq!(part2(SAMPLE).unwrap(), "12")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use anyhow::Result;
|
||||
|
||||
mod common;
|
||||
mod day01;
|
||||
mod day02;
|
||||
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
|
||||
3
2022/src/samples/02.txt
Normal file
3
2022/src/samples/02.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
A Y
|
||||
B X
|
||||
C Z
|
||||
Reference in New Issue
Block a user