2023

— R Day 6 Part 1 —

solve_day6_part1 <- function(input) {
  races <- stringi::stri_extract_all(input[[1]], regex = "\\d+")
  races <- as.numeric(unlist(races))

  records <- stringi::stri_extract_all(input[[2]], regex = "\\d+")
  records <- as.numeric(unlist(records))

  ways_to_win <- numeric(length(races))

  for (index in seq_along(races)) {
    distances <- vector("list", length(races[[index]]))

    for (hold_time in seq_len(races[[index]])) {
      distances[[hold_time]] <- hold_time * (races[[index]] - hold_time)
    }

    ways_to_win[[index]] <- sum(distances > records[[index]])
  }


  Reduce(`*`, ways_to_win)
}
Run
aoc_source(day = 6, part = 1)

input = aoc_read(day = 6)

aoc_run(solve_day6_part1(input))
Elapsed: < 0.001 seconds
Memory:  614 KB

— R Day 6 Part 2 —

solve_day6_part2 <- function(input) {
  race_time <- stringi::stri_extract_all(input[[1]], regex = "\\d+")
  race_time <- as.numeric(paste0(unlist(race_time), collapse = ""))

  record <- stringi::stri_extract_all(input[[2]], regex = "\\d+")
  record <- as.numeric(paste(unlist(record), collapse = ""))

  # I originally did brute force approach over half of the numbers
  # (recognising bell shaped curve) with:
  # hold_time * (race_time - hold_time) > record

  # Solved in <10s, but even better expanding the formula:

  # -hold_time * hold_time + race_time * hold_time > record
  # -hold_time^2 + race_time * hold_time > record
  # -hold_time^2 + race_time * hold_time - record > 0

  # i.e. ax^2 + bx + c > 0

  # x = hold_time
  # a = -1
  # b = race_time
  # c = -record

  # plug into quadratic formula
  # x = (-b +- sqrt(b^2 - 4ac)) / 2a

  x1 <- (-race_time - sqrt(race_time^2 - 4 * -1 * (-record))) / (2 * -1)
  x2 <- (-race_time + sqrt(race_time^2 - 4 * -1 * (-record))) / (2 * -1)

  abs(x2 - x1)
}
Run
aoc_source(day = 6, part = 2)

input = aoc_read(day = 6)

aoc_run(solve_day6_part2(input))
Elapsed: < 0.001 seconds
Memory:  131 KB
Back to top