Code
<- paste0(readLines("input.txt"), collapse = "") input
“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), 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.
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.
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
Regular expressions are a personal favourite, so using the mix of regmatches
and gregexec
extracts the numbers into 2 rows of a matrix that can be multiplied together.
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 future mul instructions.
- The don’t() instruction disables future mul instructions.
Only the most recent do() or don’t() instruction applies. At the beginning of the program, mul instructions are enabled.
Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
There was no easy way to include multiple look-behinds to check if don’t() doesn’t exist but do() does, and then ignoring mul() in between the calls, so just split on the don’t() and check when the next do() call appears to use the same original regular expression.
split_input <- strsplit(input, "don't()", fixed = TRUE) |> unlist()
input2 <- sub(".*?(do\\(\\)|$)", "", split_input)
input2[1] <- split_input[1]
input2 <- paste(input2, collapse = "")
matches <- regmatches(input2, gregexec("mul\\((\\d+),(\\d+)\\)", input2))[[1L]]
sum(as.numeric(matches[2, ]) * as.numeric(matches[3, ]))
[1] 100189366
An improved version where we just remove anything between “don’t()” and “do()” so we can use the same code as in Part 1.
---
title: "2024 - Day 3: Mull It Over"
format:
html:
code-fold: show
code-tools: true
code-block-bg: true
code-block-border-left: "#AB63BD"
---
> "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?"
## Set-Up
```{r setup}
input <- paste0(readLines("input.txt"), collapse = "")
```
## Part 1
> 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), 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.
>
> 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.
>
> Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
Regular expressions are a personal favourite, so using the mix of `regmatches` and `gregexec` extracts the numbers into 2 rows of a matrix that can be multiplied together.
```{r part_1}
matches <- regmatches(input, gregexec("mul\\((\\d+),(\\d+)\\)", input))[[1L]]
sum(as.numeric(matches[2, ]) * as.numeric(matches[3, ]))
```
## Part 2
> 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 future mul instructions.
> - The don't() instruction disables future mul instructions.
>
> Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.
>
> Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
There was no easy way to include multiple look-behinds to check if don't() doesn't exist but do() does, and then ignoring mul() in between the calls, so just split on the don't() and check when the next do() call appears to use the same original regular expression.
```{r part_2}
split_input <- strsplit(input, "don't()", fixed = TRUE) |> unlist()
input2 <- sub(".*?(do\\(\\)|$)", "", split_input)
input2[1] <- split_input[1]
input2 <- paste(input2, collapse = "")
matches <- regmatches(input2, gregexec("mul\\((\\d+),(\\d+)\\)", input2))[[1L]]
sum(as.numeric(matches[2, ]) * as.numeric(matches[3, ]))
```
An improved version where we just remove anything between "don't()" and "do()" so we can use the same code as in Part 1.
```{r part_2_v2}
input3 <- gsub("don't\\(\\).*?(do\\(\\))", "\\1", input)
matches <- regmatches(input3, gregexec("mul\\((\\d+),(\\d+)\\)", input3))[[1L]]
sum(as.numeric(matches[2, ]) * as.numeric(matches[3, ]))
```