Advent of Code: 2023 Day 1

R
Advent of Code 2023
Author
Published

December 11, 2023

Intro

Day 1 of AOC 2023

library(aocr)
library(tidyverse)
library(printr)

d_init <- aoc_get_data_as_tibble(2023, 1)

Part 1

head(d_init)
col_1
five3onelxjninenine45
six9mnfjmtsf2kfmznkxntninesevenrpmfjfpgsk
9vkrmbpnine5two5cbktwo6
one1bdr6
ksvctznmffourtwovbb9four5five
6nfhcklxlkg9jbqmqrrxmhn9two6

Use regex to extract the first and last digit.

d <-
  d_init |>
  mutate(
    first_digit = gsub("\\D*(\\d).*", "\\1", col_1),
    last_digit = gsub(".*?(\\d)\\D*$", "\\1", col_1),
    combo = as.numeric(paste0(first_digit, last_digit))
  )

head(d)
col_1 first_digit last_digit combo
five3onelxjninenine45 3 5 35
six9mnfjmtsf2kfmznkxntninesevenrpmfjfpgsk 9 2 92
9vkrmbpnine5two5cbktwo6 9 6 96
one1bdr6 1 6 16
ksvctznmffourtwovbb9four5five 9 5 95
6nfhcklxlkg9jbqmqrrxmhn9two6 6 6 66
sum(d$combo)
[1] 55123

Part 2

Had to get help from twitter on this one :)

num <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")

d2 <- 
  d_init |>
  extract(
    col = col_1,
    into = "first_init",
    regex = "(\\d|one|two|three|four|five|six|seven|eight|nine)",
    remove = FALSE
  ) |>
  extract(
    col = col_1,
    into = "last_init",
    regex = ".*(\\d|one|two|three|four|five|six|seven|eight|nine)",
    remove = FALSE
  ) |>
  mutate(
    first = coalesce(
      as.numeric(first_init), 
      match(first_init, num)
    ),
    last = coalesce(
      as.numeric(last_init),
      match(last_init, num)
    ),
    combo = as.numeric(paste0(first, last))
  ) %>% 
  select(
    col_1,
    first_init,
    last_init,
    first,
    last,
    combo
  )

head(d2)
col_1 first_init last_init first last combo
five3onelxjninenine45 five 5 5 5 55
six9mnfjmtsf2kfmznkxntninesevenrpmfjfpgsk six seven 6 7 67
9vkrmbpnine5two5cbktwo6 9 6 9 6 96
one1bdr6 one 6 1 6 16
ksvctznmffourtwovbb9four5five four five 4 5 45
6nfhcklxlkg9jbqmqrrxmhn9two6 6 6 6 6 66
sum(d2$combo)
[1] 55260

Sources

Session info

Click for session info

