Skip to content

Instantly share code, notes, and snippets.

@tomjemmett
Created December 3, 2024 07:56
Show Gist options
  • Select an option

  • Save tomjemmett/c6f3fd9521b5fc4f67733f2520ba5568 to your computer and use it in GitHub Desktop.

Select an option

Save tomjemmett/c6f3fd9521b5fc4f67733f2520ba5568 to your computer and use it in GitHub Desktop.
library(stringr)
library(readr)
i <- read_file("inputs/day03.txt")
f <- function(i) {
str_extract_all(i, "(?<=mul\\()(\\d+),(\\d+)(?=\\))")[[1]] |>
str_split(",") |>
sapply(as.numeric) |>
apply(2, prod) |>
sum()
}
# part 1
f(i)
x <- str_split(
i,
"(?<=do(n't)?\\(\\))"
)[[1]]
# part 2
sapply(x[c(TRUE, !str_ends(x, "don't\\(\\)")[1:70])], f) |> sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment