Initial commit

This commit is contained in:
Geoffrey Frogeye 2024-12-25 12:58:02 +01:00
commit 97a4330bc0
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8
110 changed files with 7006 additions and 0 deletions

65
2024/23/both_networkx.py Normal file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python3
import sys
import matplotlib.pyplot as plt
import networkx
import networkx as nx
input_file = sys.argv[1]
G = nx.Graph()
with open(input_file) as fd:
for line in fd.readlines():
a, b = line.rstrip().split("-")
G.add_edge(a, b)
trio_cliques: list[list[str]] = list()
lan = None
for clique in nx.enumerate_all_cliques(G):
if lan is None or len(clique) > len(lan):
lan = clique
if len(clique) != 3:
continue
if not any(c.startswith("t") for c in clique):
continue
trio_cliques.append(clique)
part1_ans = len(trio_cliques)
assert lan is not None
part2_ans = ",".join(sorted(lan))
print(f"{part1_ans=}")
print(f"{part2_ans=}")
trio_nodes = set(node for trio_clique in trio_cliques for node in trio_clique)
trio_edges = set(
edge
for clique in trio_cliques
for edge in list(nx.edge_boundary(G, clique, clique))
)
lan_edges = set(nx.edge_boundary(G, lan, lan))
for node in trio_nodes:
G.nodes[node]["color"] = "green"
for edge in trio_edges:
G.edges[edge]["color"] = "green"
G.edges[edge]["weight"] = 2
for node in lan:
G.nodes[node]["color"] = "red"
for edge in lan_edges:
# G.edges[edge]["color"] = "red"
G.edges[edge]["weight"] = 5
node_colors = [G.nodes[node].get("color", "blue") for node in G.nodes()]
edge_colors = [G.edges[edge].get("color", "blue") for edge in G.edges()]
node_pos = nx.layout.spring_layout(G)
nx.draw(
G, node_color=node_colors, edge_color=edge_colors, pos=node_pos, with_labels=True
)
plt.show()

36
2024/23/one.py Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python3
import collections
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
connections = [tuple(line.split("-")) for line in lines]
codi: collections.defaultdict[str, set[str]] = collections.defaultdict(set)
for connection in connections:
a, b = connection
codi[a].add(b)
codi[b].add(a)
threes: set[tuple[str, ...]] = set()
for connection in connections:
a, b = connection
ac, bc = codi[a], codi[b]
iis = ac.intersection(bc)
for i in iis:
threel = [a, b, i]
if not (a.startswith("t") or b.startswith("t") or i.startswith("t")):
continue
threel.sort()
threes.add(tuple(threel))
for three in threes:
print(three)
print(len(threes))
# 11011 too high (forgot starts with t)

55
2024/23/two.py Normal file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python3
import collections
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
connections = [set(line.split("-")) for line in lines]
codi: collections.defaultdict[str, set[str]] = collections.defaultdict(set)
for connection in connections:
a, b = connection
codi[a].add(b)
codi[b].add(a)
threes: set[tuple[str, ...]] = set()
for connection in connections:
a, b = connection
ac, bc = codi[a], codi[b]
iis = ac.intersection(bc)
for i in iis:
threel = [a, b, i]
threel.sort()
threes.add(tuple(threel))
j = 0
while len(threes) > 1:
inthrees: set[str] = set()
for three in threes:
inthrees.update(set(three))
print(j, len(threes), len(inthrees))
fours: set[tuple[str, ...]] = set()
for three in threes:
threeset = set(three)
for comp in inthrees - threeset:
compc = codi[comp]
if threeset.issubset(compc):
fourl = list(threeset) + [comp]
fourl.sort()
fours.add(tuple(fourl))
threes = fours
threesl = list(threes)
if len(threesl) == 1:
three = threesl[0]
print(",".join(three))
print(None)