Advent of Code 2023
  • Solutions

Day 1 ⋆

Published

December 1, 2023

Puzzle

https://adventofcode.com/2023/day/1

Solution

Preprocessing

helper1_20231201 <- function (df) {
  df2 <- df %>% 
    mutate(digit_locs = X1 %>% 
             stringr::str_locate_all("[1-9]") %>% 
             map(~.x[c(1, nrow(.x)), "start"])
    ) %>% 
    rowwise() %>% 
    mutate(X2 = as.numeric(paste0(
      stringr::str_sub(X1, start = digit_locs[1], end = digit_locs[1]),
      stringr::str_sub(X1, start = digit_locs[2], end = digit_locs[2])
    ))) %>% 
    ungroup()
  return(df2)
}
df1 <- readr::read_delim("input.txt", 
                         delim = "|", 
                         col_names = FALSE)

Part 1

answer1 <- df1 %>% 
  helper1_20231201() %>% 
  summarise(sum(X2)) %>% 
  pull()
print(answer1)
[1] 54390

Part 2

I haven’t solved this part yet!