Task 11

Thorn Thaler - <

2024-12-20

1 Setup

1.1 Libraries

library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(knitr)
library(cli)
library(bit64)

1.2 Retrieve Data from AoC

session_cookie <- set_cookies(session = keyring::key_get("AoC-GitHub-Cookie"))
base_url <- paste0("https://adventofcode.com/2024/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()) {
  text_block %>% 
    unlist() %>% 
    str_split(" ") %>% 
    extract2(1L) %>% 
    keep(nzchar) %>% 
    as.integer()
}

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

2 Puzzle Day 11

2.1 Part 1

2.1.1 Description

— Day 11: Plutonian Pebbles —

The ancient civilization on Pluto was known for its ability to manipulate spacetime, and while The Historians explore their infinite corridors, you’ve noticed a strange set of physics-defying stones.

At first glance, they seem like normal stones: they’re arranged in a perfectly straight line, and each stone has a number engraved on it.

The strange part is that every time you blink, the stones change.

Sometimes, the number engraved on a stone changes. Other times, a stone might split in two, causing all the other stones to shift over a bit to make room in their perfectly straight line.

As you observe them for a while, you find that the stones have a consistent behavior. Every time you blink, the stones each simultaneously change according to the first applicable rule in this list:

  • If the stone is engraved with the number 0, it is replaced by a stone engraved with the number 1.
  • If the stone is engraved with a number that has an even number of digits, it is replaced by two stones. The left half of the digits are engraved on the new left stone, and the right half of the digits are engraved on the new right stone. (The new numbers don’t keep extra leading zeroes: 1000 would become stones 10 and 0.)
  • If none of the other rules apply, the stone is replaced by a new stone; the old stone’s number multiplied by 2024 is engraved on the new stone.

No matter how the stones change, their order is preserved, and they stay on their perfectly straight line.

How will the stones evolve if you keep blinking at them? You take a note of the number engraved on each stone in the line (your puzzle input).

If you have an arrangement of five stones engraved with the numbers 0 1 10 99 999 and you blink once, the stones transform as follows:

  • The first stone, 0, becomes a stone marked 1.
  • The second stone, 1, is multiplied by 2024 to become 2024.
  • The third stone, 10, is split into a stone marked 1 followed by a stone marked 0.
  • The fourth stone, 99, is split into two stones marked 9.
  • The fifth stone, 999, is replaced by a stone marked 2021976.

So, after blinking once, your five stones would become an arrangement of seven stones engraved with the numbers 1 2024 1 0 9 9 2021976.

Here is a longer example:

Initial arrangement:
125 17

After 1 blink:
253000 1 7

After 2 blinks:
253 0 2024 14168

After 3 blinks:
512072 1 20 24 28676032

After 4 blinks:
512 72 2024 2 0 2 4 2867 6032

After 5 blinks:
1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32

After 6 blinks:
2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2

In this example, after blinking six times, you would have 22 stones. After blinking 25 times, you would have 55312 stones!

Consider the arrangement of stones in front of you. How many stones will you have after blinking 25 times?

2.1.2 Solution

Instead of storing all the stone ids, we store only their distribution (that is how many of eahc stone id there are). Then, in the next iteration we simply add the number of parents to the number of their offspring. If, for instance, we had 10 stones with id 1234, we will have 10 stones with id 12 and 10 stones with id 34. A samll corner case needed to be accounted for, namely if a stone id splits into the same ids (e.g. stone ide 77 will split into id 7 twice). In this case we need also to add the number of the parent twice.

In the end we simply have to sum up all numbers and we get the final count.

dance <- function(cur_seq, n) {
  dance_step <- function(d) {
    n <- nchar(as.character(d))
    if (d == 0L) {
      as.integer64(1L)
    } else if (n %% 2 == 0) {
      c(as.integer64(substr(as.character(d), 1L, n / 2L)),
        as.integer64(substr(as.character(d), n / 2L + 1L, n)))
    } else {
      as.integer64(d * 2024L)
    }
  }
  res <- rep(as.integer64(1L), length(cur_seq)) %>% 
    setNames(as.character(cur_seq))
  for (i in 1:n) {
    cur_res <- as.integer64(NULL)
    for (d in names(res)) {
      dnc <- dance_step(as.integer64(d))
      is_same_coef <- if_else(length(dnc) == 2L &&
                                length(unique(dnc)) == 1L, as.integer64(2L), 
                              as.integer64(1L))
      cur_res[as.character(dnc)] <- coalesce(cur_res[as.character(dnc)], 
                                             as.integer64(0L)) + is_same_coef * res[d]
    }
    res <- cur_res[cur_res > 0L]
  }
  sum(res)
}

dance(puzzle_data, 25L)
## integer64
## [1] 198075

2.2 Part 2

2.2.1 Description

— Part Two —

The Historians sure are taking a long time. To be fair, the infinite corridors are very large.

How many stones would you have after blinking a total of 75 times?

2.2.2 Solution

We simply need to increase the iteration count to 75.

dance(puzzle_data, 75L)
## integer64
## [1] 235571309320764