Day 23

Advent of Code: Worked Solutions

About
Date

December 23, 2024

Setup

# Libraries
library(tidyverse)
library(igraph)

# Read input from file
input <- read_lines("../input/day23.txt", skip_empty_rows = TRUE) |> 
  unglue::unglue_data("{v1}-{v2}")

Part 1

# Convert list 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(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"