Last updated: 2017-12-16
Code version: aa59a2a
See more puzzles
Advent of Code
Brief
Suddenly, a scheduled job activates the system’s disk defragmenter. Were the situation different, you might sit and watch it for a while, but today, you just don’t have that kind of time. It’s soaking up valuable system resources that are needed elsewhere, and so the only option is to help it finish its task as soon as possible.
The disk in question consists of a 128x128 grid; each square of the grid is either free or used. On this disk, the state of the grid is tracked by the bits in a sequence of knot hashes.
A total of 128 knot hashes are calculated, each corresponding to a single row in the grid
- each hash contains 128 bits which correspond to individual grid squares.
- Each bit of a hash indicates whether that square is free (0) or used (1).
Let’s go
Packages & functions
library(tidyverse)
library(testthat)
library(aocodeR)
library(sodium)
library(BMS)
Functions
knot <- function(l.input = input, v = 0:255,
skip = 0, cp = 1, cycles = 1, hex = F) {
if(hex){l.input <- l.input %>% hex.l} else{
l.input <- l.input %>% strsplit(",") %>% unlist %>% as.numeric
}
# intitialise algorithm
lv <- length(v)
for(i in rep(1:length(l.input), cycles)){
l <- l.input[i]
# twist
tc<- cp:(cp - 1 + l) %% lv %>% recode(`0` = lv)
v[tc] <- v[rev(tc)]
# move cp
cp <- (cp + l + skip) %% lv
if (cp == 0){cp <- lv} #fix %% 0s
# update params
skip <- skip + 1
i <- i + 1
}
v
}
hex.l <- function(input= "1,2,3") {
c(utf8ToInt(input) , c(17, 31, 73, 47, 23))
}
hash <- function(input, ...) {
input %>%
knot(v = 0:255, cycles = 64, hex = T) %>%
split(., ceiling(seq_along(.)/16)) %>%
map_int(~reduce(.x, bitwXor)) %>%
as.hexmode %>%
paste(collapse = "")
}
key2sum <- function(x) {
x %>% hash %>% paste(collapse = "") %>% BMS::hex2bin() %>% sum
}
sq_used <- function(key_string) {
key_string %>% paste(0:127, sep = "-") %>% map(key2sum) %>% unlist %>% sum
}
Test
There was an error in the example and example output needed the last 0000
s trimming
expect_equal(BMS::hex2bin("a0c2017 ") %>% paste(collapse = ""), "1010000011000010000000010111")
#expect_equal(sq_used("flqrgnkx"), 8186)
deploy
sq_used(input)
[1] 8292
—- Part 2 —-
Now, all the defragmenter needs to know is the number of regions. A region is a group of used squares that are all adjacent, not including diagonals. Every used square is in exactly one region: lone used squares form their own isolated regions, while several adjacent squares all count as a single region.
In the example above, the following nine regions are visible, each marked with a distinct digit:
11.2.3..–> .1.2.3.4
….5.6.
7.8.55.9
.88.5…
88..5..8
.8…8..
88.8.88.–> | |
V V
Of particular interest is the region marked 8; while it does not appear contiguous in this small view, all of the squares marked 8 are connected when considering the whole 128x128 grid. In total, in this example, 1242 regions are present.
How many regions are present given your key string?
Brief
Let’s go
#m <- matrix(c(1, 0, 1, 1, 1, 1, 1,0, 1, 1, 0, 1), ncol = 4, byrow = T)
key2bin <- function(x) {
x %>% hash %>% paste(collapse = "") %>% BMS::hex2bin()
}
key2mat <- function(key_string) {
key_string %>% paste(0:128, sep = "-") %>% map(key2bin) %>% do.call("rbind", .)
}
group_matrix <- function(m) {
mg <- m
mg[,] <- 0
xs <- rep(1:nrow(m), each = ncol(m))
ys <- rep(1:ncol(m), times = nrow(m))
#length(xs)
for(i in 1:161){
loc <- c(xs[i], ys[i])
g0 <- max(mg) + 1
if(m[loc[1], loc[2]] == 0){
next
}
x <- loc[1] -1
y <- loc[2] -1
gx <- if(x == 0){0}else{mg[x, loc[2]]}
gy <- if(y == 0){0}else{mg[loc[1], y]}
if(gx == 0 & gy == 0){
mg[loc[1], loc[2]] <- g0
}else{
min_g <- min(c(gx,gy) %>% magrittr::extract(. > 0))
m_i <- m
m_i[,] <- F
if(gx != 0){
m_i <- m_i | (mg == gx)
}
if(gy != 0){
m_i <- m_i | (mg == gy)
}
m_i[loc[1], loc[2]] <- T
mg[m_i] <- min_g
}
}
mg
}
no_groups <- function(mg) {
mg %>% as.vector %>% unique %>% magrittr::extract(. > 0) %>% length
}
Test
m %>% group_matrix %>% no_groups
[1] 1242
deploy
input %>% key2mat %>% group_matrix %>% no_groups
[1] 1069
LS0tCnRpdGxlOiAiLS0tIERheSAxNDogRGlzayBEZWZyYWdtZW50YXRpb24gLS0t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m9yKGkgaW4gMTpsZW5ndGgoeHMpKXsKICAgICAgICBsb2MgPC0gYyh4c1tpXSwgeXNbaV0pICAgCiAgICAgICAgZzAgPC0gbWF4KG1nKSArIDEKICAgICAgICBpZihtW2xvY1sxXSwgbG9jWzJdXSA9PSAwKXsKICAgICAgICAgICAgbmV4dAogICAgICAgIH0KICAgICAgICB4IDwtIGxvY1sxXSAtMQogICAgICAgIHkgPC0gbG9jWzJdIC0xCiAgICAgICAgZ3ggPC0gaWYoeCA9PSAwKXswfWVsc2V7bWdbeCwgbG9jWzJdXX0KICAgICAgICBneSA8LSBpZih5ID09IDApezB9ZWxzZXttZ1tsb2NbMV0sIHldfQogICAgICAgIGlmKGd4ID09IDAgJiBneSA9PSAwKXsKICAgICAgICAgICAgbWdbbG9jWzFdLCBsb2NbMl1dIDwtIGcwCiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIG1pbl9nIDwtIG1pbihjKGd4LGd5KSAlPiUgbWFncml0dHI6OmV4dHJhY3QoLiA+IDApKQogICAgICAgICAgICBtX2kgPC0gbSAgIAogICAgICAgICAgICBtX2lbLF0gPC0gRgogICAgICAgICAgICBpZihneCAhPSAwKXsKICAgICAgICAgICAgICAgIG1faSA8LSBtX2kgfCAobWcgPT0gZ3gpCiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoZ3kgIT0gMCl7CiAgICAgICAgICAgICAgICBtX2kgPC0gbV9pIHwgKG1nID09IGd5KQogICAgICAgICAgICB9CiAgICAgICAgICAgIG1faVtsb2NbMV0sIGxvY1syXV0gPC0gVAogICAgICAgICAgICBtZ1ttX2ldIDwtIG1pbl9nCiAgICAgICAgfQogICAgICAgIAogICAgfQogICAgbWcKfQoKbm9fZ3JvdXBzIDwtIGZ1bmN0aW9uKG1nKSB7CiAgICBtZyAlPiUgYXMudmVjdG9yICU+JSB1bmlxdWUgJT4lIG1hZ3JpdHRyOjpleHRyYWN0KC4gPiAwKSAlPiUgbGVuZ3RoCn0KYGBgCgojIyBUZ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