spin <- function(x, i) {
i <- as.numeric(i)
c(tail(x, i), head(x, -i))
}
exchange <- function(x, i, j) {
i <- as.numeric(i)
j <- as.numeric(j)
ox <- x
x[i+1] <- ox[j+1]
x[j+1] <- ox[i+1]
x
}
partner <- function(x, a, b) {
i <- match(a, x)
j <- match(b, x)
exchange(x, i-1, j-1)
}
puzzle_input <- readLines("advent-data/2017-12-16-advent-day16.txt")
parse_input <- function(input) {
ii <- strsplit(input, ",")[[1]]
data_frame(
move = substr(ii, 1, 1),
coords = substr(ii, 2, nchar(ii))
) %>%
mutate(coords_split = map(coords, ~ strsplit(., "/")[[1]])) %>%
mutate(fxn = case_when(
move == "s" ~ "spin",
move == "x" ~ "exchange",
move == "p" ~ "partner"
))
}
dance <- function(input) {
inst <- parse_input(input)
init <- letters[1:16]
for (i in seq_len(nrow(inst))) {
init <- invoke_map(inst$fxn[i], inst$coords_split[i], x = init)[[1]]
}
init
}
paste(dance(puzzle_input), collapse = "")
Comments