2024: Day 03 Mull it over

Published

December 13, 2024

Setup

The original challenge

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).

Input data

Toggle the code
library(aochelpers)
indata <- readLines("~/GitHub/AdventOfCode/2024/day/3/input") 

TLDR; Solutions

Part 1 ⭐

Toggle the code
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]
          [,1]
[1,] 161289189

Part 2 ⭐⭐

Walkthrough / Explainer

Part 1

Clearly a regex problem.

Example Data

Toggle the code
exa <- "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
Toggle the code
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.

Toggle the code
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

Toggle the code
df2[,1]%*%df2[,2]
     [,1]
[1,]  161

Well.. the example matches. Time to see how jacked up the actual data is.

Toggle the code
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.

Toggle the code
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]
          [,1]
[1,] 161289189

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
Toggle the code
exa2 <- "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"

Session info

Toggle
─ 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

──────────────────────────────────────────────────────────────────────────────