From 799f2283c754c41810894db58b15484dd012224c Mon Sep 17 00:00:00 2001 From: Jethro Stapelbroek Date: Wed, 15 Nov 2023 22:47:23 +0100 Subject: [PATCH] GlitchBot --- glitchbot.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 glitchbot.py diff --git a/glitchbot.py b/glitchbot.py new file mode 100644 index 0000000..5cd88db --- /dev/null +++ b/glitchbot.py @@ -0,0 +1,56 @@ +NORTH = 0 +EAST = 1 +SOUTH = 2 +WEST = 3 + +LEFT = 0 +RIGHT = 1 +FORWARD = 2 + + +def run_program(program: list) -> tuple: + x, y = 0, 0 + facing = NORTH + + for instruction in program: + if instruction == LEFT: + facing = (facing - 1) % 4 + + elif instruction == RIGHT: + facing = (facing + 1) % 4 + + elif instruction == FORWARD: + if facing == NORTH: + y += 1 + elif facing == SOUTH: + y -= 1 + elif facing == EAST: + x += 1 + elif facing == WEST: + x -= 1 + + return x, y + + +x, y = map(int, input().split()) +instruction_count = int(input()) + +program = [] +for _ in range(instruction_count): + instruction = input() + if instruction == "Left": + program.append(LEFT) + elif instruction == "Right": + program.append(RIGHT) + elif instruction == "Forward": + program.append(FORWARD) + +for i in range(len(program)): + for _ in range(3): + program[i] = (program[i] + 1) % 3 + result = run_program(program) + if result == (x, y): + print( + f"{i+1} {'Left' if program[i] == LEFT else 'Right' if program[i] == RIGHT else 'Forward'}" + ) + exit()