• Advent of Code 2020
  • Introduction
  • 1 Report Repair
    • 1.1 Part 1
      • 1.1.1 is the improved algorithm any better?
    • 1.2 Part 2
      • 1.2.1 Is our improved algorithm better this time?
    • 1.3 Extra: Implementing in Python
  • 2 Password Philosophy
    • 2.1 Part 1
    • 2.2 Part 2
  • 3 Toboggan Trajectory
    • 3.1 Part 1
    • 3.2 Part 2
    • 3.3 Extra: Is there a path that minimises the number of trees you encounter?
  • 4 Passport Processing
    • 4.1 Part 1
    • 4.2 Part 2
    • 4.3 Extra: Solving with regular expressions
  • 5 Binary Boarding
    • 5.1 Part 1
    • 5.2 Part 2
    • 5.3 Extra: Solving algebraicly
  • 6 Custom Customs
    • 6.1 Part 1
    • 6.2 Part 2
    • 6.3 Extra: solving part 1 & 2 with set functions
  • 7 Handy Haversacks
    • 7.1 Part 1
    • 7.2 Part 2
    • 7.3 Extra: alternative solution to part 2
  • 8 Handheld Halting
    • 8.1 Part 1
    • 8.2 Part 2
    • 8.3 Extra: update the computer class
  • 9 Encoding Error
    • 9.1 Part 1
    • 9.2 Part 2
  • 10 Adapter Array
    • 10.1 Part 1
    • 10.2 Part 2
  • 11 Seating System
    • 11.1 Part 1
    • 11.2 Part 2
    • 11.3 Solving faster
  • 12 Rain Risk
    • 12.1 Part 1
    • 12.2 Part 2
  • 13 Shuttle Search
    • 13.1 Part 1
    • 13.2 Part 2
  • 14 Docking Data
    • 14.1 Part 1
    • 14.2 Part 2
  • 15 Rambunctious Recitation
    • 15.1 Part 1
    • 15.2 Part 2
  • 16 Ticket Translation
    • 16.1 Part 1
    • 16.2 Part 2
  • 17 Conway Cubes
    • 17.1 Part 1
    • 17.2 Part 2
  • 18 Operation Order
    • 18.1 Part 1
    • 18.2 Part 2
  • 19 Monster Messages
    • 19.1 Part 1
    • 19.2 Part 2
  • 20 Jurassic Jigsaw
    • 20.1 Part 1
    • 20.2 Part 2
  • 21 Allergen Assessment
    • 21.1 Part 1
    • 21.2 Part 2
  • 22 Crab Combat
    • 22.1 Part 1
    • 22.2 Part 2
  • 23 Crab Cups
    • 23.1 Part 1
    • 23.2 Part 2
  • 24 Lobby Layout
    • 24.1 Part 1
    • 24.2 Part 2
  • 25 Combo Breaker
    • 25.1 Part 1
    • 25.2 Part 2

Advent of Code 2020

Day 13 Shuttle Search

This is my attempt to solve Day 13.

sample <- read_lines("samples/day_13_sample.txt")
actual <- read_lines("inputs/day_13_input.txt")

13.1 Part 1

We can solve part 1 by finding the first time each of the buses arrives after we arrive, then finding which bus has the minimum time.

part_1 <- function(input) {
  earliest_time <- as.integer(input[[1]])
  buses <- input[[2]] %>%
    str_split(",") %>%
    pluck(1) %>%
    str_subset("\\d") %>%
    as.integer()
  
  first_available <- ceiling(earliest_time / buses) * buses
  which_bus <- which.min(first_available)
  (first_available[[which_bus]] - earliest_time) * buses[[which_bus]]
}

We can check that this function works on the sample data:

part_1(sample) == 295
## [1] TRUE

And we can run with the actual data

part_1(actual)
## [1] 3269

13.2 Part 2

Part 2 could be solved with the Chinese Remainder Theorem, but we could solve it more simply by iterating over the input items and keeping track of the current time and the current “jump” size. The jump size is equal to the cumulative product of the busses.

part_2 <- function(input) {
  # take the 2nd row of the input, convert x's to 0's
  x <- input[[2]] %>%
    str_replace_all("x", "0") %>%
    str_split(",") %>%
    pluck(1) %>%
    as.numeric()
  
  # get the sequence of values that are non zero, index from 0
  y <- seq_along(x)[x != 0] - 1
  # get rid of the 0's (x's)
  x <- x[x != 0]
  
  # start with a jump equal to the first value in the input
  j <- x[1]
  # start at time 0
  t <- 0
  
  # iterate over the input, skipping the first item
  for (i in 2:length(x)) {
    # increase our time by the size of the jump while the time + step size is
    # not a divisor of the current input item
    while ((t + y[[i]]) %% x[[i]]) {
      t <- t + j
    }
    # increase the size of our jump by the current input item (bus)
    j <- j * x[[i]]
  }
  # return the time
  t
}

We can run this function on the sample data.

part_2(sample) == 1068781
## [1] TRUE

And we can run on the actual data. We need to make sure that we alter the “scientific notation penalty” option to view the answer as an integer.

options(scipen = 999)
part_2(actual)
## [1] 672754131923874

Elapsed Time: 0.12s