--- Day 3: Mull It Over ---
"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. The Historians head out to take a look.
The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
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!
It seems like the goal of the program is just to multiply some numbers. It does that with instructions like
mul(X,Y)
, whereX
andY
are each 1-3 digit numbers. For instance,mul(44,46)
multiplies44
by46
to get a result of2024
. Similarly,mul(123,4)
would multiply123
by4
.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 likemul(4*
,mul(6,9!
,?(12,34)
, ormul ( 2 , 4 )
do nothing.For example, consider the following section of corrupted memory:
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
Only the four highlighted sections are real
mul
instructions. Adding up the result of each instruction produces161
(2*4 + 5*5 + 11*8 + 8*5
).Scan the corrupted memory for uncorrupted
mul
instructions. What do you get if you add up all of the results of the multiplications?
show i: "c" $ read1 `3.txt "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
The input contains some "corrupted" data. We need to extract all of the instructions like mul(X,Y) where X and Y are 1-3 digit numbers.
(Incidentally, one input for this challenge has been verified as containing 4 digit numbers, and the solution doesn't require ignoring them, so the digit length requirement can be ignored)
We could do this by utilising regular expressions, but Q doesn't have them built in.muls: i ss "mul("; show m: muls _ i "mul(2,4)%&mul[3,7]!@^do_not_" "mul(5,5)+" "mul(32,64]then(" "mul(11,8)" "mul(8,5))"Now, for each of the groups, we can drop the first 4 elements, "mul(", split on ")", and take the first element
{first ")" vs 4_x} each m "2,4" "5,5" "32,64]then(" "11,8" "8,5"Now, split on "," and attempt to cast to integer.
{"J" $ "," vs first ")" vs 4_x} each m 2 4 5 5 32 11 8 8 5We can fill nulls with 0, before taking the product as 0*anything=0. Also, check that there are exactly two elements, as mul(X) or mul(X,Y,Z,...) shouldn't be included.
show nums: {(2=count n) * prd n: 0 ^ "J" $ "," vs first ")" vs 4_x} each m 8 25 0 88 40Now calculate the sum.
sum nums 161
--- Part Two ---
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.
There are two new instructions you'll need to handle:
- The
do()
instruction enables futuremul
instructions.- The
don't()
instruction disables futuremul
instructions.Only the most recent
do()
ordon't()
instruction applies. At the beginning of the program,mul
instructions are enabled.For example:
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
This corrupted memory is similar to the example from before, but this time the
mul(5,5)
andmul(11,8)
instructions are disabled because there is adon't()
instruction before them. The othermul
instructions function normally, including the one at the end that gets re-enabled by ado()
instruction.This time, the sum of the results is
48
(2*4 + 8*5
).Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
i: "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"We are told this:
do()
instruction enables future mul
instructions.don't()
instruction disables future mul
instructions.show s:i ss "do()"; s s bin muls ,59 0N 0N 0N 0N 59
show s:i ss "don't()"; s s bin muls ,20 0N 20 20 20 20Now, we can check if do >= don't.
show include: (>=) . muls {s:i ss y; s s bin x}/: ("do()"; "don't()") 10001bThis shows we should only take the first and last products, as is seen in the example.
sum include * nums 48Alternatively, there is a method using splitting that is possibly simpler. First of all, extract the code for the first part into a function.
f: {sum {(2=count n) * prd n:0^"J" $ "," vs first ")" vs 4_x} each (x ss "mul(") _ x}Now, to find the valid regions, we can split first on "do()", and for each of those, split on "don't()" and take the first item. This will find groups between "do()" and "don't()", and the first region, before either. Then use the function from the first part, and sum all of the results.
sum {f first "don't()" vs x} each "do()" vs i 48
i: "c" $ read1 `:i/3.txt
muls: i ss "mul("; m: muls _ i
/ part 1:
sum nums: {(2=count n) * prd n: 0 ^ "J" $ "," vs first ")" vs 4_x} each m
/ part 2:
include: (>=) . muls {s:i ss y; s s bin x}/: ("do()"; "don't()")
sum include * nums