-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92a3aae
commit 8b771bc
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |