Solve 2024:23 p1-2 "LAN Party"
Part 1 uses the excellent itertools.combinations(). Part 2 introduces Bron-Kerbosch algorithm to the util library!
This commit is contained in:
parent
096c3d3e5d
commit
2c2379788d
3 changed files with 54 additions and 19 deletions
|
|
@ -15,19 +15,12 @@ except ValueError:
|
||||||
day_no = None
|
day_no = None
|
||||||
name = None
|
name = None
|
||||||
|
|
||||||
print(
|
|
||||||
f"\n\033[95m\033[1mAdvent of Code {year}\033[0m"
|
|
||||||
"\n###################"
|
|
||||||
"\n\n\033[96mby Anders Englöf Ytterström\033[0m"
|
|
||||||
)
|
|
||||||
|
|
||||||
Path("./input").mkdir(parents=True, exist_ok=True)
|
Path("./input").mkdir(parents=True, exist_ok=True)
|
||||||
Path("./output").mkdir(parents=True, exist_ok=True)
|
Path("./output").mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
if day_no and name:
|
if day_no and name:
|
||||||
name = " ".join(name)
|
name = " ".join(name)
|
||||||
padded_no = day_no.zfill(2)
|
padded_no = day_no.zfill(2)
|
||||||
print(f"\n- creating output/day_{padded_no}.py")
|
|
||||||
with open("output/day_{}.py".format(padded_no), "w") as s:
|
with open("output/day_{}.py".format(padded_no), "w") as s:
|
||||||
s.write(
|
s.write(
|
||||||
f"""
|
f"""
|
||||||
|
|
@ -75,19 +68,14 @@ if __name__ == "__main__":
|
||||||
""".strip()
|
""".strip()
|
||||||
+ "\n"
|
+ "\n"
|
||||||
)
|
)
|
||||||
print(
|
|
||||||
f"""
|
|
||||||
Done! start coding.
|
|
||||||
|
|
||||||
Puzzle link:
|
|
||||||
https://adventofcode.com/{year}/day/{day_no}
|
|
||||||
|
|
||||||
Puzzle input (copy and paste to input/{day_no.zfill(2)}.txt):
|
|
||||||
https://adventofcode.com/{year}/day/{day_no}/input
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"\n\033[95m\033[1mAdvent of Code {year}\033[0m"
|
||||||
|
"\n###################"
|
||||||
|
"\n\n\033[96mby Anders Englöf Ytterström\033[0m"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
stars = 0
|
stars = 0
|
||||||
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import functools
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
|
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
|
||||||
|
|
@ -177,3 +176,13 @@ def dijkstras(grid, start, target):
|
||||||
heapq.heappush(queue, ("stuffs"))
|
heapq.heappush(queue, ("stuffs"))
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
def bk(graph, p, r=set(), x=set()):
|
||||||
|
"""Bron-Kerbosch algoritm, no pivot: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm"""
|
||||||
|
if not p and not x:
|
||||||
|
yield r
|
||||||
|
while p:
|
||||||
|
v = p.pop()
|
||||||
|
yield from bk(graph, p & set(graph[v]), r | {v}, x & graph[v])
|
||||||
|
x.add(v)
|
||||||
|
|
|
||||||
38
2024-python/output/day_23.py
Normal file
38
2024-python/output/day_23.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
from itertools import combinations
|
||||||
|
|
||||||
|
from output import bk
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
graph = defaultdict(set)
|
||||||
|
for line in data.splitlines():
|
||||||
|
a, b = line.strip().split("-")
|
||||||
|
graph[a].add(b)
|
||||||
|
graph[b].add(a)
|
||||||
|
triplets_connected = sum(
|
||||||
|
(
|
||||||
|
a in graph[b]
|
||||||
|
and a in graph[c]
|
||||||
|
and b in graph[a]
|
||||||
|
and b in graph[c]
|
||||||
|
and c in graph[a]
|
||||||
|
and a in graph[b]
|
||||||
|
and any(x.startswith("t") for x in [a, b, c])
|
||||||
|
)
|
||||||
|
for a, b, c in combinations(graph.keys(), r=3)
|
||||||
|
)
|
||||||
|
cliques = bk(graph, set(graph.keys()))
|
||||||
|
max_clique = sorted(cliques, key=lambda i: len(i), reverse=True)[0]
|
||||||
|
lan_password = ",".join(sorted(list(max_clique)))
|
||||||
|
return triplets_connected, lan_password
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/23.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
Loading…
Add table
Reference in a new issue