Task 6

Thorn Thaler - <

2025-03-06

1 Setup

1.1 Libraries

library(httr)
library(xml2)
library(tibble)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(stringi)
library(knitr)
library(cli)
library(digest)

1.2 Retrieve Data from AoC

session_cookie <- set_cookies(session = keyring::key_get("AoC-GitHub-Cookie"))
base_url <- paste0("https://adventofcode.com/", params$year, "/day/", params$task_nr)
puzzle <- GET(base_url,
              session_cookie) %>% 
  content(encoding = "UTF-8") %>% 
  xml_find_all("///article") %>% 
  lapply(as.character)

parse_puzzle_data <- function(text_block = readClipboard()) {
  if (length(text_block) == 1L) {
    text_block <- text_block %>% 
      str_split("\n") %>% 
      extract2(1L) %>% 
      keep(nzchar)
  }
  coords <- text_block %>% 
    str_extract_all("\\d+") %>% 
    do.call(rbind, .) %>% 
    set_colnames(c("x0", "y0", "x1", "y1"))
  storage.mode(coords) <- "integer"
  coords %>% 
    add(1L) %>% 
    as_tibble() %>% 
    mutate(op = text_block %>% 
             str_extract("\\D+") %>% 
             str_trim(), .before = 1L)
}

puzzle_data <- local({
  GET(paste0(base_url, "/input"),
      session_cookie) %>% 
    content(encoding = "UTF-8") %>% 
    parse_puzzle_data()
})

2 Puzzle Day 6

2.1 Part 1

2.1.1 Description

— Day 6: Probably a Fire Hazard —

Because your neighbors keep defeating you in the holiday house decorating contest year after year, you’ve decided to deploy one million lights in a 1000x1000 grid.

Furthermore, because you’ve been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration.

Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off.

To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order.

For example:

  • turn on 0,0 through 999,999 would turn on (or leave on) every light.
  • toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off.
  • turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.

After following the instructions, how many lights are lit?

2.1.2 Solution

We create a grid of booleans storing the current state and iterate through the instructions and execute each of them in turn. In the end we calculate the switched on lights.

get_indices <- function(x0, y0, x1, y1, ...) {
  expand.grid(
    row = x0:x1,
    col = y0:y1
  ) %>% 
    as.matrix()
}

get_light_show <- function(ops, fns) {
  light_array <- matrix(FALSE, 1000L, 1000L)
  for (i in seq_len(nrow(ops))) {
    op <- ops %>% 
      slice(i) %>% 
      as.list()
    idx <- do.call(get_indices, op)
    light_array[idx] <- fns[[op$op]](light_array[idx])
  }
  light_array
}

fns_boolean <-   fns <- list(
    toggle = function(x) !x,
    "turn on" = function(x) TRUE,
    "turn off" = function(x) FALSE
  )

get_light_show(puzzle_data, fns_boolean) %>% 
  sum()
## [1] 543903

2.2 Part 2

2.2.1 Description

— Part Two —

You just finish implementing your winning light pattern when you realize you mistranslated Santa’s message from Ancient Nordic Elvish.

The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero.

The phrase turn on actually means that you should increase the brightness of those lights by 1.

The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero.

The phrase toggle actually means that you should increase the brightness of those lights by 2.

What is the total brightness of all lights combined after following Santa’s instructions?

For example:

  • turn on 0,0 through 0,0 would increase the total brightness by 1.
  • toggle 0,0 through 999,999 would increase the total brightness by 2000000.

2.2.2 Solution

This time, instead of booleans we store integers reflecting their brightness.

fns_integer <-   fns <- list(
    toggle = function(x) x + 2,
    "turn on" = function(x) x + 1,
    "turn off" = function(x) pmax(x - 1, 0)
  )

get_light_show(puzzle_data, fns_integer) %>% 
  sum()
## [1] 14687245

For the fun of it, here’s a picture of the lights:

heatmap(
  get_light_show(puzzle_data, fns_integer),
  Rowv = NA,
  Colv = NA,
  col =  rgb(red = 0:255 / 255, blue = 0:255 / 255, green = 0:255 / 255),
  scale = "none",
  xaxt = "n",
  yaxt = "n",
  labRow = NA,
  labCol = NA
)