Skip to content

Commit 0c459a4

Browse files
committed
2024 Day 23
1 parent fad36c7 commit 0c459a4

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

2024/lib/day23.ex

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

2024/test/day23_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule Day23Test do
2+
use ExUnit.Case
3+
4+
def example, do: ""
5+
6+
# test "Part 1 Example" do
7+
# assert Day23a.run(example()) == :todo
8+
# end
9+
10+
# test "Part 2 Example" do
11+
# assert Day23b.run(example()) == :todo
12+
# end
13+
end

0 commit comments

Comments
 (0)