Last updated: 2017-12-08
Code version: aad1333
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 aocodeR_0.1.1 testthat_1.0.2 forcats_0.2.0 stringr_1.2.0
[6] dplyr_0.7.4 purrr_0.2.4 readr_1.1.1 tidyr_0.7.2 tibble_1.3.4
[11] ggplot2_2.2.1 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] reshape2_1.4.2 haven_1.1.0 lattice_0.20-35 colorspace_1.3-2 htmltools_0.3.6
[6] base64enc_0.1-3 yaml_2.1.15 rlang_0.1.4 foreign_0.8-69 glue_1.2.0
[11] modelr_0.1.1 readxl_1.0.0 bindr_0.1 plyr_1.8.4 munsell_0.4.3
[16] gtable_0.2.0 cellranger_1.1.0 rvest_0.3.2 evaluate_0.10.1 psych_1.7.8
[21] knitr_1.17 curl_3.0 parallel_3.4.2 broom_0.4.2 Rcpp_0.12.14
[26] scales_0.5.0 backports_1.1.1 jsonlite_1.5 mnormt_1.5-5 digest_0.6.12
[31] hms_0.4.0 stringi_1.1.6 grid_3.4.2 rprojroot_1.2 cli_1.0.0
[36] tools_3.4.2 magrittr_1.5 lazyeval_0.2.1 crayon_1.3.4 pkgconfig_2.0.1
[41] rsconnect_0.8.5 xml2_1.1.1 lubridate_1.7.1 assertthat_0.2.0 rmarkdown_1.8
[46] httr_1.3.1 rstudioapi_0.7 R6_2.2.2 nlme_3.1-131 compiler_3.4.2
[51] git2r_0.19.0
You receive a signal directly from the CPU. Because of your recent assistance with jump instructions, it would like you to compute the result of a series of unusual register instructions.
Each instruction consists of several parts: the register to modify, whether to increase or decrease that register’s value, the amount by which to increase or decrease it, and a condition. If the condition fails, skip the instruction without modifying the register. The registers all start at 0. The instructions look like this:
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
These instructions would be processed as follows:
Because a starts at 0, it is not greater than 1, and so b is not modified. a is increased by 1 (to 1) because b is less than 5 (it is 0). c is decreased by -10 (to 10) because a is now greater than or equal to 1 (it is 1). c is increased by -20 (to -10) because c is equal to 10. After this process, the largest value in any register is 1.
You might also encounter <= (less than or equal to) or != (not equal to). However, the CPU doesn’t have the bandwidth to tell you what all the registers are named, and leaves that to you to determine.
What is the largest value in any register after completing the instructions in your puzzle input?
library(tidyverse)
library(testthat)
library(aocodeR)
input <- aoc_get_input(day = 8, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
"/secrets/session_cookie.txt"))
expressify_input <- function(input) {
input %>%
strsplit("\n") %>% unlist %>%
map(~{strsplit(., " ") %>%
unlist %>%
setNames(c("reg", "s", "n", "cond", "cond_reg", "cond_cnd", "cond_n"))}) %>%
do.call("bind_rows", .) %>%
mutate(s = recode(s, "inc" = "+","dec" = "-"),
cmd = paste(cond,"(", paste0("e$",cond_reg), cond_cnd, cond_n, ")",
paste0("e$",reg), "<-", paste0("e$",reg), s, n,";"))
}
eval_input_df <- function(input, out_max = F) {
max.e <- 0
e <- new.env(parent = emptyenv())
# assign registers to environment e
input %>% select(reg, cond_reg) %>% unlist %>% unique %>%
lapply(FUN = function(x){assign(x, 0, envir = e)})
for(i in 1:nrow(input)){
eval(parse(text = input$cmd[i]))
max.e <- max(max.e, mget(ls(e), envir = e) %>% unlist %>% max, envir = max.e)
}
if(out_max){max.e}else{
mget(ls(e), envir = e) %>% unlist %>% max}
}
test_input <- "b inc 5 if a > 1\na inc 1 if b < 5\nc dec -10 if a >= 1\nc inc -20 if c == 10" %>%
expressify_input()
expect_equal(eval_input_df(test_input), 1)
expressify_input(input) %>% eval_input_df()
[1] 5849
system.time(expressify_input(input) %>% eval_input_df())
user system elapsed
0.498 0.020 0.523
To be safe, the CPU also needs to know the highest value held in any register during this process so that it can decide how much memory to allocate to these operations. For example, in the above instructions, the highest value ever held was 10 (in register c after the third instruction was evaluated).
expect_equal(eval_input_df(test_input, out_max = T), 10)
expressify_input(input) %>% eval_input_df(out_max = T)
[1] 6702
expressify_input(input)
ls(e)
[1] "a" "b" "bt" "c" "clj" "cto" "d" "f" "gnz" "hwv" "ino" "jp" "l" "liq"
[15] "ly" "mu" "mvh" "nm" "noj" "ooh" "or" "q" "sp" "t" "uc" "uix" "vv" "w"
mget(ls(e), envir = e) %>% unlist
a b bt c clj cto d f gnz hwv ino jp l liq ly mu mvh nm noj ooh or q
2 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sp t uc uix vv w
0 0 0 0 0 0
I really liked today’s. It got me practicing with working with environments and eval
. It also prompted some useful discussions in the office about the dangers in eval
, summarised in this xkcd (HT @acceleratedsci)
template based on the workflowr standalone template