Advent of Code 2024 - Day 3

Author
Published

December 14, 2024

You really shouldn’t be reading this before you’ve had a go yourself!

R

Let’s try with test data:

library("tidyverse")

data_test <- "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"

extracted_operations <- str_extract_all(data_test, "mul[(]\\d{1,3},\\d{1,3}[)]")[[1]] %>% 
  paste(collapse = "") %>% 
  str_extract_all(.,"\\d{1,3},\\d{1,3}") %>% 
  map(~str_replace(., ",", "*")) %>% 
  .[[1]] 


eval(parse(text = paste(extracted_operations, collapse = "+")))
[1] 161

And the real data

data_real <- paste(read_lines(quarto_here("input.txt")), collapse = "")

extracted_operations <- str_extract_all(data_real, "mul[(]\\d{1,3},\\d{1,3}[)]")[[1]] %>% 
  paste(collapse = "") %>% 
  str_extract_all(.,"\\d{1,3},\\d{1,3}") %>% 
  map(~str_replace(., ",", "*")) %>% 
  .[[1]] 

eval(parse(text = paste(extracted_operations, collapse = "+")))
[1] 183380722

Reuse

Citation

BibTeX citation:
@online{hadley2024,
  author = {Hadley, Charlie},
  title = {Advent of {Code} 2024 - {Day} 3},
  date = {2024-12-14},
  url = {https://visibledata.co.uk/posts/2024-12-14_advent-of-code-2024-03/},
  langid = {en}
}
For attribution, please cite this work as:
Hadley, Charlie. 2024. “Advent of Code 2024 - Day 3.” December 14, 2024. https://visibledata.co.uk/posts/2024-12-14_advent-of-code-2024-03/.