Day 23

Advent of Code: Worked Solutions

About
Date

December 23, 2024

Setup

Import libraries:

library(tidyverse)
library(unglue)
library(igraph)

Read input from text file into a data frame:

input <- read_lines("../input/day23.txt") |> 
  unglue_data("{v1}-{v2}")

Part 1

Convert the lists of connections to an undirected graph:

g <- input |> 
  pmap(function(v1, v2) c(v1, v2)) |> 
  unlist() |> 
  make_graph(directed = FALSE)

Find all sets of 3 connected vertices (cliques of size 3) and count the sets having some vertex starting with ‘t’

cliques(g, min = 3, max = 3) |> 
  keep(
    ~ names(.x) |> 
      str_starts("t") |> 
      any()
  ) |> 
  length()

Part 2

Find the largest interconnected set of vertices, then convert the list of vertices into the “password” by sorting alphabetically and concatenating with commas:

g |> 
  largest_cliques() |> 
  unlist() |> 
  names() |> 
  sort() |> 
  str_flatten(",")