|
| 1 | +defmodule Day23a do |
| 2 | + def run do |
| 3 | + File.read!(".input.txt") |> run |
| 4 | + end |
| 5 | + |
| 6 | + def run(input) do |
| 7 | + con = input |> String.split("\n") |> Enum.map(&List.to_tuple(String.split(&1, "-"))) |
| 8 | + |
| 9 | + graph = |
| 10 | + Graph.new(type: :undirected) |
| 11 | + |> Graph.add_edges(con) |
| 12 | + |
| 13 | + Graph.components(graph) |
| 14 | + |> Enum.flat_map(fn comp -> |
| 15 | + comp |
| 16 | + |> Enum.filter(&String.starts_with?(&1, "t")) |
| 17 | + |> Enum.flat_map(fn x -> |
| 18 | + n = graph |> Graph.neighbors(x) |
| 19 | + |
| 20 | + n |
| 21 | + |> Enum.flat_map(fn y -> n |> Enum.map(&{y, &1}) end) |
| 22 | + |> Enum.filter(fn {a, b} -> a != b end) |
| 23 | + |> Enum.filter(fn {a, b} -> Graph.edge(graph, a, b) != nil end) |
| 24 | + |> Enum.map(fn {a, b} -> MapSet.new([a, b, x]) end) |
| 25 | + end) |
| 26 | + end) |
| 27 | + |> MapSet.new() |
| 28 | + |> MapSet.size() |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +defmodule Day23b do |
| 33 | + def run do |
| 34 | + File.read!(".input.txt") |> run |
| 35 | + end |
| 36 | + |
| 37 | + def bron_kerbosch(graph) do |
| 38 | + bron_kerbosch(graph, MapSet.new(), MapSet.new(Graph.vertices(graph)), MapSet.new(), []) |
| 39 | + end |
| 40 | + |
| 41 | + def bron_kerbosch(graph, r, p, x, max_cliques) do |
| 42 | + if MapSet.size(p) == 0 and MapSet.size(x) == 0 do |
| 43 | + [MapSet.to_list(r) | max_cliques] |
| 44 | + else |
| 45 | + p |
| 46 | + |> Enum.reduce({p, x, max_cliques}, fn v, {p, x, max_cliques} -> |
| 47 | + n_v = MapSet.new(Graph.neighbors(graph, v)) |
| 48 | + |
| 49 | + max_cliques = |
| 50 | + bron_kerbosch( |
| 51 | + graph, |
| 52 | + MapSet.put(r, v), |
| 53 | + MapSet.intersection(p, n_v), |
| 54 | + MapSet.intersection(x, n_v), |
| 55 | + max_cliques |
| 56 | + ) |
| 57 | + |
| 58 | + p = p |> MapSet.delete(v) |
| 59 | + x = x |> MapSet.put(v) |
| 60 | + |
| 61 | + {p, x, max_cliques} |
| 62 | + end) |
| 63 | + |> elem(2) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def run(input) do |
| 68 | + con = input |> String.split("\n") |> Enum.map(&List.to_tuple(String.split(&1, "-"))) |
| 69 | + |
| 70 | + graph = |
| 71 | + Graph.new(type: :undirected) |
| 72 | + |> Graph.add_edges(con) |
| 73 | + |
| 74 | + bron_kerbosch(graph) |
| 75 | + |> Enum.max_by(fn comp -> length(comp) end) |
| 76 | + |> Enum.sort() |
| 77 | + |> Enum.join(",") |
| 78 | + end |
| 79 | +end |
0 commit comments