2 minute read

Only 1 star for me today :(

Part 1

library(readr)

.get <- function(x, envir) {
    if (exists(x, envir = envir))
        get(x, envir = envir)
    else 0
}

snd <- function(note, ..., e) {
    freq <- .get(note, envir = e)
    res <- setNames(freq, note)
    assign("last_played", res, envir = e)
}

set <- function(note, freq, e) {
    freq <- guess_num(freq, e)
    assign(note, freq, envir = e)
}

guess_num <- function(freq, e) {
    if (!grepl("[0-9]", freq)) {
        freq <- .get(freq, envir = e)
    } else freq <- as.numeric(freq)
    freq
}

add <- function(note, freq, e) {
    res <- .get(note, envir = e)
    freq <- guess_num(freq, e)
    set(note, res + freq, e)
}

mul <- function(note, freq, e) {
    res <- .get(note, envir = e)
    freq <- guess_num(freq, e)
    set(note, res * freq, e)
}

mod <- function(note, freq, e) {
    res <- .get(note, envir = e)
    freq <- guess_num(freq, e)
    set(note, res %% freq, e)
}

rcv <- function(note, ..., e) {
    if (note > 0)
        .get("last_played", envir = e)
}


play_duet <- function(input) {
    i <- 1
    e <- new.env()
    while (i <= nrow(input)) {
        if (input$instruction[i] == "jgz") {
            if (.get(input$note[i], envir = e) > 0) {
                i <- i + guess_num(input$freq[i])
                next
            } else {
                i <- i + 1
                next
            }
        }

        if (input$instruction[i] == "rcv" &&
            .get(input$note[i], envir = e) > 0) {
            return(get("last_played", envir = e))
        }

        invoke_map(input$instruction[i],
                   input$note[i], input$freq[i], e = e)
        i <- i + 1
    }
    
}

input <- read_delim("advent-data/2017-12-18-test-data.txt",
                    delim = " ", col_names = FALSE) %>%
    set_names(c("instruction", "note", "freq")) 
## Parsed with column specification:
## cols(
##   X1 = col_character(),
##   X2 = col_character(),
##   X3 = col_character()
## )
## Warning: 2 parsing failures.
## row # A tibble: 2 x 5 col     row   col  expected    actual                                   file expected   <int> <chr>     <chr>     <chr>                                  <chr> actual 1     5  <NA> 3 columns 2 columns 'advent-data/2017-12-18-test-data.txt' file 2     7  <NA> 3 columns 2 columns 'advent-data/2017-12-18-test-data.txt'
puzzle_input <- read_delim("advent-data/2017-12-18-data.txt",
                           delim = " ", col_names = FALSE) %>%
    set_names(c("instruction", "note", "freq"))
## Parsed with column specification:
## cols(
##   X1 = col_character(),
##   X2 = col_character(),
##   X3 = col_character()
## )
## Warning in rbind(names(probs), probs_f): number of columns of result is not
## a multiple of vector length (arg 1)
## Warning: 7 parsing failures.
## row # A tibble: 5 x 5 col     row   col  expected    actual                              file expected   <int> <chr>     <chr>     <chr>                             <chr> actual 1    18  <NA> 3 columns 2 columns 'advent-data/2017-12-18-data.txt' file 2    22  <NA> 3 columns 2 columns 'advent-data/2017-12-18-data.txt' row 3    26  <NA> 3 columns 2 columns 'advent-data/2017-12-18-data.txt' col 4    27  <NA> 3 columns 2 columns 'advent-data/2017-12-18-data.txt' expected 5    32  <NA> 3 columns 2 columns 'advent-data/2017-12-18-data.txt'
## ... ................. ... ................................................................... ........ ................................................................... ...... ................................................................... .... ................................................................... ... ................................................................... ... ................................................................... ........ ...................................................................
## See problems(...) for more details.
play_duet(input)
## a 
## 4
play_duet(puzzle_input)
##    b 
## 7071

Comments