From b0c2ab4b946b0a89277b67a3033732cf6be888b0 Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Sun, 7 Dec 2025 14:17:28 +0100 Subject: [PATCH] 2025(07): this is recursive death --- 2025/07/part2.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 2025/07/part2.py diff --git a/2025/07/part2.py b/2025/07/part2.py new file mode 100644 index 0000000..f5d5ca5 --- /dev/null +++ b/2025/07/part2.py @@ -0,0 +1,61 @@ +import numpy as np +from part1 import load_diagram, print_diagram + +debug = False + +def quantum_propagate_diagram(in_diagram): + diagram = in_diagram.copy() + ylen = len(diagram) + + # Find start location + y0 = 0 + x0, = np.where(diagram[y0] == "S")[0] + + # Emit starting beam + diagram[y0 + 1, x0] = "|" + + # Propagate + worlds_count = quantum_propagate(x0, diagram[y0 + 2:]) + + return worlds_count + +def quantum_propagate(x, subdiagram): + ylen = len(subdiagram) + # print_diagram(subdiagram) + # print() + for y in range(0, ylen - 1): + if subdiagram[y, x] == ".": + # subdiagram[y, x] = "|" + if debug: + print("--- no split ---") + print_diagram(subdiagram) + print() + elif subdiagram[y, x] == "^": + # subdiagram[y, (x - 1, x + 1)] = "|" + # Split into two worlds, ignore what has happened before. + d1 = subdiagram[y + 1:]#.copy() + if debug: + print("--- split left ---") + print_diagram(d1) + print() + worlds_count = quantum_propagate(x - 1, d1) + d2 = subdiagram[y + 1:]#.copy() + if debug: + print("--- split right ---") + print_diagram(d2) + print() + worlds_count += quantum_propagate(x + 1, d2) + # print(worlds_count) + return worlds_count + # Reached the end, so this must be a world + if debug: + print("Reached end of world!") + return 1 + +if __name__ == "__main__": + test_diagram = load_diagram("testinput") + + assert quantum_propagate_diagram(test_diagram) == 40 + + diagram = load_diagram("input") + print(quantum_propagate_diagram(diagram))