From 2d48f385114bbe1eb6703ebe36b38cd637723b36 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 30 Jul 2025 13:43:37 +0200 Subject: [PATCH] 2019 day6 --- 2019/day6/day6.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2019/day6/day6.py diff --git a/2019/day6/day6.py b/2019/day6/day6.py new file mode 100644 index 0000000..cebeeb7 --- /dev/null +++ b/2019/day6/day6.py @@ -0,0 +1,46 @@ +from collections import defaultdict + + +def path(graph, start, goal): + path = [] + node = start + while node != goal: + node = graph[node] + path.append(node) + return path + + +def find_first_common(x, y): + "returns the indices i and j of the first common element of x and y" + for i, xx in enumerate(x): + for j, yy in enumerate(y): + if xx == yy: + return i, j + + +def main(inp): + graph = {} + for l in inp: + left, right = l.rstrip().split(")") + graph[right] = left + + total = 0 + for node, child in graph.items(): + cnt = 1 + while child != "COM": + child = graph[child] + cnt += 1 + total += cnt + print("Part 1: ", total) + + p1 = path(graph, "YOU", "COM") + p2 = path(graph, "SAN", "COM") + a, b = find_first_common(p1, p2) + total_path = p1[:a] + p2[b::-1] # p1 + p2 - p1 n p2 + print("Part 2: ", len(total_path) - 1) + + +if __name__ == "__main__": + import fileinput + main(fileinput.input()) +