def read_file(): with open("input.txt","r") as f: return f.read().split('\n') def navigate(instructions): direction = "E" x = 0 y = 0 for instruction in instructions: action = instruction[0] value = int(instruction[1:]) if action == "N": y += value elif action == "S": y -= value elif action == "E": x += value elif action == "W": x -= value elif action == "F": if direction == "N": y += value elif direction == "S": y -= value elif direction == "E": x += value elif direction == "W": x -= value elif action == "L": if direction == "E": if value == 90: direction = "N" elif value == 180: direction = "W" elif value == 270: direction = "S" elif direction == "N": if value == 90: direction = "W" elif value == 180: direction = "S" elif value == 270: direction = "E" elif direction == "W": if value == 90: direction = "S" elif value == 180: direction = "E" elif value == 270: direction = "N" elif direction == "S": if value == 90: direction = "E" elif value == 180: direction = "N" elif value == 270: direction = "W" elif action == "R": if direction == "E": if value == 90: direction = "S" elif value == 180: direction = "W" elif value == 270: direction = "N" elif direction == "S": if value == 90: direction = "W" elif value == 180: direction = "N" elif value == 270: direction = "E" elif direction == "W": if value == 90: direction = "N" elif value == 180: direction = "E" elif value == 270: direction = "S" elif direction == "N": if value == 90: direction = "E" elif value == 180: direction = "S" elif value == 270: direction = "W" return x, y, direction def navigate2(instructions): x = 0 y = 0 wx = 10 wy = 1 for instruction in instructions: action = instruction[0] value = int(instruction[1:]) if action == "N": wy += value elif action == "S": wy -= value elif action == "E": wx += value elif action == "W": wx -= value elif action == "F": x += value * wx y += value * wy elif action == "L": if value == 90: wx, wy = -wy, wx elif value == 180: wx, wy = -wx, -wy elif value == 270: wx, wy = wy, -wx elif action == "R": if value == 90: wx, wy = wy, -wx elif value == 180: wx, wy = -wx, -wy elif value == 270: wx, wy = -wy, wx return x, y, wx, wy def part1(instructions): x, y, direction = navigate(instructions) return abs(x) + abs(y) def part2(instructions): x, y, wx, wy = navigate2(instructions) return abs(x) + abs(y) def main(): instructions = read_file() print(part1(instructions), part2(instructions)) if __name__ == "__main__": main()