2025(07): this is recursive death
This commit is contained in:
61
2025/07/part2.py
Normal file
61
2025/07/part2.py
Normal file
@ -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))
|
||||||
Reference in New Issue
Block a user