My 2017 Solutions

Day 1

Part 1

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

chars <- as.numeric(strsplit(input, "")[[1]])

sum(chars[chars == c(chars[-1], chars[1])])
[1] 1044
0.002 sec elapsed

Part 2

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

chars <- as.numeric(strsplit(input, "")[[1]])

len <- seq_len(length(chars)/2)

sum(chars[chars == c(chars[-len], chars[len])])
[1] 1054
0.002 sec elapsed

Day 2

Part 1

input <- readr::read_tsv("2017/02-input", col_names = FALSE)

sum(apply(as.matrix(input), 1, function(x) max(x)-min(x)))
[1] 32020
0.358 sec elapsed

Part 2

input <- readr::read_tsv("2017/02-input", col_names = FALSE)

evenly <- function(x) {
  outers <- outer(x, x, "/")

  outers[((outers %% 1) == 0) & (outers != 1)]
}

sum(apply(as.matrix(input), 1, evenly))

x <- as.numeric(input[1, ])
0.088 sec elapsed

Day 3

Part 1

input <- scan("2017/03-input")

center <- 1

right <- 2
up <- 4
left <- 6
down <- 8

ring <- 1

repeat {
  ring <- ring + 1
  right <- down + ring + (ring - 1)
  up <- right + ring * 2
  left <- up + ring * 2
  down <- left + ring * 2
  if (any(c(right, up, left, down) >= input)) break
}

min(abs(c(right, up, left, down) - input)) + ring
[1] 371
0.004 sec elapsed

Part 2

input <- scan("2017/03-input")

get_sum <- function(x, values) {
  neightbor_locs <- purrr::pmap_chr(
    expand.grid(x = c(-1, 0, 1) + x[1], y = c(-1, 0, 1) + x[2]),
    function(x, y) paste0(x, "_", y)
  )
  sum(unlist(values[neightbor_locs]), na.rm = TRUE)
}

loc <- c(0, 0)

values <- list()

values[paste0(loc, collapse = "_")] <- 1

ring <- 1

# repeat
ring <- 0
repeat {
  if (value > input) break
  ring <- ring + 1

  # right (advance)
  loc <- loc + c(1, 0)

  value <- get_sum(loc, values)
  values[paste0(loc, collapse = "_")] <- value
  cat("loc:", loc, "value:", value, "\n")
  if (value > input) break

  # go up
  while (value <= input) {
    loc <- loc + c(0, 1)

    value <- get_sum(loc, values)
    values[paste0(loc, collapse = "_")] <- value
    cat("loc:", loc, "value:", value, "\n")
    if (identical(loc, c(ring, ring))) break
    if (value > input) break
  }

  # go left
  while (value <= input) {
    loc <- loc + c(-1, 0)

    value <- get_sum(loc, values)
    values[paste0(loc, collapse = "_")] <- value
    cat("loc:", loc, "value:", value, "\n")
    if (identical(loc, c(-ring, ring))) break
    if (value > input) break
  }

  # go down
  while (value <= input) {
    loc <- loc + c(0, -1)

    value <- get_sum(loc, values)
    values[paste0(loc, collapse = "_")] <- value
    cat("loc:", loc, "value:", value, "\n")
    if (identical(loc, c(-ring, -ring))) break
    if (value > input) break
  }

  # go right
  while (value <= input) {
    loc <- loc + c(1, 0)

    value <- get_sum(loc, values)
    values[paste0(loc, collapse = "_")] <- value
    cat("loc:", loc, "value:", value, "\n")
    if (identical(loc, c(ring, -ring))) break
    if (value > input) break
  }
}

value
[1] 9132360
0.015 sec elapsed

Day 4

Part 1

library(purrr)

input <- readLines("2017/04-input")

passwords <- strsplit(input, " ")

sum(map_lgl(passwords, ~ all(table(.x) == 1)))
[1] 386
0.08 sec elapsed

Part 2

library(purrr)

input <- readLines("2017/04-input")

passwords <- strsplit(input, " ")

order_letters <- function(x) {
  paste0(sort(strsplit(x, "")[[1]]), collapse = "")
}

sum(map_lgl(passwords, ~ all(table(map_chr(.x, order_letters)) == 1)))
[1] 208
0.218 sec elapsed

Day 5

Part 1

input <- as.numeric(readLines("2017/05-input"))

len <- length(input)

index <- 1
steps <- 0

repeat {
  steps <- steps + 1
  offset <- input[index]
  input[index] <- input[index] + 1
  index <- index + offset

  if (index < 1 | index > len) break
}

steps
[1] 364539
0.164 sec elapsed

Part 2

input <- as.numeric(readLines("2017/05-input"))

len <- length(input)

index <- 1
steps <- 0

repeat {
  steps <- steps + 1
  offset <- input[index]

  if (offset >= 3) {
    input[index] <- input[index] - 1
  } else {
    input[index] <- input[index] + 1
  }
  index <- index + offset

  if (index < 1 | index > len) break
}

steps
[1] 27477714
8.334 sec elapsed

Day 6

Part 1

input <- scan("2017/06-input")

seen <- paste(input, collapse = " ")

input_len <- length(input)

cycles <- 0

