Skip to content

Commit

Permalink
[2023/25] solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 25, 2023
1 parent 92a3aae commit 8b771bc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 2023/25/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr
47 changes: 47 additions & 0 deletions 2023/25/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from GhostyUtils import aoc
from collections import defaultdict, Counter
import random


def bfs(components, start, end):
visited = set()
frontier = [[start]]
while frontier:
path = frontier.pop(0)
node = path[-1]
if node == end:
return path
for new_node in components[node]:
if new_node in visited:
continue
visited.add(new_node)
frontier.append(path + [new_node])
return visited


def main():
components = defaultdict(set)
for wires in aoc.read_lines():
comp, connections = wires.split(': ')
components[comp].update(connections.split())
for other_comp in components[comp]:
components[other_comp].add(comp)

seen = defaultdict(int)
for _ in range(1000):
comps = random.sample(list(components.keys()), 2)
path = bfs(components, comps[0], comps[1])
for link in zip(path, path[1:]):
seen[tuple(sorted(link))] += 1
snips = Counter(seen).most_common(3)
for snip in snips:
l, r = snip[0]
components[l].remove(r)
components[r].remove(l)
one = bfs(components, snips[0][0][0], None)
two = bfs(components, snips[0][0][1], None)
print('p1:', len(one) * len(two), f'| one: {len(one)} two: {len(two)}')


if __name__ == "__main__":
main()

0 comments on commit 8b771bc

Please sign in to comment.