year2024/
day03.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use utils::prelude::*;

/// Extracting instructions from corrupted input.
#[derive(Clone, Debug)]
pub struct Day03 {
    part1: u32,
    part2: u32,
}

enum Instruction {
    Mul(u32, u32),
    Do,
    Dont,
}

impl Day03 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        let matches = parser::parse_tree!(
            ("mul(", a @ parser::u32(), ",", b @ parser::u32(), ")") => Instruction::Mul(a, b),
            ("don't()") => Instruction::Dont,
            ("do()") => Instruction::Do,
        )
        .matches_iterator(input);

        let (mut part1, mut part2) = (0, 0);
        let mut enabled = true;
        for instruction in matches {
            match instruction {
                Instruction::Mul(a, b) => {
                    part1 += a * b;
                    part2 += if enabled { a * b } else { 0 };
                }
                Instruction::Do => enabled = true,
                Instruction::Dont => enabled = false,
            }
        }

        Ok(Self { part1, part2 })
    }

    #[must_use]
    pub fn part1(&self) -> u32 {
        self.part1
    }

    #[must_use]
    pub fn part2(&self) -> u32 {
        self.part2
    }
}

examples!(Day03 -> (u32, u32) [
    {input: "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))", part1: 161},
    {input: "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))", part2: 48},
]);