repeat {
  cycles <- cycles + 1

  index <- min(which(max(input) == input))
  blocks <- input[index]
  input[index] <- 0

  while (blocks > 0) {
    index <- index + 1
    if (index > input_len) {
      index <- 1
    }
    input[index] <- input[index] + 1
    blocks <- blocks - 1
  }

  if (paste(input, collapse = " ") %in% seen) break
  seen <- c(seen, paste(input, collapse = " "))
}

cycles

length(seen) - which(paste(input, collapse = " ") == seen) + 1
[1] 4
0.006 sec elapsed

Part 2

input <- scan("2017/06-input")

seen <- paste(input, collapse = " ")

input_len <- length(input)

cycles <- 0

repeat {
  cycles <- cycles + 1

  index <- min(which(max(input) == input))
  blocks <- input[index]
  input[index] <- 0

  while (blocks > 0) {
    index <- index + 1
    if (index > input_len) {
      index <- 1
    }
    input[index] <- input[index] + 1
    blocks <- blocks - 1
  }

  if (paste(input, collapse = " ") %in% seen) break
  seen <- c(seen, paste(input, collapse = " "))
}

length(seen) - which(paste(input, collapse = " ") == seen) + 1
[1] 4
0.006 sec elapsed

Day 7

Part 1

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

library(stringr)

loc <- str_subset(input, "->")[1] |>
  str_extract("[a-z]+")

repeat {

  next_id <- str_detect(input, paste0(".+", loc))

  if (all(!next_id)) break

  loc <- str_extract(input[next_id], "^[a-z]+")
}

loc
[1] "dgoocsw"
0.056 sec elapsed

Part 2

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

library(stringr)

loc <- str_subset(input, "->")[1] |>
  str_extract("[a-z]+")

repeat {

  next_id <- str_detect(input, paste0(".+", loc))

  if (all(!next_id)) break

  loc <- str_extract(input[next_id], "^[a-z]+")
}

get_children <- function(x, input) {
  str_subset(input, paste0("^", x)) |>
    str_remove(".*> ") |>
    str_split_1(", ")
}

get_weight <- function(x) {
  value <- str_subset(input, paste0("^", x))

  res <- str_extract(value, "[0-9]+") |> as.integer()
  if (str_detect(value, ">")) {
    res <- res + sum(purrr::map_int(get_children(x, input), get_weight))
  }
  res
}

bot <- loc

error <- function(x) max(x) - min(x)
wrong_amount  <- error(purrr::map_int(get_children(bot, input), get_weight))

repeat {

children <- get_children(bot, input)

weights <- purrr::map_int(children, get_weight)

if (length(table(weights)) == 1) break

bot <- children[names(table(weights)[table(weights) == 1]) == weights]

}

wrong_weight <- str_subset(input, paste(children, collapse = ", ")) |>
  str_extract("[0-9]+") |>
  as.integer()

wrong_weight - wrong_amount
[1] 1275
0.571 sec elapsed

Day 8

Part 1

input <- readLines("2017/08-input")

inc <- function(x, value) {
  name <- deparse(substitute(x))
  .GlobalEnv[[name]] <- .GlobalEnv[[name]] + value
}

dec <- function(x, value) {
  name <- deparse(substitute(x))
  .GlobalEnv[[name]] <- .GlobalEnv[[name]] - value
}

library(stringr)

registers <- str_extract_all(input, "[a-z]+") |>
  unlist() |>
  unique() |>
  setdiff(c("dec", "inc", "if"))

materialize <- function(name) {
  .GlobalEnv[[name]] <- 0
}

purrr::walk(registers, materialize)

reorganize <- function(x) {
  glue::glue("if ({x[5]} {x[6]} {x[7]}) {x[2]}({x[1]}, {x[3]})")
}

str_split(input, " ") |>
  purrr::map_chr(reorganize) |>
  parse(text = _) |>
  eval()

purrr::map_dbl(registers, ~eval(parse(text = .x))) |>
  max()
[1] 4902
0.123 sec elapsed

Part 2

input <- readLines("2017/08-input")

highest <- 0

inc <- function(x, value) {
  name <- deparse(substitute(x))
  .GlobalEnv[[name]] <- .GlobalEnv[[name]] + value
  .GlobalEnv[["highest"]] <- max(.GlobalEnv[["highest"]], .GlobalEnv[[name]])
}

dec <- function(x, value) {
  name <- deparse(substitute(x))
  .GlobalEnv[[name]] <- .GlobalEnv[[name]] - value
  .GlobalEnv[["highest"]] <- max(.GlobalEnv[["highest"]], .GlobalEnv[[name]])
}

library(stringr)

registers <- str_extract_all(input, "[a-z]+") |>
  unlist() |>
  unique() |>
  setdiff(c("dec", "inc", "if"))

materialize <- function(name) {
  .GlobalEnv[[name]] <- 0
}

purrr::walk(registers, materialize)

reorganize <- function(x) {
  glue::glue("if ({x[5]} {x[6]} {x[7]}) {x[2]}({x[1]}, {x[3]})")
}

str_split(input, " ") |>
  purrr::map_chr(reorganize) |>
  parse(text = _) |>
  eval()

highest
[1] 7037
0.12 sec elapsed

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