import numpy as np lines = ((x[0], int(x[1:].strip())) for x in open("input.txt")) pos = np.array([0, 0], dtype=np.int32) drctn = np.array([10, 1], dtype=np.int32) targets = np.array([ [0, 1], [1, 0], [0, -1], [-1, 0] ], dtype=np.int32) target_str = "NESW" def direction(target): return targets[target_str.index(target)] def rotate(current, amount): angle = np.arctan2(current[1], current[0]) + np.pi / 180 * amount amount = np.linalg.norm(current) return np.array([np.rint(np.cos(angle) * amount), np.rint(np.sin(angle) * amount)], dtype=np.int32) for line in lines: print(line) if line[0] in target_str: drctn += direction(line[0]) * line[1] elif line[0] in "LR": drctn = rotate(drctn, -line[1] if line[0] == "R" else line[1]) else: pos += drctn * line[1] print(pos, drctn) print(sum(np.abs(pos)))