# Libraries
library(tidyverse)
library(igraph)
# Read input from file
<- read_lines("../input/day23.txt", skip_empty_rows = TRUE) |>
input ::unglue_data("{v1}-{v2}") unglue
Day 23
Advent of Code: Worked Solutions
Setup
Part 1
# Convert list of connections to an undirected graph
<- input |>
g pmap(function(v1, v2) c(v1, v2)) |>
unlist() |>
make_graph(directed = FALSE)
# Find all sets of 3 connected vertices
cliques(g, min = 3, max = 3) |>
# Keep only the sets having some vertex starting with 't'
keep(
~ names(.x) |>
str_starts("t") |>
any()
|>
)
# Count the number of resulting sets
length()
[1] 1423
Part 2
|>
g
# Find the largest interconnected set of vertices
largest_cliques() |>
unlist() |>
# Convert the list of vertices into the "password" by sorting alphabetically
names() |>
sort() |>
str_c(collapse = ",")
[1] "gt,ha,ir,jn,jq,kb,lr,lt,nl,oj,pp,qh,vy"