My 2018 Solutions

Day 1

Part 1

input <- readLines("2018/01-input")
eval(parse(text = paste(input, collapse = "")))
[1] 425
0.006 sec elapsed

Part 2

input <- readLines("2018/01-input")

input <- as.integer(input)
input_len <- length(input)

res <- list()

total <- 0

i <- 0
repeat {
  total <- total + input[i %% input_len + 1]
  if (is.null(res[as.character(total)][[1]])) {
    res[as.character(total)] <- 1
  } else {
    break
  }
  i <- i + 1
}
total
[1] 57538
464.892 sec elapsed

Day 2

Part 1

input <- readLines("2018/02-input")

chars <- strsplit(input, "")
x <- input[[1]]

count_2 <- function(x) {
  any(table(x) == 2)
}

count_3 <- function(x) {
  any(table(x) == 3)
}

sum(purrr::map_lgl(chars, count_2)) * sum(purrr::map_lgl(chars, count_3))
[1] 6696
0.052 sec elapsed

Part 2

input <- readLines("2018/02-input")

dist <- stringdist::stringdistmatrix(input, input)

matched_strings <- input[which(dist == 1) %% length(input)]

matched_letters <- strsplit(matched_strings, "")

paste(matched_letters[[1]][matched_letters[[1]] == matched_letters[[2]]], collapse = "")
[1] "bvnfawcnyoeyudzrpgslimtkj"
0.215 sec elapsed

Day 3

Part 1

library(stringr)

input <- readLines("2018/03-input")

data <- str_match(input, "(?<id>[0-9]+) @ (?<left>[0-9]+),(?<top>[0-9]+): (?<width>[0-9]+)x(?<height>[0-9]+)")[,-1]
data <- apply(data, 2, as.numeric)

grid <- matrix(0, 1000, 1000)

for (i in seq_len(nrow(data))) {
  xs <- data[i, "left"] + seq_len(data[i, "width"])
  ys <- data[i, "top"] + seq_len(data[i, "height"])
  grid[xs, ys] <- grid[xs, ys] + 1
}

sum(grid >= 2)
[1] 111935
0.034 sec elapsed

Part 2

library(stringr)

input <- readLines("2018/03-input")

data <- str_match(input, "(?<id>[0-9]+) @ (?<left>[0-9]+),(?<top>[0-9]+): (?<width>[0-9]+)x(?<height>[0-9]+)")[,-1]
data <- apply(data, 2, as.numeric)

grid <- matrix(0, 1000, 1000)

for (i in seq_len(nrow(data))) {
  xs <- data[i, "left"] + seq_len(data[i, "width"])
  ys <- data[i, "top"] + seq_len(data[i, "height"])
  if (all(grid[xs, ys] == 1)) break
}

data[i, "id"]
  id 
1327 
0.042 sec elapsed

Day 4

Part 1

library(stringr)
input <- readLines("2018/04-input") |> sort()

times <- str_extract(input, "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}") |>
  as.POSIXct()

inst <- str_remove(input, "\\[.*\\] ")

guard_id <- str_extract(inst, "\\d+")

shift <- cumsum(!is.na(guard_id))

for (i in seq_along(guard_id)) {
  if (i == 1) next

  if (is.na(guard_id[i])) {
    guard_id[i] <- guard_id[i - 1]
  }
}

library(dplyr)

sleep_calc <- function(x) {
  rowSums(outer(0:59, lubridate::minute(x)[-1], ">=")) %% 2
}

sleep_sched <- tibble(shift, guard_id, inst, times) %>%
  group_by(guard_id, shift) %>%
  summarise(
    minute = 0:59,
    slept = sleep_calc(times), .groups = "drop")

sleep_sched %>%
  group_by(guard_id) %>%
  summarise(sum = sum(slept)) %>%
  arrange(desc(sum)) %>%
  slice(1) %>%
  left_join(sleep_sched, by = "guard_id") %>%
  filter(slept == 1) %>%
  count(guard_id, minute, sort = TRUE) %>%
  slice(1) %>%
  mutate(res = as.integer(guard_id) * minute) %>%
  pull(res)
[1] 103720
0.325 sec elapsed

Part 2

library(stringr)
input <- readLines("2018/04-input") |> sort()

times <- str_extract(input, "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}") |>
  as.POSIXct()

inst <- str_remove(input, "\\[.*\\] ")

guard_id <- str_extract(inst, "\\d+")

shift <- cumsum(!is.na(guard_id))

for (i in seq_along(guard_id)) {
  if (i == 1) next

  if (is.na(guard_id[i])) {
    guard_id[i] <- guard_id[i - 1]
  }
}

library(dplyr)

sleep_calc <- function(x) {
  rowSums(outer(0:59, lubridate::minute(x)[-1], ">=")) %% 2
}

