See more puzzles

Advent of Code

Session information

sessionInfo()

Brief

— Day 18: Duet —

You discover a tablet containing some strange assembly code labeled simply “Duet”. Rather than bother the sound card with it, you decide to run the code yourself. Unfortunately, you don’t see any documentation, so you’re left to figure out what the instructions mean on your own.

It seems like the assembly is meant to operate on a set of registers that are each named with a single letter and that can each hold a single integer. You suppose each register should start with a value of 0.

There aren’t that many instructions, so it shouldn’t be hard to figure out what they do. Here’s what you determine:

Many of the instructions can take either a register (a single letter) or a number. The value of a register is the integer it contains; the value of a number is that number.

After each jump instruction, the program continues with the instruction to which the jump jumped. After any other instruction, the program continues with the next instruction. Continuing (or jumping) off either end of the program terminates it.

For example:

set a 1
add a 2
mul a a
mod a 5
snd a
set a 0
rcv a
jgz a -1
set a 1
jgz a -2

The first four instructions set a to 1, add 2 to it, square it, and then set it to itself modulo 5, resulting in a value of 4.

Then, a sound with frequency 4 (the value of a) is played. After that, a is set to 0, causing the subsequent rcv and jgz instructions to both be skipped (rcv because a is 0, and jgz because a is not greater than 0).

Finally, a is set to 1, causing the next jgz instruction to activate, jumping back two instructions to another jump, which jumps again to the rcv, which ultimately triggers the recover operation. At the time the recover operation is executed, the frequency of the last sound played is 4.

What is the value of the recovered frequency (the value of the most recently played sound) the first time a rcv instruction is executed with a non-zero value?

Let’s go

Packages & functions

library(tidyverse)
library(testthat)
library(aocodeR)

Input

input <- aoc_get_input(day = 18, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
                                                 "/secrets/session_cookie.txt")) %>% 
    strsplit(., "\n") %>% unlist %>% 
    strsplit(., " ") 

Functions

value.Y <- function(Y){
    Y <- type.convert(Y, as.is = T)
    if(class(Y) == "character"){as.numeric(get(Y, envir = e))}else{as.numeric(Y)}
}

#expect_equal(,)
create_inst_frm <- function(f, envir = e){
    
    f$snd <- function(X, Y = NA){
        assign("snd", value.Y(X), envir = e)
    }
    f$set <- function(X, Y){
        assign(X, value.Y(Y), envir = e)    
    }
    f$add <- function(X, Y){
        assign(X, get(X, envir = e) + value.Y(Y), envir = e)
    }
    f$mul <- function(X, Y){
        assign(X, get(X, envir = e) * value.Y(Y), envir = e)
    }
    f$mod <- function(X, Y){
        assign(X, get(X, envir = e) %% value.Y(Y), envir = e)
    }
    f$rcv <- function(X, Y = NA){
        if(value.Y(X) != 0){f$out <- T}
    }
    f$jgz <- function(X, Y){
        if(value.Y(X) > 0){
            assign("i", (get("i", envir = f) + value.Y(Y) - 1) * as.numeric(value.Y(Y) != 0), envir = f)}
    }
}

run_inst <- function(input, f) {
      
    f$i <- 1
    i <- 1
    f$out <- F
    while((1 <= f$i & f$i <= length(input)) & f$out == F){
            cat("i = ", i, " --- f$i = ", f$i, "\n\n")
        input[[f$i]]
        inst <- input[[f$i]]
        f[[inst[1]]](inst[2], inst[3])
        f$i <- f$i + 1
        i <- i + 1
    }
    e$snd
}

Test

e <- new.env(parent = emptyenv())
f <- new.env(parent = emptyenv())
lapply(unique(map(input, 2)), FUN = assign, value = 0, envir = e)

create_inst_frm(f)
run_inst(input, f)

Success!



—- Part 2 —-

❗️ HELP NEEDED ❗️

Really not sure how to approach this, most likely on top of performance considerations. Is this a job for future package?

Any feedback? let me know here!


Brief

As you congratulate yourself for a job well done, you notice that the documentation has been on the back of the tablet this entire time. While you actually got most of the instructions correct, there are a few key differences. This assembly code isn’t about sound at all - it’s meant to be run twice at the same time.

