2025

— R Day 12 Part 1 —

solve_day12_part1 <- function(input) {
  # Parsing ----
  pres_start <- which(substr(input, 2L, 2L) == ":") + 1L
  empty <- which(substr(input, 1L, 1L) == "")

  n_lines <- length(input)
  n_empty <- length(empty)
  n_presents <- length(pres_start)

  presents <- vector("list", n_presents)
  # present_lookup <- c("#" = TRUE, "." = FALSE)
  for (i in seq_len(n_presents)) {
    start <- pres_start[[i]]
    present <- input[start:(start + 2L)]
    present <- t(simplify2array(strsplit(present, "")))
    present[present == "#"] <- 1L
    present[present == "."] <- 0L
    storage.mode(present) <- "integer"
    presents[[i]] <- present
  }

  split <- empty[n_empty]
  trees <- input[(split + 1):n_lines]

  base_cols <- stringi::stri_extract_first_regex(trees, r"{\d+(?=x)}")
  base_cols <- as.integer(base_cols)

  base_rows <- stringi::stri_extract_first_regex(trees, r"{(?<=x)\d+(?=:)}")
  base_rows <- as.integer(base_rows)


  pres_counts <- stringi::stri_extract_all_regex(trees, r"{(?<= )\d+}")
  pres_counts <- lapply(pres_counts, as.integer)

  # Now what ----

  # for tree in trees
  #  for pres in pres counts
  #      try placing in every possible position?

  # can we shortcut with not enough space?
  base_areas <- base_cols * base_rows

  pres_areas <- vapply(presents, sum, integer(1))
  min_areas <- lapply(pres_counts, \(x) x * pres_areas)

  min_areas <- vapply(min_areas, sum, numeric(1))

  # uhh, ahahahaha
  sum(min_areas < base_areas)
}
Run
aoc_source(day = 12, part = 1)

input = aoc_read(day = 12)

aoc_run(solve_day12_part1(input))
Elapsed: 0.005 seconds
Memory:  768 KB

— R Day 12 Part 2 —

solve_day12_part2 <- function(inupt) {
  free <- star <- 1

  free * star
}
Run
aoc_source(day = 12, part = 2)

input = aoc_read(day = 12)

aoc_run(solve_day12_part2(input))
Elapsed: 0.001 seconds
Memory:  < 1 KB

— Python Day 12 Part 1 —

import re
def solve_day12_part1(text):
    spaces = [i for i, v in enumerate(text) if v == "\n"]
    split = spaces[len(spaces)-1] + 1
    trees = text[split:]

    widths = [int(line[:2]) for line in trees]
    heights = [int(line[3:5]) for line in trees]
    areas = [w * h for w, h in zip(widths, heights)]

    counts = [re.findall("(?<= )\d+", line) for line in trees]
    counts = [[int(x) for x in line] for line in counts]
    counts = [sum(line) for line in counts]

    valid = [area >= (9 * count) for area, count in zip(areas, counts)]

    sum(valid)
Run
aoc_source(day = 12, part = 1)

input = aoc_read(day = 12)

result = aoc_run("solve_day12_part1(input)")
Elapsed: 0.016 seconds
Memory:  621 KB
Back to top