My 2019 Solutions

Day 1

Part 1

input <- scan("2019/01-input")

sum(floor(input / 3) - 2)
[1] 3368364
0.001 sec elapsed

Part 2

input <- scan("2019/01-input")

calc_fuel <- function(x) {
  total <- 0
  repeat {
    fuel <- floor(x / 3) - 2
    if (fuel <= 0) return(total)
    total <- total + fuel
    x <- fuel
  }
}
vapply(input, calc_fuel, FUN.VALUE = numeric(1)) |>
  sum()
[1] 5049684
0.002 sec elapsed

Day 2

Day 3

Day 4

Part 1

library(purrr)

input <- 136818:685979

chars <- input |>
  as.character() |>
  strsplit("") |>
  map(as.numeric)

map_lgl(chars, ~ any(diff(.x) == 0) & all(diff(.x) >= 0)) |>
  sum()
[1] 1919
8.552 sec elapsed

Part 2

library(purrr)

input <- 136818:685979

chars <- input |>
  as.character() |>
  strsplit("") |>
  map(as.numeric)

check <- function(x) {
  x_rle <- rle(diff(x))
  if (all(x_rle$values != 0)) {
    return(FALSE)
  }

  if (any(x_rle$lengths[x_rle$values == 0] == 1)) {
    return(TRUE)
  }
  FALSE
}

map_lgl(chars, ~ all(diff(.x) >= 0) & check(.x)) |>
  sum()
[1] 1291
16.432 sec elapsed

Day 5

Day 6

Day 7

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