GlitchBot

This commit is contained in:
2023-11-15 22:47:23 +01:00
parent b58f0998db
commit 799f2283c7

56
glitchbot.py Normal file
View File

@@ -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()