use std::{
fs::File,
io::{BufReader, Read},
path::Path,
};
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::i32,
sequence::{delimited, separated_pair},
IResult,
};
#[derive(Debug)]
enum Instruction {
Do,
Dont,
Mul { left: i32, right: i32 },
}
fn parse_int32_pair(input: &str) -> IResult<&str, (i32, i32)> {
separated_pair(i32, tag(","), i32)(input)
}
fn parse_do(input: &str) -> IResult<&str, Instruction> {
let (rem, _) = tag("do()")(input)?;
Ok((rem, Instruction::Do))
}
fn parse_dont(input: &str) -> IResult<&str, Instruction> {
let (rem, _) = tag("don't()")(input)?;
Ok((rem, Instruction::Dont))
}
fn parse_mul(input: &str) -> IResult<&str, Instruction> {
let (rem, (left, right)) = delimited(tag("mul("), parse_int32_pair, tag(")"))(input)?;
Ok((rem, Instruction::Mul { left, right }))
}
fn parse(input: &str) -> IResult<&str, Instruction> {
let (rem, ins) = alt((parse_do, parse_dont, parse_mul))(input)?;
Ok((rem, ins))
}
pub(super) fn part_two(input: &Path) -> anyhow::Result<i32> {
let mut reader = BufReader::new(File::open(input)?);
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
let mut answer = 0;
let mut mul_enabled = true;
loop {
if buf.len() < 5 {
break;
}
let (rem, ins) = match parse(&buf) {
Ok((rem, ins)) => (rem, ins),
Err(_) => {
buf.replace_range(0..1, "");
continue;
}
};
buf = rem.into();
match ins {
Instruction::Do => mul_enabled = true,
Instruction::Dont => mul_enabled = false,
Instruction::Mul { left, right } => {
if mul_enabled {
answer += left * right
}
}
}
}
Ok(answer)
}