To install `mistlecode` yourself, run `devtools::install_github('guslipkin/mistlecode')`.
Also loading: cipheR data.table dplyr purrr slider stringr tidyverse
2022-10: Cathode-Ray Tube
Oh. This looks a lot like last year’s day one.
Part 1
Part 1 wasn’t too bad. I was a really big dummy and forgot to download the test input then was trying to check my real input as the test input which didn’t work. Other than that, I’m really happy I got to use mistlecode::expand_directions
which I only wrote a few hours prior. I did have to make some live modifications, but nothing crazy. Once everything was mostly right, I had a problem where the first five signals were correct, but the last wasn’t. I eventually realized that I needed to lead the add values and that got everything sorted.
dt <-
dt |>
mutate("mag" = ifelse(X1 == "noop", 1, 2)) |>
select("dir" = "X1", "mag", "X2") |>
mutate(ins = row_number()) |>
expand_directions(preserve_data = TRUE) |>
data.table()
dt$dup = duplicated(dt$ins, fromLast = TRUE)
dt$X2 = ifelse(dt$dup, NA, dt$X2)
dt$X3 <- lead(dt$X2, 1)
sapply(c(20, 60, 100, 140, 180, 220), \(x) {
sum(1, sum(dt[1:x, "X3"], na.rm = TRUE)) * x
}) |>
sum()
[1] 14260
Part 2
This was an absolute mess. I didn’t understand the question at all and still don’t really get it. The difference between what I had and what I thought I should have made the solution relatively clear, but only after struggling with it for hours. I do like ggplot-able solutions though. Those are always nice.
m <- matrix(" ", 6, 40)
dt$X2 <- ifelse(is.na(dt$X2), 0, dt$X2)
dt$cumsum <- sapply(1:240, \(x) { sum(1, sum(dt[1:x, "X2"], na.rm = TRUE)) })
m <-
sapply(1:240, \(x) {
forty <- (x %/% 40)
cumsum <- c(dt$cumsum[x] - 1, dt$cumsum[x], dt$cumsum[x] + 1)
sprite_pos <- cumsum[!is.na(cumsum)]
if (any((sprite_pos + (40 * forty)) == x)) {
sprite_pos <- suppressWarnings(min(which(sprite_pos == x), na.rm = TRUE))
if (sprite_pos == 1) { sprite_pos <- cumsum[1] }
else if (sprite_pos == 3) { sprite_pos <- cumsum[3] }
else { sprite_pos <- cumsum[2] }
return(sprite_pos + (40 * forty))
}
return(NA)
})
m[240] <- 0
matrix_to_coords(matrix(m, 6, 40, byrow = TRUE)) |>
data.frame() |>
filter(!is.na(data)) |>
mutate(col = ifelse(col == 40, 0, col)) |>
ggplot() +
geom_point(aes(x = col, y = -row), size = 5, shape = 15) +
coord_fixed()