|
| 1 | +use nom::{ |
| 2 | + branch::alt, |
| 3 | + bytes::complete::tag, |
| 4 | + character::complete::{anychar, char, digit1}, |
| 5 | + combinator::{map, map_res}, |
| 6 | + multi::many0, |
| 7 | + sequence::{delimited, preceded, separated_pair}, |
| 8 | + IResult, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::solution::{AocError, Solution}; |
| 12 | + |
| 13 | +#[derive(PartialEq, Debug)] |
| 14 | +enum Instruction { |
| 15 | + Mul(u32, u32), |
| 16 | + Enable, |
| 17 | + Disable, |
| 18 | +} |
| 19 | + |
| 20 | +fn parse_mul(input: &str) -> IResult<&str, (u32, u32)> { |
| 21 | + preceded( |
| 22 | + tag("mul"), |
| 23 | + delimited( |
| 24 | + char('('), |
| 25 | + separated_pair( |
| 26 | + map_res(digit1, |i: &str| i.parse::<u32>()), |
| 27 | + char(','), |
| 28 | + map_res(digit1, |i: &str| i.parse::<u32>()), |
| 29 | + ), |
| 30 | + char(')'), |
| 31 | + ), |
| 32 | + )(input) |
| 33 | +} |
| 34 | + |
| 35 | +fn parse_other(input: &str) -> IResult<&str, ()> { |
| 36 | + let (unhandled, _ignored) = anychar(input)?; |
| 37 | + Ok((unhandled, ())) |
| 38 | +} |
| 39 | + |
| 40 | +fn parse_instruction(input: &str) -> IResult<&str, Option<Instruction>> { |
| 41 | + alt(( |
| 42 | + map(tag("do()"), |_| Some(Instruction::Enable)), |
| 43 | + map(tag("don't()"), |_| Some(Instruction::Disable)), |
| 44 | + map(parse_mul, |(a, b)| Some(Instruction::Mul(a, b))), |
| 45 | + map(parse_other, |_| None), |
| 46 | + ))(input) |
| 47 | +} |
| 48 | + |
| 49 | +fn parse(input: &str) -> Result<Vec<Instruction>, AocError> { |
| 50 | + let (_unhandled, parsed) = |
| 51 | + many0(parse_instruction)(input).map_err(|err| AocError::parse(input, err))?; |
| 52 | + |
| 53 | + let instructions = parsed.into_iter().flatten().collect(); |
| 54 | + |
| 55 | + Ok(instructions) |
| 56 | +} |
| 57 | + |
| 58 | +pub struct Day03; |
| 59 | +impl Solution for Day03 { |
| 60 | + type A = u32; |
| 61 | + type B = u32; |
| 62 | + |
| 63 | + fn default_input(&self) -> &'static str { |
| 64 | + include_str!("../../../inputs/2024/day03.txt") |
| 65 | + } |
| 66 | + |
| 67 | + fn part_1(&self, input: &str) -> Result<u32, AocError> { |
| 68 | + let sum = parse(input)? |
| 69 | + .iter() |
| 70 | + .map(|instruction| match instruction { |
| 71 | + Instruction::Mul(a, b) => a * b, |
| 72 | + _ => 0, |
| 73 | + }) |
| 74 | + .sum(); |
| 75 | + |
| 76 | + Ok(sum) |
| 77 | + } |
| 78 | + |
| 79 | + fn part_2(&self, input: &str) -> Result<u32, AocError> { |
| 80 | + let mut enabled = true; |
| 81 | + let sum = parse(input)? |
| 82 | + .iter() |
| 83 | + .map(|instruction| match instruction { |
| 84 | + Instruction::Mul(a, b) if enabled => a * b, |
| 85 | + Instruction::Enable => { |
| 86 | + enabled = true; |
| 87 | + 0 |
| 88 | + } |
| 89 | + Instruction::Disable => { |
| 90 | + enabled = false; |
| 91 | + 0 |
| 92 | + } |
| 93 | + _ => 0, |
| 94 | + }) |
| 95 | + .sum(); |
| 96 | + |
| 97 | + Ok(sum) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn it_solves_part1_example() { |
| 107 | + assert_eq!( |
| 108 | + Day03.part_1( |
| 109 | + "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" |
| 110 | + ), |
| 111 | + Ok(161) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn it_solves_part2_example() { |
| 117 | + assert_eq!( |
| 118 | + Day03.part_2( |
| 119 | + "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" |
| 120 | + ), |
| 121 | + Ok(48) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn it_parses_input() { |
| 127 | + assert_eq!( |
| 128 | + parse("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"), |
| 129 | + Ok(vec![ |
| 130 | + Instruction::Mul(2, 4), |
| 131 | + Instruction::Disable, |
| 132 | + Instruction::Mul(5, 5), |
| 133 | + Instruction::Mul(11, 8), |
| 134 | + Instruction::Enable, |
| 135 | + Instruction::Mul(8, 5), |
| 136 | + ]) |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments