From 8b771bc4d91be9da3e158333b6e6b5df574453ff Mon Sep 17 00:00:00 2001 From: StarlitGhost <679547+StarlitGhost@users.noreply.github.com> Date: Mon, 25 Dec 2023 17:34:14 +0000 Subject: [PATCH] [2023/25] solved --- 2023/25/example | 13 +++++++++++++ 2023/25/script.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 2023/25/example create mode 100644 2023/25/script.py diff --git a/2023/25/example b/2023/25/example new file mode 100644 index 0000000..bbfda0b --- /dev/null +++ b/2023/25/example @@ -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 diff --git a/2023/25/script.py b/2023/25/script.py new file mode 100644 index 0000000..9293e9f --- /dev/null +++ b/2023/25/script.py @@ -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()