Advent of Code 2023, Day 2

advent of code
Author

Michael Luu

Published

December 3, 2023

library(tidyverse)
library(here)

Part 1

The solution for Part 1 was actually quite simple. The major complexity of this puzzle is due to the data not being in a ‘tidy’ format. The majority of the code below is just cleaning the data

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

data <- as_tibble(data)

data <- data |> 
  separate(value, sep = ': ', into = c('game', 'value')) |> 
  separate(value, sep = '; ', into = paste0('set', 1:10)) |> 
  pivot_longer(contains('set'), names_to = 'set') |> 
  filter(!is.na(value)) |> 
  separate(value, sep = ', ', into = paste0('cube', 1:3)) |> 
  pivot_longer(contains('cube'), names_to = 'cube') |> 
  filter(!is.na(value)) |> 
  separate(value, sep = ' ', into = c('count', 'color')) |> 
  pivot_wider(names_from = 'color', values_from = 'count', values_fill = '0') |> 
  mutate(across(c('blue', 'red', 'green'), as.numeric)) |> 
  mutate(game_id = str_extract(game, '\\d+'), .after = game)

data
# A tibble: 1,161 × 7
   game   game_id set   cube   blue   red green
   <chr>  <chr>   <chr> <chr> <dbl> <dbl> <dbl>
 1 Game 1 1       set1  cube1     1     0     0
 2 Game 1 1       set1  cube2     0     1     0
 3 Game 1 1       set2  cube1     0    10     0
 4 Game 1 1       set3  cube1     0     8     0
 5 Game 1 1       set3  cube2     1     0     0
 6 Game 1 1       set3  cube3     0     0     1
 7 Game 1 1       set4  cube1     0     0     1
 8 Game 1 1       set4  cube2     5     0     0
 9 Game 2 2       set1  cube1     0     0     9
10 Game 2 2       set1  cube2     0    11     0
# ℹ 1,151 more rows

Now that we have the data in a ‘tidy’ format, let’s refer back to the puzzle prompt.

The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?

We can figure this out by using filter on each color columns and checking whether any of the games contained any invalid number of cubes.

res <- data |> 
  group_by(game) |>
  mutate(
    valid = case_when(
      any(red > 12 | green > 13 | blue > 14) ~ FALSE,
      .default = TRUE
    )
  ) |> 
  ungroup() |> 
  filter(valid == TRUE)

res
# A tibble: 539 × 8
   game   game_id set   cube   blue   red green valid
   <chr>  <chr>   <chr> <chr> <dbl> <dbl> <dbl> <lgl>
 1 Game 1 1       set1  cube1     1     0     0 TRUE 
 2 Game 1 1       set1  cube2     0     1     0 TRUE 
 3 Game 1 1       set2  cube1     0    10     0 TRUE 
 4 Game 1 1       set3  cube1     0     8     0 TRUE 
 5 Game 1 1       set3  cube2     1     0     0 TRUE 
 6 Game 1 1       set3  cube3     0     0     1 TRUE 
 7 Game 1 1       set4  cube1     0     0     1 TRUE 
 8 Game 1 1       set4  cube2     5     0     0 TRUE 
 9 Game 2 2       set1  cube1     0     0     9 TRUE 
10 Game 2 2       set1  cube2     0    11     0 TRUE 
# ℹ 529 more rows

Once we have the valid games, we can simply sum the game_id columns to get the solution

res |> pull(game_id) |> unique() |> 
  as.numeric() |> sum()

Part 2

Again, the solution for Part 2 is quite simple once we have the data in a ‘tidy’ format.

For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?

The elfs would like to know what is the ‘minimum’ number of cubes that has to be in each bag for each of the games played. The solution is the sum of the ‘power’ (defined as the product of the number of cubes for each color). We can solve this by taking the maximum number of cubes observed for a given game, then taking the product of the maximum number of cubes for each color.

res <- data |> 
  group_by(game) |>
  summarise(across(c(blue, red, green), \(x) max(x))) |> 
  mutate(power = blue * red * green)

res
# A tibble: 100 × 5
   game      blue   red green power
   <chr>    <dbl> <dbl> <dbl> <dbl>
 1 Game 1       5    10     1    50
 2 Game 10      8     9    12   864
 3 Game 100     5     7     1    35
 4 Game 11      1    15     3    45
 5 Game 12      8     9     7   504
 6 Game 13      8     2    18   288
 7 Game 14      4     3     5    60
 8 Game 15     15     3     7   315
 9 Game 16     20     6    13  1560
10 Game 17     11     4     1    44
# ℹ 90 more rows
res |> summarise(total_power = sum(power))

Reuse

Citation

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