2025(07): inspired by Marit, a new, better function

This commit is contained in:
2025-12-08 08:06:25 +01:00
parent b0c2ab4b94
commit f648a40dd6

View File

@ -52,10 +52,35 @@ def quantum_propagate(x, subdiagram):
print("Reached end of world!")
return 1
def sninkogate(diagram):
ylen = len(diagram)
# Find start location
y0 = 0
x0, = np.where(diagram[y0] == "S")[0]
# Save worlds per column
I = np.zeros_like(diagram, dtype=int)
# Emit starting beam
I[y0 + 1, x0] = 1
for y in range(2, ylen):
for x in np.where((diagram[y] == "^")&(I[y - 1] > 0))[0]:
# NOTE: I do not ever check whether carets are on the boundary.
I[y, (x - 1, x + 1)] += I[y - 1, x]
for x in np.where((diagram[y] == ".")&(I[y - 1] > 0))[0]:
I[y, x] += I[y - 1, x]
if debug:
print_diagram(diagram)
print()
return I[-1].sum()
if __name__ == "__main__":
test_diagram = load_diagram("testinput")
assert quantum_propagate_diagram(test_diagram) == 40
diagram = load_diagram("input")
print(quantum_propagate_diagram(diagram))
print(sninkogate(diagram))