Advent of Code 2023, Day 1

advent of code
Author

Michael Luu

Published

December 2, 2023

library(tidyverse)
library(here)

It’s the season for Advent of Code 2023. Advent of code is an ‘advent calendar’ of small programming puzzles that begins on 12/1 of every year and last until Christmas 12/25. The following is my solution of Day 1, Part 1 and Part 2 of the 2023 puzzle solved using R.

We begin by loading the puzzle input.

data <- read_lines(
  here('posts', 'aoc-2023-d1', 'puzzle-input.txt')
)

data <- as_tibble(data)

Part 1

The first part of the puzzle is fairly straight forward. We begin by using the str_remove_all function from the stringr package to remove all of the characters (and leave only the numeric) from the value column. Next we use str_extract to extract the first and last digit using regex, and paste0 to paste the numbers together to get the ‘calibration’ value. Finally we sum the cal column to get the solution.

res <- data |> 
  mutate(
    digits = str_remove_all(value, '[:alpha:]'),
    first = str_extract(digits, '^[:digit:]'),
    last = str_extract(digits, '[:digit:]$'),
    cal = as.numeric(paste0(first, last))
  ) 

res
# A tibble: 1,000 × 5
   value                                 digits first last    cal
   <chr>                                 <chr>  <chr> <chr> <dbl>
 1 9vxfg                                 9      9     9        99
 2 19qdlpmdrxone7sevennine               197    1     7        17
 3 1dzntwofour9nineffck                  19     1     9        19
 4 7bx8hpldgzqjheight                    78     7     8        78
 5 joneseven2sseven64chvczzn             264    2     4        24
 6 seven82683                            82683  8     3        83
 7 7onefour1eighttwo5three               715    7     5        75
 8 8lmsk871eight7                        88717  8     7        87
 9 ninefivefive2nine5ntvscdfdsmvqgcbxxxt 25     2     5        25
10 onepx6hbgdssfivexs                    6      6     6        66
# ℹ 990 more rows
res |> 
  summarise(results = sum(cal))

Part 2

The second part of the puzzle was extremely tricky. There are a number of special cases where the words are overlapping. My messy solution was to ‘fix’ those overlapping words such as ‘twone’ to ‘twoone’, using the str_replace_all function, then using additional str_replace_all to convert the words to numeric. Finally we would use a similar solution as above in extracting the first and last digit.

convert_char_to_numeric <- \(x) {
  
  # special cases with overlapping characters
  x <- str_replace_all(x, 'twone', 'twoone') |> 
    str_replace_all('oneight', 'oneeight') |> 
    str_replace_all('sevenine', 'sevennine') |> 
    str_replace_all('threeight', 'threeeight') |> 
    str_replace_all('fiveight', 'fiveeight') |> 
    str_replace_all('eightwo', 'eighttwo') |> 
    str_replace_all('eighthree', 'eightthree') |> 
    str_replace_all('nineight', 'nineeight')
  
  x <- str_replace_all(x, 'one', '1') |>
    str_replace_all('two', '2') |>
    str_replace_all('three', '3') |>
    str_replace_all('four', '4') |>
    str_replace_all('five', '5') |>
    str_replace_all('six', '6') |>
    str_replace_all('seven', '7') |>
    str_replace_all('eight', '8') |>
    str_replace_all('nine', '9')
  
}

res <- data |> 
  mutate(
    value2 = convert_char_to_numeric(value),
    digits = str_remove_all(value2, '[:alpha:]'),
    first = str_extract(digits, '^[:digit:]'),
    last = str_extract(digits, '[:digit:]$'),
    cal = as.numeric(paste0(first, last))
  ) 

res
# A tibble: 1,000 × 6
   value                                 value2         digits first last    cal
   <chr>                                 <chr>          <chr>  <chr> <chr> <dbl>
 1 9vxfg                                 9vxfg          9      9     9        99
 2 19qdlpmdrxone7sevennine               19qdlpmdrx1779 191779 1     9        19
 3 1dzntwofour9nineffck                  1dzn2499ffck   12499  1     9        19
 4 7bx8hpldgzqjheight                    7bx8hpldgzqjh8 788    7     8        78
 5 joneseven2sseven64chvczzn             j172s764chvcz… 172764 1     4        14
 6 seven82683                            782683         782683 7     3        73
 7 7onefour1eighttwo5three               71418253       71418… 7     3        73
 8 8lmsk871eight7                        8lmsk87187     887187 8     7        87
 9 ninefivefive2nine5ntvscdfdsmvqgcbxxxt 955295ntvscdf… 955295 9     5        95
10 onepx6hbgdssfivexs                    1px6hbgdss5xs  165    1     5        15
# ℹ 990 more rows
res |> summarise(results = sum(cal))

Reuse

Citation

BibTeX citation:
@online{luu2023,
  author = {Luu, Michael},
  title = {Advent of {Code} 2023, {Day} 1},
  date = {2023-12-02},
  langid = {en}
}
For attribution, please cite this work as:
Luu, Michael. 2023. “Advent of Code 2023, Day 1.” December 2, 2023.