Last updated: 2017-12-28
Code version: 4c4c68b
sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.2 microbenchmark_1.4-2.1 aocodeR_0.1.1 testthat_1.0.2
[5] forcats_0.2.0 stringr_1.2.0 dplyr_0.7.4 purrr_0.2.4
[9] readr_1.1.1 tidyr_0.7.2 tibble_1.3.4 ggplot2_2.2.1.9000
[13] tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] zoo_1.8-0 reshape2_1.4.2 splines_3.4.2 haven_1.1.0 lattice_0.20-35
[6] colorspace_1.3-2 htmltools_0.3.6 yaml_2.1.15 base64enc_0.1-3 survival_2.41-3
[11] rlang_0.1.4 foreign_0.8-69 glue_1.2.0 modelr_0.1.1 readxl_1.0.0
[16] multcomp_1.4-8 bindr_0.1 plyr_1.8.4 munsell_0.4.3 gtable_0.2.0
[21] cellranger_1.1.0 rvest_0.3.2 mvtnorm_1.0-6 codetools_0.2-15 psych_1.7.8
[26] evaluate_0.10.1 knitr_1.17 parallel_3.4.2 curl_3.0 TH.data_1.0-8
[31] broom_0.4.2 Rcpp_0.12.14 scales_0.5.0.9000 backports_1.1.1 jsonlite_1.5
[36] mnormt_1.5-5 hms_0.4.0 digest_0.6.12 stringi_1.1.6 grid_3.4.2
[41] rprojroot_1.2 cli_1.0.0 tools_3.4.2 sandwich_2.4-0 magrittr_1.5
[46] lazyeval_0.2.1 crayon_1.3.4 pkgconfig_2.0.1 MASS_7.3-47 Matrix_1.2-12
[51] rsconnect_0.8.5 xml2_1.1.1 lubridate_1.7.1 assertthat_0.2.0 rmarkdown_1.8
[56] httr_1.3.1 rstudioapi_0.7 R6_2.2.2 nlme_3.1-131 git2r_0.19.0
[61] compiler_3.4.2
Following the twisty passageways deeper and deeper into the CPU, you finally reach the core of the computer. Here, in the expansive central chamber, you find a grand apparatus that fills the entire room, suspended nanometers above your head.
You had always imagined CPUs to be noisy, chaotic places, bustling with activity. Instead, the room is quiet, motionless, and dark.
Suddenly, you and the CPU’s garbage collector startle each other. “It’s not often we get many visitors here!”, he says. You inquire about the stopped machinery.
“It stopped milliseconds ago; not sure why. I’m a garbage collector, not a doctor.” You ask what the machine is for.
“Programs these days, don’t know their origins. That’s the Turing machine! It’s what makes the whole computer work.” You try to explain that Turing machines are merely models of computation, but he cuts you off. “No, see, that’s just what they want you to think. Ultimately, inside every CPU, there’s a Turing machine driving the whole thing! Too bad this one’s broken. We’re doomed!”
You ask how you can help. “Well, unfortunately, the only way to get the computer running again would be to create a whole new Turing machine from scratch, but there’s no way you can-” He notices the look on your face, gives you a curious glance, shrugs, and goes back to sweeping the floor.
You find the Turing machine blueprints (your puzzle input) on a tablet in a nearby pile of debris. Looking back up at the broken Turing machine above, you can start to identify its parts:
A tape which contains 0 repeated infinitely to the left and right. A cursor, which can move left or right along the tape and read or write values at its current position. A set of states, each containing rules about what to do based on the current value under the cursor. Each slot on the tape has two possible values: 0 (the starting value for all slots) and 1. Based on whether the cursor is pointing at a 0 or a 1, the current state says what value to write at the current position of the cursor, whether to move the cursor left or right one slot, and which state to use next.
For example, suppose you found the following blueprint:
Begin in state A. Perform a diagnostic checksum after 6 steps.
In state A: If the current value is 0: - Write the value 1. - Move one slot to the right. - Continue with state B. If the current value is 1: - Write the value 0. - Move one slot to the left. - Continue with state B.
In state B: If the current value is 0: - Write the value 1. - Move one slot to the left. - Continue with state A. If the current value is 1: - Write the value 1. - Move one slot to the right. - Continue with state A. Running it until the number of steps required to take the listed diagnostic checksum would result in the following tape configurations (with the cursor marked in square brackets):
... 0 0 0 [0] 0 0 ... (before any steps; about to run state A)
... 0 0 0 1 [0] 0 ... (after 1 step; about to run state B)
... 0 0 0 [1] 1 0 ... (after 2 steps; about to run state A)
... 0 0 [0] 0 1 0 ... (after 3 steps; about to run state B)
... 0 [0] 1 0 1 0 ... (after 4 steps; about to run state A)
... 0 1 [1] 0 1 0 ... (after 5 steps; about to run state B)
... 0 1 1 [0] 1 0 ... (after 6 steps; about to run state A)
The CPU can confirm that the Turing machine is working by taking a diagnostic checksum after a specific number of steps (given in the blueprint). Once the specified number of steps have been executed, the Turing machine should pause; once it does, count the number of times 1 appears on the tape. In the above example, the diagnostic checksum is 3.
Recreate the Turing machine and save the computer! What is the diagnostic checksum it produces once it’s working again?
library(tidyverse)
library(testthat)
library(aocodeR)
input <- aoc_get_input(day = 25, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
"/secrets/session_cookie.txt")) %>%
strsplit(., "\n") %>% unlist
input
[1] "Begin in state A."
[2] "Perform a diagnostic checksum after 12481997 steps."
[3] ""
[4] "In state A:"
[5] " If the current value is 0:"
[6] " - Write the value 1."
[7] " - Move one slot to the right."
[8] " - Continue with state B."
[9] " If the current value is 1:"
[10] " - Write the value 0."
[11] " - Move one slot to the left."
[12] " - Continue with state C."
[13] ""
[14] "In state B:"
[15] " If the current value is 0:"
[16] " - Write the value 1."
[17] " - Move one slot to the left."
[18] " - Continue with state A."
[19] " If the current value is 1:"
[20] " - Write the value 1."
[21] " - Move one slot to the right."
[22] " - Continue with state D."
[23] ""
[24] "In state C:"
[25] " If the current value is 0:"
[26] " - Write the value 0."
[27] " - Move one slot to the left."
[28] " - Continue with state B."
[29] " If the current value is 1:"
[30] " - Write the value 0."
[31] " - Move one slot to the left."
[32] " - Continue with state E."
[33] ""
[34] "In state D:"
[35] " If the current value is 0:"
[36] " - Write the value 1."
[37] " - Move one slot to the right."
[38] " - Continue with state A."
[39] " If the current value is 1:"
[40] " - Write the value 0."
[41] " - Move one slot to the right."
[42] " - Continue with state B."
[43] ""
[44] "In state E:"
[45] " If the current value is 0:"
[46] " - Write the value 1."
[47] " - Move one slot to the left."
[48] " - Continue with state F."
[49] " If the current value is 1:"
[50] " - Write the value 1."
[51] " - Move one slot to the left."
[52] " - Continue with state C."
[53] ""
[54] "In state F:"
[55] " If the current value is 0:"
[56] " - Write the value 1."
[57] " - Move one slot to the right."
[58] " - Continue with state D."
[59] " If the current value is 1:"
[60] " - Write the value 1."
[61] " - Move one slot to the right."
[62] " - Continue with state A."
setup_system <- function(){
states <- LETTERS[1:6]
states %>% map(~assign(.x, new.env(parent = emptyenv()), envir = globalenv()))
assign("sys", new.env(parent = emptyenv()), envir = globalenv())
assign("locs", new.env(parent = emptyenv()), envir = globalenv())
sys$state <- "A"
sys$loc <- 0
}
setup_programs <- function(){
foo <- list(
A = function(){
locs[[as.character(sys$loc)]] <- as.numeric(!sys$value)
sys$loc <- sys$loc + c(1, -1)[sys$value + 1]
sys$state <- c("B", "C")[sys$value + 1]},
B = function(){
locs[[as.character(sys$loc)]] <- 1
sys$loc <- sys$loc + c(-1, 1)[sys$value + 1]
sys$state <- c("A", "D")[sys$value + 1]},
C = function(){
locs[[as.character(sys$loc)]] <- 0
sys$loc <- sys$loc -1
sys$state <- c("B", "E")[sys$value + 1]},
D = function(){
locs[[as.character(sys$loc)]] <- as.numeric(!sys$value)
sys$loc <- sys$loc + 1
sys$state <- c("A", "B")[sys$value + 1]},
E = function(){
locs[[as.character(sys$loc)]] <- 1
sys$loc <- sys$loc -1
sys$state <- c("F", "C")[sys$value + 1]},
F = function(){
locs[[as.character(sys$loc)]] <- 1
sys$loc <- sys$loc + 1
sys$state <- c("D", "A")[sys$value + 1]})
mapply(assign, value = foo, envir = mget(names(foo), envir = globalenv()),
MoreArgs = list(x = "foo"))
}
run_program <- function(n){
for(i in 1:n){
if(!exists(as.character(sys$loc), envir = locs)){
sys$value <- locs[[as.character(sys$loc)]] <- 0}else{
sys$value <- locs[[as.character(sys$loc)]]}
get(sys$state)$foo()
}
}
#expect_equal(,)
mget(ls(locs), envir = locs) %>% unlist %>% sum
[1] 3362
— Part Two — The Turing machine, and soon the entire computer, springs back to life. A console glows dimly nearby, awaiting your command.
reboot printer Error: That command requires priority 50. You currently have priority 0. You must deposit 50 stars to increase your priority to the required level. The console flickers for a moment, and then prints another message:
Star accepted. You must deposit 49 stars to increase your priority to the required level. The garbage collector winks at you, then continues sweeping.
You don’t have enough stars to reboot the printer, though. You need 5 more.