sleep_sched <- tibble(shift, guard_id, inst, times) %>%
  group_by(guard_id, shift) %>%
  summarise(
    minute = 0:59,
    slept = sleep_calc(times), .groups = "drop")

sleep_sched %>%
  filter(slept == 1) %>%
  count(guard_id, minute, sort = TRUE) %>%
  slice(1) %>%
  mutate(res = as.integer(guard_id) * minute) %>%
  pull(res)
[1] 110913
0.07 sec elapsed

Day 5

Part 1

library(stringr)

input <- readLines("2018/05-input")

voids <- c(paste0(letters, LETTERS), paste0(LETTERS, letters))

void_regex <- paste0("(", paste(voids, collapse = "|"), ")")

new <- old <- input

repeat {
  new <- str_remove_all(old, void_regex)
  if (new == old) break
  old <- new
}

nchar(new)
[1] 9078
12.245 sec elapsed

Part 2

library(stringr)

input <- readLines("2018/05-input")

voids <- c(paste0(letters, LETTERS), paste0(LETTERS, letters))

void_regex <- paste0("(", paste(voids, collapse = "|"), ")")

reduce_poly <- function(x, string) {
  string <- str_remove_all(string, x)
  new <- old <- string
  repeat {
    new <- str_remove_all(old, void_regex)
    if (new == old) break
    old <- new
  }

  nchar(new)
}

purrr::map_int(paste0("(", letters, "|", LETTERS, ")"), reduce_poly, input) |>
  min()
[1] 5698
302.518 sec elapsed

Day 6

Part 1

library(dplyr)
library(stringr)

input <- readLines("2018/06-input")

point <- str_match(input, "(?<x>[0-9]+), (?<y>[0-9]+)")[,-1]
point <- apply(point, 2, as.numeric)

make_range <- function(x) {
  r <- range(point[,"x"])
  seq(r[1]- 100, r[2] + 100)
}

grid <- expand.grid(X = make_range(point[,"x"]), Y = make_range(point[,"y"]))

classify <- function(x, y) {

  dists <- abs(point[, "x"] - x) + abs(point[, "y"] - y)

  which_min <- which(dists == min(dists))

  if (length(which_min) != 1) {
    return(0)
  } else {
    return(which_min)
  }
}

grid <- grid %>%
  mutate(class = purrr::map2_dbl(X, Y, classify))

border_grid <- grid %>%
  filter(X == min(X) | X == max(X) | Y == min(Y) | Y == max(Y))

border_classes <- border_grid %>%
  pull(class) %>%
  unique()

grid %>%
  filter(!class %in% border_classes) %>%
  count(class, sort = TRUE) %>%
  slice(1) %>%
  pull(n)
[1] 4754
1.995 sec elapsed

Part 2

library(dplyr)
library(stringr)

input <- readLines("2018/06-input")

point <- str_match(input, "(?<x>[0-9]+), (?<y>[0-9]+)")[,-1]
point <- apply(point, 2, as.numeric)

make_range <- function(x) {
  r <- range(point[,"x"])
  seq(r[1]- 100, r[2] + 100)
}

grid <- expand.grid(X = make_range(point[,"x"]), Y = make_range(point[,"y"]))

classify <- function(x, y) {

  dists <- abs(point[, "x"] - x) + abs(point[, "y"] - y)

  which_min <- which(dists == min(dists))

  if (length(which_min) != 1) {
    return(0)
  } else {
    return(which_min)
  }
}

grid <- grid %>%
  mutate(class = purrr::map2_dbl(X, Y, classify))

classify2 <- function(x, y) {
  dists <- abs(point[, "x"] - x) + abs(point[, "y"] - y)

  sum(dists) < 10000
}

grid %>%
  mutate(class2 = purrr::map2_dbl(X, Y, classify2)) %>%
  filter(class2 == 1) %>%
  nrow()
[1] 42344
3.257 sec elapsed

Day 7

Part 1

library(stringr)

input <- readLines("2018/07-input")

from <- str_sub(input, 6, 6)
to <- str_sub(input, 37, 37)

letters <- c(from, to) |> unique() |> sort()

graph <- purrr::map(letters, ~character(0)) |>
  setNames(letters)

for (i in seq_along(from)) {
  graph[[to[i]]] <- c(graph[[to[i]]], from[i])
}

res <- character()

repeat {
  selection <- names(which.min(lengths(graph)))[1]

  res <- c(res, selection)

  graph <- graph[names(graph) != selection]

  if (length(graph) == 0) break

  graph <- purrr::map(graph, setdiff, selection)
}

paste0(res, collapse = "")
[1] "CABDFE"
0.012 sec elapsed

Day 8

Day 9

Day 10

Day 11

Day 12

Day 13

Day 14

Day 15

Day 16

Day 17

Day 18

Day 19

Day 20

Day 21

Day 22

Day 23

Day 24

Day 25