2025

— R Day 8 Part 1 —

solve_day8_part1 <- function(input, n_edges = 1000) {
  xyz <- read.table(text = input, sep =",", col.names = c("x", "y", "z"))
  n_coords <- NROW(xyz)
  xyz$graph_id <- xyz$box_id <- seq_len(n_coords)

  pairs <- merge(xyz, xyz, by = NULL, suffixes = c("_1", "_2"))

  pairs <- pairs |>
    subset(box_id_1 < box_id_2) |>
    transform(dist = sqrt((x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2))


  pairs <- sort_by(pairs, ~dist)
  pairs <- pairs[1:n_edges, ]

  for (edge in seq_len(n_edges)) {
    box_id_1 <- pairs[edge, "box_id_1"]
    box_id_2 <- pairs[edge, "box_id_2"]

    graph_id_1 <- xyz$graph_id[box_id_1]
    graph_id_2 <- xyz$graph_id[box_id_2]

    xyz$graph_id[xyz$graph_id == graph_id_2] <- graph_id_1
  }

  circuit_sizes <- as.numeric(table(xyz$graph_id))
  largest_circuits <- sort(circuit_sizes, decreasing = TRUE)[1:3]
  prod(largest_circuits)
}
Run
aoc_source(day = 8, part = 1)

input = aoc_read(day = 8)

aoc_run(solve_day8_part1(input))
Elapsed: 1.656 seconds
Memory:  377196 KB

— R Day 8 Part 2 —

solve_day8_part2 <- function(input) {
  xyz <- read.table(text = input, sep =",", col.names = c("x", "y", "z"))
  n_boxes <- NROW(xyz)
  xyz$graph_id <- xyz$box_id <- seq_len(n_boxes)

  pairs <- merge(xyz, xyz, by = NULL, suffixes = c("_1", "_2"))

  pairs <- pairs |>
    subset(box_id_1 < box_id_2) |>
    transform(dist = sqrt((x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2))

  pairs <- sort_by(pairs, ~dist)
  n_edges <- NROW(pairs)
  n_circuits <- n_boxes

  for (edge in seq_len(n_edges)) {
    box_id_1 <- pairs[edge, "box_id_1"]
    box_id_2 <- pairs[edge, "box_id_2"]

    graph_id_1 <- xyz$graph_id[box_id_1]
    graph_id_2 <- xyz$graph_id[box_id_2]

    if (graph_id_1 != graph_id_2) {
      n_circuits <- n_circuits - 1L
      xyz$graph_id[xyz$graph_id == graph_id_2] <- graph_id_1
    }

    if (n_circuits == 1L) {
      return(prod(xyz[c(box_id_1, box_id_2), "x"]))
    }
  }
}
Run
aoc_source(day = 8, part = 2)

input = aoc_read(day = 8)

aoc_run(solve_day8_part2(input))
Elapsed: 1.854 seconds
Memory:  361623 KB

— Python Day 8 Part 1 —

from math import dist, prod
import networkx as nx

def solve_day8_part1(text, n_edges = 1000):
    coords = [line.split(",") for line in text]
    coords = [[int(x) for x in box] for box in coords]

    edges = []
    for i in range(len(coords)):
        for j in range(len(coords)):
            if i < j:
                box_1, box_2 = coords[i], coords[j]
                edge_len = dist(box_1, box_2)
                edges += [[i, j, edge_len]]

    edges.sort(key = lambda x : x[2])
    edges = edges[:n_edges]
    edges = [edge[:2] for edge in edges] # remove distances

    uf = nx.utils.UnionFind()
    for box_1, box_2 in edges:
        uf.union(box_1, box_2)

    groups = list(uf.to_sets())
    sizes = [len(g) for g in groups]
    sizes.sort(reverse=True)

    return(prod(sizes[:3]))
Run
aoc_source(day = 8, part = 1)

input = aoc_read(day = 8)

result = aoc_run("solve_day8_part1(input)")
Elapsed: 2.906 seconds
Memory:  77395 KB

— Python Day 8 Part 2 —

from math import dist
import networkx as nx

def solve_day8_part2(text):
    coords = [line.split(",") for line in text]
    coords = [[int(x) for x in box] for box in coords]
    n_boxes = len(coords)

    edges = []
    for i in range(len(coords)):
        for j in range(len(coords)):
            if i < j:
                box_1, box_2 = coords[i], coords[j]
                edge_len = dist(box_1, box_2)
                edges += [[i, j, edge_len]]

    edges.sort(key = lambda x : x[2])
    edges = [edge[:2] for edge in edges] # remove distances

    uf = nx.utils.UnionFind()
    n_circuits = n_boxes
    for box_1, box_2 in edges:
        # union if boxes not already in same circuit
        if uf[box_1] != uf[box_2]:
            uf.union(box_1, box_2)
            n_circuits -= 1

        if n_circuits == 1:
            return(coords[box_1][0] * coords[box_2][0])
Run
aoc_source(day = 8, part = 2)

input = aoc_read(day = 8)

result = aoc_run("solve_day8_part2(input)")
Elapsed: 4.79 seconds
Memory:  108779 KB
Back to top