Skip to content

Commit 6596934

Browse files
author
Rudy Puig
committed
chore: implement day 3 solution
1 parent 3ce835c commit 6596934

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

docs/2024/day-03.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Day 1: Mull It Over
2+
3+
## Part One
4+
5+
--- Day 3: Mull It Over ---
6+
7+
"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the [North Pole Toboggan Rental Shop](https://adventofcode.com/2020/day/2). The Historians head out to take a look.
8+
9+
The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
10+
11+
The computer appears to be trying to run a program, but its memory (your puzzle input) is _corrupted_. All of the instructions have been jumbled up!
12+
13+
It seems like the goal of the program is just to _multiply some numbers_. It does that with instructions like `mul(X,Y)`, where `X` and `Y` are each 1-3 digit numbers. For instance, `mul(44,46)` multiplies `44` by `46` to get a result of `2024`. Similarly, `mul(123,4)` would multiply `123` by `4`.
14+
15+
However, because the program's memory has been corrupted, there are also many invalid characters that should be _ignored_, even if they look like part of a `mul` instruction. Sequences like `mul(4*`, `mul(6,9!`, `?(12,34)`, or `mul ( 2 , 4 )` do _nothing_.
16+
17+
For example, consider the following section of corrupted memory:
18+
19+
```
20+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
21+
```
22+
23+
Only the four highlighted sections are real `mul` instructions. Adding up the result of each instruction produces `_161_` (`2*4 + 5*5 + 11*8 + 8*5`).
24+
25+
Scan the corrupted memory for uncorrupted `mul` instructions. _What do you get if you add up all of the results of the multiplications?_
26+
27+
## Part Two
28+
29+
As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result.
30+
31+
There are two new instructions you'll need to handle:
32+
33+
- The `do()` instruction _enables_ future `mul` instructions.
34+
- The `don't()` instruction _disables_ future `mul` instructions.
35+
36+
Only the _most recent_ `do()` or `don't()` instruction applies. At the beginning of the program, `mul` instructions are _enabled_.
37+
38+
For example:
39+
40+
```
41+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
42+
```
43+
44+
This corrupted memory is similar to the example from before, but this time the `mul(5,5)` and `mul(11,8)` instructions are _disabled_ because there is a `don't()` instruction before them. The other `mul` instructions function normally, including the one at the end that gets re-_enabled_ by a `do()` instruction.
45+
46+
This time, the sum of the results is `48` (`2*4 + 8*5`).
47+
48+
Handle the new instructions; _what do you get if you add up all of the results of just the enabled multiplications?_

src/aoc/2024/day_03.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from __future__ import annotations
2+
import re
3+
4+
from functools import reduce
5+
from operator import mul
6+
from typing import Iterable, Iterator, List
7+
8+
9+
RE_INSTRUCTION_P1 = re.compile(r"(?P<value>mul\(\d+,\d+\))+?", re.I)
10+
RE_INSTRUCTION_P2 = re.compile(r"(?P<value>(mul\(\d+,\d+\)|do(n't)?\(\)))+?", re.I)
11+
12+
13+
def answer_1(input: List[str]) -> int:
14+
instructions = extract_instructions(input, RE_INSTRUCTION_P1)
15+
multiplications = extract_multiplications(instructions)
16+
return sum(multiplications)
17+
18+
19+
def answer_2(input: List[str]) -> int:
20+
instructions = extract_instructions(input, RE_INSTRUCTION_P2)
21+
multiplications = extract_multiplications(instructions)
22+
return sum(multiplications)
23+
24+
25+
def extract_instructions(input: List[str], search_pattern: re.Pattern, /) -> Iterator[re.Match[str]]:
26+
instructions = search_pattern.finditer("".join(input))
27+
yield from instructions
28+
29+
30+
def extract_multiplications(input: Iterator[re.Match[str]], /) -> Iterator[int]:
31+
instruction_enabled = True
32+
for instruction in input:
33+
instruction_value = instruction.group("value")
34+
if instruction_value.startswith("don't("):
35+
instruction_enabled = False
36+
elif instruction_value.startswith("do("):
37+
instruction_enabled = True
38+
elif instruction_value.startswith("mul("):
39+
instruction_result = reduce(mul, extract_operands(instruction_value)) if instruction_enabled else 0
40+
yield instruction_result
41+
42+
43+
def extract_operands(input: str) -> Iterable[int]:
44+
operands = map(lambda s: int(re.sub(r"\D+", "", s)), input.split(","))
45+
return operands

tests/fixtures/2024/03-example.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
2+
--EXPECT--
3+
161
4+
--EXPECT--
5+
48

0 commit comments

Comments
 (0)