Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
- Advent of Code 2024 Day 21
- Advent of Code 2024 Day 22
- Advent of Code 2024 Day 23
- Advent of Code 2024 Day 24
- Advent of Code 2024 Day 25
-
Older post
-
Newer post
Advent of Code 2024 Day 3
Day 3: Mull It Over
https://adventofcode.com/2024/day/3
Today’s location to search for the chief historian is familiar (Again! Makes sense, you’ve been to many interesting places.). You remember visiting it.
The elves in charge of the place ask you to help with their computer. Something is wrong with the computer’s memory.
Ah, this feels like christmas! Going to places you haven’t been in a while and people asking you to fix their computer. Next thing you know, you’ll be reinstalling the elves’ printer drivers.
That’s your input, the memory.
An example input looks like this:
The goal of the program is multiplying some numbers.
It does that with instructions like mul(X,Y)
, where X
and Y
are each 1-3 digit numbers.
It then multiplies X
by Y
.
Because the memory has been corrupted, there are many invalid characters that should be ignored.
Only valid mul(X,Y)
instructions should be considered.
Part 1
If you know me, you might know I have a strong irrational hatred of regular expressions.
But even I have to admit, they’ll do well today.
HOWEVER
I coded 2 solutions again, one using regex, and a (much faster, mind you!) custom parsing thing-a-majig (solution, the word I was looking for was solution).
The question asks for the sum of all valid multiplication instructions.
Option 1: regex
Option 2: custom logic
Part 2
There are 2 additional instructions in the corrupted memory to pay attention to.
do()
enablesmul
instructionsdon't()
disablesmul
instructions
The most recent one of these instructions applies.
If you come across a mul
, but the previous conditional was a don't()
, ignore that mul
.
In other words, those instructions flip a boolean from true
to false
,
and you should only do mul
if that boolean is true
.
mul
instructions start as enabled. (the boolean starts as true
)