Each running copy of the program has its own set of registers and follows the code independently - in fact, the programs don’t even necessarily run at the same speed. To coordinate, they use the send (snd) and receive (rcv) instructions:

  • snd X sends the value of X to the other program. These values wait in a queue until that program is ready to receive them. Each program has its own message queue, so a program can never receive a message it sent.

  • rcv X receives the next value and stores it in register X. If no values are in the queue, the program waits for a value to be sent to it. Programs do not continue to the next instruction until they have received a value. Values are received in the order they are sent. Each program also has its own program ID (one 0 and the other 1); the register p should begin with this value.

For example:

snd 1
snd 2
snd p
rcv a
rcv b
rcv c
rcv d

Both programs begin by sending three values to the other. Program 0 sends 1, 2, 0; program 1 sends 1, 2, 1. Then, each program receives a value (both 1) and stores it in a, receives another value (both 2) and stores it in b, and then each receives the program ID of the other program (program 0 receives 1; program 1 receives 0) and stores it in c. Each program now sees a different value in its own copy of register c.

Finally, both programs try to rcv a fourth time, but no data is waiting for either of them, and they reach a deadlock. When this happens, both programs terminate.

It should be noted that it would be equally valid for the programs to run at different speeds; for example, program 0 might have sent all three values and then stopped at the first rcv before program 1 executed even its first instruction.

Once both of your programs have terminated (regardless of what caused them to do so), how many times did program 1 send a value?

Let’s go

value.Y <- function(Y, envir = e){
    Y <- type.convert(Y, as.is = T)
    if(class(Y) == "character"){as.numeric(get(Y, envir = envir))}else{as.numeric(Y)}
}
#expect_equal(,)
create_inst_frm <- function(f){
    
    f$snd <- function(X, Y = NA, envir, env_name){
       snd_env <- get(c("e1", "e2")[c("e1", "e2") != env_name])
     assign("rcv", c(get("rcv", envir = snd_env), 
                     value.Y(X, envir = envir)), 
            envir = snd_env)
    }
    f$set <- function(X, Y, envir, env_name){
        assign(X, value.Y(Y, envir = envir), envir = envir)    
    }
    f$add <- function(X, Y, envir, env_name){
        assign(X, get(X, envir = envir) + value.Y(Y, envir = envir), envir = envir)
    }
    f$mul <- function(X, Y, envir, env_name){
        assign(X, get(X, envir = envir) * value.Y(Y, envir = envir), envir = envir)
    }
    f$mod <- function(X, Y, envir, env_name){
        assign(X, get(X, envir = envir) %% value.Y(Y, envir = envir), envir = envir)
    }
    f$rcv <- function(X, Y = NA, envir, env_name){
        next_rcv_i <- get("rcv_i", envir = envir) + 1
        
          if(next_rcv_i > length(get("rcv", envir = envir))){
              assign("run", F, envir = envir)
             }else{
                 assign(X, get("rcv", envir = envir)[next_rcv_i], envir = envir)
                 assign("rcv_i", next_rcv_i, envir = envir)
              }
    }
    f$jgz <- function(X, Y, envir, env_name){
        if(value.Y(X, envir = envir) > 0){
            assign("ii", (get("ii", envir = envir) + value.Y(Y, envir = envir) - 1) * 
                       as.numeric(value.Y(Y, envir = envir) != 0), envir = envir)}
    }
}
setup_env <- function(env_name, input){
    envir <- get(env_name, envir = globalenv())
    assign("ii", 1, envir = envir)
    assign("run", T, envir = envir)
    assign("rcv_i", 0, envir = envir)
    assign("rcv", NULL, envir = envir)
    lapply(unique(map(input, 2)), FUN = assign, value = 0, envir = envir)
    if(env_name == "e2"){e2$p <- 1}
}
check_end <- function(envir, input){
    next_ii <- get("ii", envir = envir) + 1
    if(next_ii %in% 1:length(input)){
        assign("ii", next_ii, envir = envir)
    }else{
        assign("run", F, envir = envir) 
    }
}
run_inst <- function(input, f) {
     cat("run:", e1$run, "e1$ii = ", e1$ii, "--- run:", e1$run, "e2$ii =", e2$ii, "\n \n")
    while(any(get("run", envir = e1), get("run", envir = e2))){
           cat("run:", e1$run, "e1$ii = ", e1$ii, "--- run:", e2$run, "e2$ii =", e2$ii, "\n \n")
        if(e1$run){            
        inst1 <- input[[e1$ii]]
          f[[inst1[1]]](inst1[2], inst1[3], e1, env_name = "e1")
          check_end(e1, input)}
          
           if(e2$run){  
        inst2 <- input[[e2$ii]]
        f[[inst2[1]]](inst2[2], inst2[3], e2, env_name = "e2")
        check_end(e2, input)
           }
        
    }
}

