Toggle the code
library(aochelpers)
<- readLines("~/GitHub/AdventOfCode/2024/day/3/input") indata
December 13, 2024
Goal: Multiply numbers using mul(X,Y)
function. Except instructions got corrupt and so there are incorrect symbols.
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 produces 161 (24 + 55 + 118 + 85).
Clearly a regex problem.
Example Data
Throw it into a data frame, will automatically separate into rows. Now I can get out the #’s and multiply.
Now to do rowwise multiplication and sum it up
Well.. the example matches. Time to see how jacked up the actual data is.
It’s reading it in as multiple lines… when I don’t think that was the intention. I think that’s okay, when I unlist
it combined it into one vector.
[,1]
[1,] 161289189
Yep. that worked. Coolio
There are two new instructions
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.4.1 (2024-06-14)
os macOS Sonoma 14.6.1
system aarch64, darwin20
ui X11
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz America/Los_Angeles
date 2024-12-13
pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
aochelpers * 0.1.0.9000 2024-12-02 [1] Github (EllaKaye/aochelpers@d4ccd91)
[1] /Users/rdonatello/Library/R/arm64/4.4/library
[2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
──────────────────────────────────────────────────────────────────────────────
---
title: "2024: Day 03 Mull it over"
date: 2024-12-13
categories:
draft: false
---
## Setup
[The original challenge](https://adventofcode.com/2024/day/03)
Goal: Multiply numbers using `mul(X,Y)` function.
Except instructions got corrupt and so there are incorrect symbols.
x`mul(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 produces 161 (2*4 + 5*5 + 11*8 + 8*5).
# Input data
```{r}
library(aochelpers)
indata <- readLines("~/GitHub/AdventOfCode/2024/day/3/input")
```
# TLDR; Solutions
## Part 1 ⭐
```{r}
a <- stringr::str_extract_all(indata, "\\mul\\(\\d+,\\d+\\)") |> unlist()
df <- data.frame(a = a)
df$a1 <- gsub("mul", "", df$a)
df2 <- stringr::str_extract_all(df$a, "\\d+", simplify = TRUE)
df2 <- apply(df2, 2, as.numeric)
df2[,1]%*%df2[,2]
```
## Part 2 ⭐⭐
::: {.callout-danger}
### ❓
:::
# Walkthrough / Explainer
## Part 1
Clearly a regex problem.
:::{.callout-exa icon=true}
**Example Data**
```{r}
exa <- "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
```
:::
```{r}
a <- stringr::str_extract_all(exa, "\\mul\\(\\d+,\\d+\\)") |> unlist()
```
Throw it into a data frame, will automatically separate into rows. Now I can get out the #'s and multiply.
```{r}
df <- data.frame(a = a)
df$a1 <- gsub("mul", "", df$a)
df2 <- stringr::str_extract_all(df$a, "\\d+", simplify = TRUE)
df2 <- apply(df2, 2, as.numeric)
```
Now to do rowwise multiplication and sum it up
```{r}
df2[,1]%*%df2[,2]
```
Well.. the example matches. Time to see how jacked up the actual data is.
```{r}
indata <- readLines("~/GitHub/AdventOfCode/2024/day/3/input")
```
It's reading it in as multiple lines... when I don't think that was the intention. I think that's okay, when I `unlist` it combined it into one vector.
```{r}
a <- stringr::str_extract_all(indata, "\\mul\\(\\d+,\\d+\\)") |> unlist()
df <- data.frame(a = a)
df$a1 <- gsub("mul", "", df$a)
df2 <- stringr::str_extract_all(df$a, "\\d+", simplify = TRUE)
df2 <- apply(df2, 2, as.numeric)
df2[,1]%*%df2[,2]
```
Yep. that worked. Coolio
## Part 2
There are two new instructions
* The do() instruction enables future mul instructions.
- The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.
* The don't() instruction disables future mul instructions.
- mul(5,5) and mul(11,8) instructions are disabled
```{r}
exa2 <- "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
```
##### Session info {.appendix}
<details><summary>Toggle</summary>
```{r}
#| echo: false
sessioninfo::session_info(pkgs = "attached")
```
</details>