knitr::opts_chunk$set(
  collapse = TRUE,
  comment = ""
)
library(adventofcode21)

Day 1

Compute sum of positive differences between values:

x <- read.table(system.file("input01.txt", package = "adventofcode21"))[[1]]
f01a(x)

Compute sum of positive differences between sums of three consecutive values:

f01b(x)

Day 2

x <- read.table(system.file("input02.txt", package = "adventofcode21"),
                col.names = c("direction", "amount"))
head(x)

Compute horizontal location and depth

position <- f02a(x$direction, x$amount)
position

Compute product

prod(position)

Compute horizontal location and depth based on aim

position <- f02b(x$direction, x$amount)
position

Compute product

prod(position)

Day 3

Gamma and epsilon rate:

x <- read.fwf(system.file("input03.txt", package = "adventofcode21"), 
              widths = rep(1, 12))
x <- as.matrix(x)
rates <- f03a(x)
rates

Power consumption

prod(rates)

Oxygen generator rating and CO2 scrubber rating

rating <- f03b(x)
rating
prod(rating)

Day 4

x <- list()
x[[1]] <- scan(system.file("input04.txt", package = "adventofcode21"), 
               sep = ",", nlines = 1)
x[[2]] <- list()
for (i in 1:1000){
   res <- 
    try(as.matrix(read.fwf(system.file("input04.txt", 
                                       package = "adventofcode21"), 
                           widths = rep(3, 5), skip = 2 + (i - 1)*6, n = 5)),
        silent = TRUE)
   if (!inherits(res, "try-error")) {
     x[[2]][[i]] <- res
   } else break
}
prod(f04a(x[[1]], x[[2]]))
prod(f04b(x[[1]], x[[2]]))

Day 5

x <- read.table(system.file("input05.txt", 
                            package = "adventofcode21"),
                sep = ",", col.names = c("x1", "y1x2", "y2"))
x$y1 <- as.numeric(gsub("([0-9]*)[ ].*", "\\1", x$y1x2))
x$x2 <- as.numeric(gsub("[0-9]*[ ]->[ ]([0-9]*)", "\\1", x$y1x2))
f05a(x$x1, x$y1, x$x2, x$y2)

Day 6

x <- scan(system.file("input06.txt", package = "adventofcode21"),sep = ',')
f06a(x,80)
res <- f06a(x,256)
format(res, digits = 15)

Day 7

x <- scan(system.file("input07.txt", package = "adventofcode21"),sep = ',')
f07a(x)
f07b(x)

Day 8

x <- as.matrix(read.table(system.file("input08.txt", package = "adventofcode21"),sep = ' '))[,-11]
f08a(x)
f08b(x)

Day 9

x <- as.matrix(read.fwf(system.file("input09.txt", package = "adventofcode21"), widths = rep(1, 100)))

Find low points

low <- f09a(x)
sum(1 + x[low])

Plot basins

basins <- x == 9
mode(basins) <- "numeric"
image(1:ncol(x), 1:nrow(x), t(basins[nrow(basins):1,]))

Find product of sizes of the 3 largest basins:

f09b(x, low) # ~2s

Alternative with floodFill from EBImage (Bioconductor package) - uses fast scanning algorithm implemented in C

library(EBImage)
n <- nrow(low)
size <- numeric(n)
for (i in seq_len(n)){ # ~ 0.03s
  size[i] <- sum(floodFill(basins, low[i, ], 2) == 2)
}
prod(sort(size, decreasing = TRUE)[1:3]) 

Day 10

x <- scan(system.file("input10.txt", package = "adventofcode21"),
          "character")
sum(f10a(x))
f10b(x)

Day 11

x <- as.matrix(read.fwf(system.file("input11.txt", package = "adventofcode21"),
                        widths = rep(1, 10)))
f11a(x, 100)
f11b(x)

Day 12

x <- read.table(system.file("input12.txt", 
                            package = "adventofcode21"), sep = "-")
names(x) <- c("from", "to")
f12a(x)
f12b(x)

Day 13

coord <- read.table(system.file("input13.txt", package = "adventofcode21"), 
                    sep = ",", nrows = 853)
