import numpy as np debug = False def load_diagram(filename: str): # Read file as list of strings with open(filename, "r") as fp: data = fp.read().splitlines() data_arr = np.array(data, dtype=str)\ .view("U1")\ .reshape((len(data), -1)) return data_arr def print_diagram(diagram): for line in diagram: for char in line: print(char, end="") print() def propagate_diagram(in_diagram, inplace=True): diagram = in_diagram.copy() if not inplace else in_diagram ylen = len(diagram) # Find start location y0 = 0 x0, = np.where(diagram[y0] == "S")[0] # Emit starting beam diagram[y0 + 1, x0] = "|" splits_count = 0 # Propagate the beams, starting with y = 2, as the y = 1 layer has our first beam for y in range(2, ylen): for x in np.where((diagram[y] == "^")&(diagram[y - 1] == "|"))[0]: # NOTE: I do not ever check whether carets are on the boundary. diagram[y, (x - 1, x + 1)] = "|" splits_count += 1 for x in np.where((diagram[y] == ".")&(diagram[y - 1] == "|"))[0]: diagram[y, x] = "|" if debug: print_diagram(diagram) print() return splits_count if __name__ == "__main__": test_diagram = load_diagram("testinput") assert propagate_diagram(test_diagram) == 21 diagram = load_diagram("input") print(propagate_diagram(diagram)) def gnonkel(x, a): if x: a += 1 # ----- if x and y: a+=1 elif x: a+=1