─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RTerm
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       America/Chicago
 date     2023-12-17
 pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 ! package     * version    date (UTC) lib source
 P aocr        * 0.0.0.9000 2023-12-09 [?] Github (koderkow/aocr@24fd057)
 P backports     1.4.1      2021-12-13 [?] RSPM (R 4.3.0)
 P bit           4.0.5      2022-11-15 [?] CRAN (R 4.3.1)
 P bit64         4.0.5      2020-08-30 [?] CRAN (R 4.3.1)
 P checkmate     2.3.1      2023-12-04 [?] CRAN (R 4.3.2)
 P cli           3.6.1      2023-03-23 [?] RSPM (R 4.3.0)
 P clipr         0.8.0      2022-02-22 [?] CRAN (R 4.3.1)
 P colorspace    2.1-0      2023-01-23 [?] CRAN (R 4.3.1)
 P crayon        1.5.2      2022-09-29 [?] RSPM (R 4.3.0)
 P curl          5.0.2      2023-08-14 [?] CRAN (R 4.3.1)
 P desc          1.4.2      2022-09-08 [?] RSPM (R 4.3.0)
 P details     * 0.3.0      2022-03-27 [?] RSPM (R 4.3.0)
 P digest        0.6.33     2023-07-07 [?] RSPM (R 4.3.0)
 P dplyr       * 1.1.2      2023-04-20 [?] CRAN (R 4.3.1)
 P evaluate      0.21       2023-05-05 [?] CRAN (R 4.3.1)
 P fansi         1.0.4      2023-01-22 [?] CRAN (R 4.3.1)
 P fastmap       1.1.1      2023-02-24 [?] CRAN (R 4.3.1)
 P forcats     * 1.0.0      2023-01-29 [?] CRAN (R 4.3.1)
 P generics      0.1.3      2022-07-05 [?] CRAN (R 4.3.1)
 P ggplot2     * 3.4.3      2023-08-14 [?] CRAN (R 4.3.1)
 P glue          1.6.2      2022-02-24 [?] RSPM (R 4.3.0)
 P gtable        0.3.4      2023-08-21 [?] CRAN (R 4.3.1)
 P hms           1.1.3      2023-03-21 [?] CRAN (R 4.3.1)
 P htmltools     0.5.6      2023-08-10 [?] CRAN (R 4.3.1)
 P httr          1.4.7      2023-08-15 [?] CRAN (R 4.3.1)
 P httr2         0.2.3      2023-05-08 [?] CRAN (R 4.3.1)
 P jsonlite      1.8.7      2023-06-29 [?] CRAN (R 4.3.1)
 P knitr         1.43       2023-05-25 [?] CRAN (R 4.3.1)
 P lifecycle     1.0.3      2022-10-07 [?] RSPM (R 4.3.0)
 P lubridate   * 1.9.2      2023-02-10 [?] CRAN (R 4.3.1)
 P magrittr      2.0.3      2022-03-30 [?] RSPM (R 4.3.0)
 P munsell       0.5.0      2018-06-12 [?] CRAN (R 4.3.1)
 P pillar        1.9.0      2023-03-22 [?] CRAN (R 4.3.1)
 P pkgconfig     2.0.3      2019-09-22 [?] CRAN (R 4.3.1)
 P png           0.1-8      2022-11-29 [?] RSPM (R 4.3.0)
 P printr      * 0.3        2023-03-08 [?] CRAN (R 4.3.2)
 P purrr       * 1.0.2      2023-08-10 [?] RSPM (R 4.3.0)
 P R6            2.5.1      2021-08-19 [?] RSPM (R 4.3.0)
 P rappdirs      0.3.3      2021-01-31 [?] CRAN (R 4.3.1)
 P readr       * 2.1.4      2023-02-10 [?] CRAN (R 4.3.1)
   renv          1.0.3      2023-09-19 [1] CRAN (R 4.3.2)
 P rlang         1.1.1      2023-04-28 [?] RSPM (R 4.3.0)
 P rmarkdown     2.24       2023-08-14 [?] CRAN (R 4.3.1)
 P rprojroot     2.0.3      2022-04-02 [?] RSPM (R 4.3.0)
 P rstudioapi    0.15.0     2023-07-07 [?] CRAN (R 4.3.1)
 P scales        1.2.1      2022-08-20 [?] CRAN (R 4.3.1)
 P sessioninfo   1.2.2      2021-12-06 [?] RSPM (R 4.3.0)
 P stringi       1.7.12     2023-01-11 [?] RSPM (R 4.3.0)
 P stringr     * 1.5.0      2022-12-02 [?] RSPM (R 4.3.0)
 P tibble      * 3.2.1      2023-03-20 [?] CRAN (R 4.3.1)
 P tidyr       * 1.3.0      2023-01-24 [?] CRAN (R 4.3.1)
 P tidyselect    1.2.0      2022-10-10 [?] CRAN (R 4.3.1)
 P tidyverse   * 2.0.0      2023-02-22 [?] CRAN (R 4.3.1)
 P timechange    0.2.0      2023-01-11 [?] CRAN (R 4.3.1)
 P tzdb          0.4.0      2023-05-12 [?] CRAN (R 4.3.1)
 P utf8          1.2.3      2023-01-31 [?] CRAN (R 4.3.1)
 P vctrs         0.6.3      2023-06-14 [?] CRAN (R 4.3.1)
 P vroom         1.6.3      2023-04-28 [?] CRAN (R 4.3.1)
 P withr         2.5.0      2022-03-03 [?] CRAN (R 4.3.1)
 P xfun          0.40       2023-08-09 [?] RSPM (R 4.3.0)
 P xml2          1.3.5      2023-07-06 [?] RSPM (R 4.3.0)
 P yaml          2.3.7      2023-01-23 [?] RSPM (R 4.3.0)

 [1] C:/Users/kypap/Documents/projects/quarto-site/renv/library/R-4.3/x86_64-w64-mingw32
 [2] C:/Users/kypap/AppData/Local/R/cache/R/renv/sandbox/R-4.3/x86_64-w64-mingw32/bd3f13aa

 P ── Loaded and on-disk path mismatch.

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