Implement 2022 day 21 part 1

This commit is contained in:
2022-12-21 08:47:47 +01:00
parent b1d9314bc7
commit 6c58a3ba69
3 changed files with 2881 additions and 2 deletions

2755
2022/inputs/21.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,118 @@
use ahash::AHashMap;
use anyhow::Result; use anyhow::Result;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take;
use nom::character::complete::alpha1;
use nom::character::complete::newline;
use nom::combinator::map;
use nom::combinator::map_res;
use nom::multi::fold_many1;
use nom::sequence::preceded;
use nom::sequence::terminated;
use nom::sequence::tuple;
use nom::IResult;
pub fn part1(_input: &[u8]) -> Result<String> { use crate::common::parse_input;
anyhow::bail!("not implemented")
type Input<'a> = AHashMap<&'a [u8], Monkey<'a>>;
#[derive(Clone, Copy)]
enum Operation {
Mul,
Div,
Add,
Sub,
}
impl Operation {
pub fn apply(self, first: i64, second: i64) -> i64 {
match self {
Operation::Mul => first * second,
Operation::Div => first / second,
Operation::Add => first + second,
Operation::Sub => first - second,
}
}
}
impl TryFrom<u8> for Operation {
type Error = anyhow::Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value {
b'*' => Operation::Mul,
b'/' => Operation::Div,
b'+' => Operation::Add,
b'-' => Operation::Sub,
other => anyhow::bail!("Invalid operation: {other}"),
})
}
}
enum Monkey<'a> {
Operation(&'a [u8], &'a [u8], Operation),
Literal(i64),
}
fn parse_monkeys(input: &[u8]) -> IResult<&[u8], Input> {
let parse_monkey = terminated(
tuple((
alpha1,
preceded(
tag(": "),
alt((
map(nom::character::complete::i64, Monkey::Literal),
map(
tuple((
terminated(alpha1, tag(" ")),
map_res(take(1usize), |v: &[u8]| Operation::try_from(v[0])),
preceded(tag(" "), alpha1),
)),
|(first, operation, second)| Monkey::Operation(first, second, operation),
),
)),
),
)),
newline,
);
fold_many1(parse_monkey, AHashMap::new, |mut map, (name, monkey)| {
map.insert(name, monkey);
map
})(input)
}
fn evaluate(monkeys: &Input, start: &[u8]) -> i64 {
match &monkeys[start] {
Monkey::Operation(first, second, op) => {
let first = evaluate(monkeys, first);
let second = evaluate(monkeys, second);
op.apply(first, second)
}
Monkey::Literal(value) => *value,
}
}
pub fn part1(input: &[u8]) -> Result<String> {
let monkeys = parse_input(input, parse_monkeys)?;
Ok(evaluate(&monkeys, b"root").to_string())
} }
pub fn part2(_input: &[u8]) -> Result<String> { pub fn part2(_input: &[u8]) -> Result<String> {
anyhow::bail!("not implemented") anyhow::bail!("not implemented")
} }
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &[u8] = include_bytes!("samples/21.txt");
#[test]
fn sample_part1() {
assert_eq!(part1(SAMPLE).unwrap(), "152");
}
}

15
2022/src/samples/21.txt Normal file
View File

@@ -0,0 +1,15 @@
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32