2025

— R Day 7 Part 1 —

solve_day7_part1 <- function(input) {
  grid <- aoc_input_to_chr_matrix(input)

  nrows <- NROW(grid)
  ncols <- NCOL(grid)

  source <- which(grid == "S", arr.ind = TRUE)

  row <- source[[1L]] + 1L
  beams <- vector("list", nrows)
  beams[[2L]] <- source[[2L]]
  split_count <- 0L

  while(row < nrows) {
    next_row <- row + 1L

    for (i in beams[[row]]) {
      if (grid[row, i] == "^") {
        split_count <- split_count + 1L
        beams[[next_row]] <- c(beams[[next_row]], i-1, i+1)
        next
      }

      if (!i %in% beams[[next_row]]) {
        beams[[next_row]] <- c(beams[[next_row]], i)
      }
    }

    row <- next_row
  }

  split_count
}
Run
aoc_source(day = 7, part = 1)

input = aoc_read(day = 7)

aoc_run(solve_day7_part1(input))
Elapsed: 0.014 seconds
Memory:  4084 KB

— R Day 7 Part 2 —

solve_day7_part2 <- function(input) {
  grid <- aoc_input_to_chr_matrix(input)
  nrows <- NROW(grid)
  ncols <- NCOL(grid)

  source <- which(grid == "S", arr.ind = TRUE)

  row <- source[[1L]]
  beams <- matrix(0, nrows, ncols)
  beams[row, source[[2L]]] <- 1

  for (row in seq_len(nrows-1L)) {
    curr <- beams[row, ]
    cols <- which(curr > 0)

    next_row <- row + 1L
    is_split <- grid[next_row, cols] == "^"

    splits  <- cols[is_split]
    not_split <- cols[!is_split]

    if (length(not_split) > 0L) {
      beams[next_row, not_split] <- beams[next_row, not_split] + curr[not_split]
    }

    if (length(splits) > 0L) {
      left  <- splits - 1L
      right <- splits + 1L

      beams[next_row, left]  <- beams[next_row, left]  + curr[splits]
      beams[next_row, right] <- beams[next_row, right] + curr[splits]
    }
  }

  sum(beams[nrows, ])
}
Run
aoc_source(day = 7, part = 2)

input = aoc_read(day = 7)

aoc_run(solve_day7_part2(input))
Elapsed: 0.002 seconds
Memory:  1574 KB

— Python Day 7 Part 1 —

def solve_day7_part1(text):
    nrows = len(text)
    ncols = len(text[0])

    source = text[0].index("S")
    beams = [[0] * ncols for _ in range(nrows)]
    beams[0][source] = 1

    split_count = 0

    for row in range(nrows-1):
        next_row = row + 1
        beam_cols = [i for i, val in enumerate(beams[row]) if val > 0]

        for col in beam_cols:
            if text[next_row][col] == "^":
                beams[next_row][col+1] += 1
                beams[next_row][col-1] += 1
                split_count += 1
                continue

            if beams[next_row][col] == 0:
                beams[next_row][col] = 1

    return(split_count)
Run
aoc_source(day = 7, part = 1)

input = aoc_read(day = 7)

result = aoc_run("solve_day7_part1(input)")
Elapsed: 0.005 seconds
Memory:  173 KB

— Python Day 7 Part 2 —

def solve_day7_part2(text):
    nrows = len(text)
    ncols = len(text[0])

    source = text[0].index("S")
    beams = [[0] * ncols for _ in range(nrows)]
    beams[0][source] = 1

    for row in range(nrows-1):
        next_row = row + 1
        beam_cols = [i for i, val in enumerate(beams[row]) if val > 0]

        for col in beam_cols:
            n_beams = beams[row][col]
            if text[next_row][col] == "^":
                beams[next_row][col+1] += n_beams
                beams[next_row][col-1] += n_beams
                continue

            beams[next_row][col] += n_beams

    return(sum(beams[nrows-1]))
Run
aoc_source(day = 7, part = 2)

input = aoc_read(day = 7)

result = aoc_run("solve_day7_part2(input)")
Elapsed: 0.015 seconds
Memory:  302 KB
Back to top