# Libraries
library(tidyverse)
library(unglue)
library(igraph)
# Read input from file
<- read_lines("../input/day25.txt", skip_empty_rows = FALSE) input
Day 25
Advent of Code: Worked Solutions
Setup
Part 1
Convert text input to a graph:
<- input |>
wires str_split(":? ") |>
unlist() |>
unique()
<- input |>
edges unglue_data("{name}: {conn}") |>
mutate(conn = str_split(conn, " ")) |>
unnest_longer(conn) |>
mutate(across(c(name, conn), ~ match(.x, wires))) |>
pmap(\(name, conn) c(name, conn)) |>
unlist()
<- make_graph(edges, directed = FALSE) g
Compute the betweenness of each edge and pull the three with the maximum values:
<- edge_betweenness(g)
scores <- scores |>
idx sort(decreasing = TRUE) |>
head(3) |>
match(scores)
Plot for visual confirmation:
<- E(g) |>
edge_colors as.numeric() |>
case_match(idx ~ "blue", .default = "grey")
plot(g, vertex.size = 4, vertex.label = NA, edge.color = edge_colors)
Remove the selected vertices from the graph, then calculate the product of the size of its two disconnected groups:
|>
g delete_edges(idx) |>
components() |>
pluck("csize") |>
prod()
[1] 552695