Advent of Code 2020 - Day 15
Published: 2021-11-28 02:23:23

---
title: Advent of Code 2020 - Day 15
published: true
date: 2020-12-16 00:00:00 UTC
tags: 
canonical_url: https://ericburden.work/blog/2020/12/16/advent-of-code-2020-day-15/
---

In the spirit of the holidays (and programming), I’ll be posting my solutions to the [Advent of Code 2020](https://adventofcode.com/) puzzles here, at least one day after they’re posted (no spoilers!). I’ll be implementing the solutions in R because, well, that’s what I like! What I won’t be doing is posting any of the actual answers, just the reasoning behind them.

Also, as a general convention, whenever the puzzle has downloadable input, I’m saving it in a file named `input.txt`.

# Day 15 - Rambunctious Recitation

Find the problem description [HERE](https://adventofcode.com/2020/day/15).

## Part One - ~~Reindeer~~ Elf Games

For part one, we have elves playing a memory game. Given a list of numbers, the elves say each number in turn. When they reach the end of the list, they begin saying the number of rounds it has been since the number spoken on the previous turn had been spoken prior to the previous turn. So, for the list `0, 3, 6`, you get `[0, 3, 6], 0, 3, 1, 0, 4, 0` for the first 9 turns. The real question is: What will the number spoken on turn 2020 be?

I’ll be honest, the implementation below isn’t my first crack at this one, but, for reasons that will become clear later (or sooner if you’re starting to get an idea of how these puzzles are structured), it was the fastest implementation I could come up with in R.

```r
library(testthat) # For tests

# Given a starting sequence `start_vec` and a number `n`, returns the `n`th 
# number of the elve's counting game, according to the rules in the puzzle
# instructions. Note, the `rounds` vector below contains, at each index, the
# last round (prior to the most recent round) that the number (index - 1) was
# spoken. This is because R is 1-indexed, so the value for '0' is stored in
# index '1' and so on.
number_spoken <- function(start_vec, n) {
  rounds <- numeric(0) # Empty vector for the round a number was last spoken
  start_len <- length(start_vec) # Length of the `start_vec`
  last_number <- start_vec[start_len] # Last number spoken, starting value
  
  # Fill in the starting numbers into `rounds`
  for (i in 1:start_len) { rounds[[start_vec[i]+1]] <- i }
  
  # For each number after the starting veector...
  for (i in (start_len+1):n) {
    index <- last_number + 1 # Correction for 1-indexing
    
    # If the `index` either contains the number of the last round or an NA, 
    # then the last round was the first time `last_number` was spoken and the
    # `next_number` should be '0'. Otherwise, the `next_number` should be the
    # number of the last round (i-1) minus the number of the previous round
    # in which the number was spoken (rounds[last_number+1])
    next_number <- if (is.na(rounds[index]) || rounds[index] == i - 1) {
      0
    } else {
      (i - 1) - rounds[last_number+1]
    }
    
    rounds[last_number+1] <- i - 1 # Update the round number stored for this number
    last_number <- next_number # The new `last_number` is this `next_number`
    
    # Sanity Check
    if (i %% 10000 == 0) { cat('\r', paste('Simulating round:', i)) }
  }
  
  next_number # Return the `n`th number spoken
}

test_that("sample inputs return expected results", {
  expect_equal(number_spoken(c(0, 3, 6), 2020), 436)
  expect_equal(number_spoken(c(1, 3, 2), 2020), 1)
  expect_equal(number_spoken(c(1, 2, 3), 2020), 27)
  expect_equal(number_spoken(c(2, 3, 1), 2020), 78)
  expect_equal(number_spoken(c(3, 2, 1), 2020), 438)
  expect_equal(number_spoken(c(3, 1, 2), 2020), 1836)
})

answer1 <- number_spoken(c(2, 0, 1, 7, 4, 14, 18), 2020)

# Answer: 496

```

For reasons that I can’t recall, I was inspired to use testing for this puzzle. I think it had something to do with the number of test inputs we were given, since the `testthat` package and functions give me an easy way to test them all at once. I could also make tweaks to my implementation while being sure I didn’t miss any known edge cases.

## Part Two - MORE!

[![](https://ericburden.work/blog/2020-12-16-advent-of-code-2020-day-15/index.en_files/day_15_elf_meme.jpeg)](https://www.reddit.com/r/adventofcode/comments/kdiu1k/2020_day_15_when_elves_wanna_play_with_you/)

```r
source('exercise_1.R')

# Yep, that's it.
answer2 <- number_spoken(c( 2, 0, 1, 7, 4, 14, 18), 30000000)

```

This is why the speed of the implementation mattered.

## Wrap-Up

Ah, there are some days when I’m jealous of the folks using compiled languages, but then I realize that my day job makes MUCH better use of `shiny` dashboards and data analysis with `tidyverse` than it does blazing fast array lookups, and I snap out of it. That said, this was an excellent opportunity to learn some things about doing things quickly in R (which is something I hadn’t spent much time on up to now). My brief takeaway is: getting a value from a known index in a vector, even if the vector contains a lot of `NA`’s , is pretty snappy. It’s definitely faster than looking up values by key in an environment, and it’s _way_ faster than using a named list as a sort of pseudo-dictionary. Overall, a good day!

If you found a different solution (or spotted a mistake in one of mine), please drop me a line!