Test

#expect_equal(,)

deploy

e1 <- new.env(parent = emptyenv())
e2 <- new.env(parent = emptyenv())
f <- new.env(parent = emptyenv())
setup_env(env_name = "e1", input)
setup_env(env_name = "e2", input)
create_inst_frm(f)
run_inst(input, f)

My reproducible input

dput(aoc_get_input(day = 18, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
                                                 "/secrets/session_cookie.txt")))
"set i 31\nset a 1\nmul p 17\njgz p p\nmul a 2\nadd i -1\njgz i -2\nadd a -1\nset i 127\nset p 622\nmul p 8505\nmod p a\nmul p 129749\nadd p 12345\nmod p a\nset b p\nmod b 10000\nsnd b\nadd i -1\njgz i -9\njgz a 3\nrcv b\njgz b -1\nset f 0\nset i 126\nrcv a\nrcv b\nset p a\nmul p -1\nadd p b\njgz p 4\nsnd a\nset a b\njgz 1 3\nsnd b\nset f 1\nadd i -1\njgz i -11\nsnd a\njgz f -16\njgz a -19"



template based on the workflowr standalone template

LS0tCnRpdGxlOiAiLS0tIERheSAxODogRHVldCAtLS0iCmF1dGhvcjogImFubmFrcnlzdGFsbGkiCmRhdGU6IDIwMTctMTItMTgKb3V0cHV0OiBodG1sX25vdGVib29rCmVkaXRvcl9vcHRpb25zOiAKICBjaHVua19vdXRwdXRfdHlwZTogaW5saW5lCi0tLQoKYGBge3Iga25pdHItb3B0cy1jaHVuaywgaW5jbHVkZT1GQUxTRX0KIyBVcGRhdGUga25pdHIgY2h1bmsgb3B0aW9ucwojIGh0dHBzOi8veWlodWkubmFtZS9rbml0ci9vcHRpb25zLyNjaHVuay1vcHRpb25zCmtuaXRyOjpvcHRzX2NodW5rJHNldCgKICBjb21tZW50ID0gTkEsCiAgZmlnLmFsaWduID0gImNlbnRlciIsCiAgdGlkeSA9IEZBTFNFLAogIGZpZy5wYXRoID0gcGFzdGUwKCJmaWd1cmUvIiwga25pdHI6OmN1cnJlbnRfaW5wdXQoKSwgIi8iKQopCmBgYAoKYGBge3IgbGFzdC11cGRhdGVkLCBlY2hvPUZBTFNFLCByZXN1bHRzPSdhc2lzJ30KIyBJbnNlcnQgdGhlIGRhdGUgdGhlIGZpbGUgd2FzIGxhc3QgdXBkYXRlZApjYXQoc3ByaW50ZigiKipMYXN0IHVwZGF0ZWQ6KiogJXMiLCBTeXMuRGF0ZSgpKSkKYGBgCgpgYGB7ciBjb2RlLXZlcnNpb24sIGVjaG89RkFMU0UsIHJlc3VsdHM9J2FzaXMnfQojIEluc2VydCB0aGUgY29kZSB2ZXJzaW9uIChHaXQgY29tbWl0IFNIQTEpIGlmIEdpdCByZXBvc2l0b3J5IGV4aXN0cyBhbmQgUgojIHBhY2thZ2UgZ2l0MnIgaXMgaW5zdGFsbGVkCmlmKHJlcXVpcmVOYW1lc3BhY2UoImdpdDJyIiwgcXVpZXRseSA9IFRSVUUpKSB7CiAgaWYoZ2l0MnI6OmluX3JlcG9zaXRvcnkoKSkgewogICAgY29kZV92ZXJzaW9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ciA9IGVudmlyKSwgZW52aXIgPSBlbnZpcikKICAgIH0KICAgIGYkbW9kIDwtIGZ1bmN0aW9uKFgsIFksIGVudmlyLCBlbnZfbmFtZSl7CiAgICAgICAgYXNzaWduKFgsIGdldChYLCBlbnZpciA9IGVudmlyKSAlJSB2YWx1ZS5ZKFksIGVudmlyID0gZW52aXIpLCBlbnZpciA9IGVudmlyKQogICAgfQogICAgZiRyY3YgPC0gZnVuY3Rpb24oWCwgWSA9IE5BLCBlbnZpciwgZW52X25hbWUpewogICAgICAgIG5leHRfcmN2X2kgPC0gZ2V0KCJyY3ZfaSIsIGVudmlyID0gZW52aXIpICsgMQogICAgICAgIAogICAgICAgICAgaWYobmV4dF9yY3ZfaSA+IGxlbmd0aChnZXQoInJjdiIsIGVudmlyID0gZW52aXIpKSl7CiAgICAgICAgICAgICAgYXNzaWduKCJydW4iLCBGLCBlbnZpciA9IGVudmlyKQogICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICAgYXNzaWduKFgsIGdldCgicmN2IiwgZW52aXIgPSBlbnZpcilbbmV4dF9yY3ZfaV0sIGVudmlyID0gZW52aXIpCiAgICAgICAgICAgICAgICAgYXNzaWduKCJyY3ZfaSIsIG5leHRfcmN2X2ksIGVudmlyID0gZW52aXIpCiAgICAgICAgICAgICAgfQogICAgfQogICAgZiRqZ3ogPC0gZnVuY3Rpb24oWCwgWSwgZW52aXIsIGVudl9uYW1lKXsKICAgICAgICBpZih2YWx1ZS5ZKFgsIGVudmlyID0gZW52aXIpID4gMCl7CiAgICAgICAgICAgIGFzc2lnbigiaWkiLCAoZ2V0KCJpaSIsIGVudmlyID0gZW52aXIpICsgdmFsdWUuWShZLCBlbnZpciA9IGVudmlyKSAtIDEpICogCiAgICAgICAgICAgICAgICAgICAgICAgYXMubnVtZXJpYyh2YWx1ZS5ZKFksIGVudmlyID0gZW52aXIpICE9IDApLCBlbnZpciA9IGVudmlyKX0KICAgIH0KfQoKc2V0dXBfZW52IDwtIGZ1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G4gXG4iKQogICAgICAgIGlmKGUxJHJ1bil7ICAgICAgICAgICAgCiAgICAgICAgaW5zdDEgPC0gaW5wdXRbW2UxJGlpXV0KICAgICAgICAgIGZbW2luc3QxWzFdXV0oaW5zdDFbMl0sIGluc3QxWzNdLCBlMSwgZW52X25hbWUgPSAiZTEiKQogICAgICAgICAgY2hlY2tfZW5kKGUxLCBpbnB1dCl9CiAgICAgICAgICAKICAgICAgICAgICBpZihlMiRydW4peyAgCiAgICAgICAgaW5zdDIgPC0gaW5wdXRbW2UyJGlpXV0KICAgICAgICBmW1tpbnN0MlsxXV1dKGluc3QyWzJdLCBpbnN0MlszXSwgZTIsIGVudl9uYW1lID0gImUyIikKICAgICAgICBjaGVja19lbmQoZTIsIGlucHV0KQogICAgICAgICAgIH0KICAgICAgICAKCiAgICB9Cn0KCgoKCgpgYGAKCiMjIFRlc3QKYGBge3J9CiNleHBlY3RfZXF1YWwoLCkKYGBgCgoKIyMgZGVwbG95CgpgYGB7cn0KZTEgPC0gbmV3LmVudihwYXJlbnQgPSBlbXB0eWVudigpKQplMiA8LSBuZXcuZW52KHBhcmVudCA9IGVtcHR5ZW52KCkpCmYgPC0gbmV3LmVudihwYXJlbnQgPSBlbXB0eWVudigpKQpzZXR1cF9lbnYoZW52X25hbWUgPSAiZTEiLCBpbnB1dCkKc2V0dXBfZW52KGVudl9uYW1lID0gImUyIiwgaW5wdXQpCmNyZWF0ZV9pbnN0X2ZybShmKQpydW5faW5zdChpbnB1dCwgZikKCmBgYAoKCiMjIE15IHJlcHJvZHVjaWJsZSBpbnB1dApgYGB7cn0KZHB1dChhb2NfZ2V0X2lucHV0KGRheSA9IDE4LCBjb29raWVfcGF0aCA9IHBhc3RlMChycHJvanJvb3Q6OmZpbmRfcnN0dWRpb19yb290X2ZpbGUoKSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICIvc2VjcmV0cy9zZXNzaW9uX2Nvb2tpZS50eHQiKSkpCmBgY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