Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active December 23, 2017 19:17
Show Gist options
  • Save spicydog/3415e75c7216a8f3a9aae77df2cd4ee1 to your computer and use it in GitHub Desktop.
Save spicydog/3415e75c7216a8f3a9aae77df2cd4ee1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# # -*- coding: utf-8 -*-
"""
__project__ = "MAQE Bot"
__reference__ = "https://maqe.github.io/maqe-bot.html"
__author__ = "Spicydog Proxy"
"""
def translate(input):
commands = []
buffer = ''
for c in input:
if (c in ['W','L','R']):
# If it is a command
if len(buffer) > 0:
# Get the stepping buffer and clear it
commands.append({'name':'W', 'step':int(buffer)})
buffer = ''
if c != 'W':
# Add the new command if not a walking step
commands.append({'name':c, 'step':0})
else:
# It is a walking step, add to buffer
if (c in ['0','1','2','3','4','5','6','7','8','9']):
buffer += c
if len(buffer) > 0:
# If the last step is walking, so do walk
commands.append({'name':'W', 'step':int(buffer)})
return commands;
def walk(step, direction, x, y):
if direction == 0: # N
y += step;
elif direction == 1: # E
x += step;
elif direction == 2: # S
y -= step;
elif direction == 3: # W
x -= step;
return [x, y];
def turn(turn, direction):
if (turn == 'R'):
direction += 1
if (turn == 'L'):
direction -= 1
direction %= 4;
return direction
# Run the bot with full instruction string
def run(str):
direction, x, y = 0,0,0
commands = translate(str)
for command in commands:
if command['name'] == 'W':
[x, y] = walk(command['step'], direction, x, y)
else:
direction = turn(command['name'], direction)
direction = ['North', 'East', 'South', 'West'][direction]
return "X: %d Y: %d Direction: %s" % (x, y, direction)
import sys
input = ''
if len(sys.argv) == 2:
input = sys.argv[1]
output = run(input)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment