Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code 2024 - Day 23

2024/day-23.livemd

Advent of Code 2024 - Day 23

Mix.install([
  {:kino_aoc, "~> 0.1.7"},
  {:libgraph, "~> 0.13.3"}
])

Section

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2024", "23", System.fetch_env!("LB_AOC_SESSION"))
defmodule MyList do
  def combinations(_, 0) do
    [[]]
  end

  def combinations(ls, k) when length(ls) < k do
    []
  end

  def combinations([h | t], k) do
    for combo <- combinations(t, k - 1) do
      [h | combo]
    end
    |> Kernel.++(combinations(t, k))
  end
end
import MyList
combinations([1,2,3,4,5,6], 0)
graph =
  puzzle_input
  |> String.split(~r/\W/)
  |> Enum.chunk_every(2)
  |> Enum.map(&amp;List.to_tuple/1)
  |> then(fn edges ->
    Graph.new(type: :undirected)
    |> Graph.add_edges(edges)
  end)

Part 1

graph
|> Graph.cliques()
|> Enum.map(fn clique ->
  Enum.split_with(clique, &amp;match?(<_>, &amp;1))
end)
|> Enum.flat_map(fn {t, nt} ->
  c1 =
    for a <- combinations(t, 1), b <- combinations(nt, 2) do
      a ++ b
    end

  c2 =
    for a <- combinations(t, 2), b <- combinations(nt, 1) do
      a ++ b
    end

  c3 = combinations(t, 3)

  Enum.flat_map([c1, c2, c3], &amp;Function.identity/1)
end)
|> Enum.uniq()
|> length()

Part 2

graph
|> Graph.cliques()
|> Enum.max_by(&amp;length/1)
|> Enum.sort()
|> Enum.join(",")