instructions <- scan(system.file("input13.txt", package = "adventofcode21"), 
                     what = "character", skip = 854, sep = "\n")
f13a(coord, instructions)
x <- f13b(coord, instructions)
image(t(x)[,nrow(x):1])

Day 14

template <- readLines(system.file("input14.txt", package = "adventofcode21"), 
                      n = 1)
code <- scan(system.file("input14.txt", package = "adventofcode21"), 
             what = "character", skip = 2, sep = "\n")
template <- strsplit(template, "")[[1]]
code <- structure(gsub(".*[ ]([A-Z])", "\\1", code), 
                  names = gsub("([A-Z]+).*", "\\1", code))
f14a(template, code)
res <- f14b(template, code, 40)
format(res, digits = 13)

Day 15

x <- read.fwf(system.file("input15.txt", 
                          package = "adventofcode21"), 
              widths = rep(1, 100))
x <- as.matrix(x)
f15a(x)
f15a(f15b(x))

Day 16

x <- readLines(system.file("input16.txt", 
                          package = "adventofcode21"))
f16a(x)$version_sum
res <- f16a(x)$value
format(res, digits = 13)

Day 17

x <- readLines(system.file("input17.txt", package = "adventofcode21"), 
               n = 1)
max(f17a(x))
length(f17b(x))

Day 18

x <- readLines(system.file("input18.txt", package = "adventofcode21"))
f18a(x)
library(utils)
pairs <- combn(x, 2)
n <- ncol(pairs)
res1 <- res2 <- numeric(n)
for (i in 1:n){
  res1[i] <- f18a(pairs[,i])
}
for (i in 1:n){
  res2[i] <- f18a(rev(pairs[,i]))
}
max(c(res1, res2))

Day 19

# find beginnings
x <- readLines(system.file("input19.txt", package = "adventofcode21"))
start <- grep("^---.*", x)
end <- c(start[-1] - 2, length(x))
res <- vector(mode = "list", length = length(start))
for (i in seq_along(res)){
  res[[i]] <- as.matrix(read.table(system.file("input19.txt", 
                                         package = "adventofcode21"), 
                                   sep = ",", skip = start[i], 
                                   nrows = end[i] - start[i]))
}
out <- f19a(res)
nrow(out$beacons)
max(dist(out$scanners, method = "manhattan"))

Day 20

code <- read.fwf(system.file("input20.txt", package = "adventofcode21"), 
                 n = 1,
                 widths = rep(1, 512), comment.char = "")
code <- unlist(unname(code))
code <- as.numeric(code == "#")
x <- as.matrix(read.fwf(system.file("input20.txt", package = "adventofcode21"), 
              skip = 2, widths = rep(1, 100), comment.char = ""))
x <- x == "#"
mode(x) <- "numeric"
f20a(code, x, steps = 2)
f20a(code, x, steps = 50)

Day 21

input <- readLines(system.file("input21.txt", package = "adventofcode21"))
pos <- as.numeric(gsub("[^:]+: ([0-9])", "\\1", input))
f21a(pos[1], pos[2])
out <- f21b(pos[1], pos[2])
format(max(out), digits = 15)

Day 22

input <- readLines(system.file("input22.txt", package = "adventofcode21"), 
                   n = 20)
x <- gsub("([^x]+)(x.*)", "list(\\2)", input)
x <- paste0("list(", 
            paste(gsub("..", ":", x, fixed = TRUE), collapse = ","),
            ")")
x <- eval(parse(text = x))
on <- gsub("([^ ]+)( .*)", "\\1", input) == "on"
f22a(x, on)

Day 24

input <- readLines(system.file("input24.txt", package = "adventofcode21"))
start <- grep("inp", input)
end <- c(start[-1] - 1, length(input))
prog <- list()
for (i in seq_len(14)) prog[[i]] <- input[start[i]:end[i]]

Day 25

input <- read.fwf(system.file("input25.txt", package = "adventofcode21"),
                  rep(1, 139))
input <- as.matrix(input)
f25a(input)


hturner/adventofcode21 documentation built on Jan. 14, 2022, 7:03 a.m.