infile="input" with open(infile, "r") as fp: moves = fp.readlines() counter = 50 code = 0 for idx, move in enumerate(moves): direction, number = move[0], int(move[1:]) assert direction in ['L', 'R'], f"Direction should be left (L) or right (R), not '{direction}'" assert number > 0, f"Number should be positive, not '{number}'" # Do counterthings prev_counter = counter sgn = 1 if direction == "R" else -1 counter += sgn*number # Going left is weird if direction == 'L': if prev_counter != 0 and counter % 100 == 0: code += 1 if prev_counter == 0 and counter % 100 != 0: code -= 1 # If we cross zero, let's store that. code += abs(counter // 100) counter %= 100 print("The actual password is:", code)