Skip to content

Instantly share code, notes, and snippets.

@MinimumViablePerson
Created February 15, 2017 15:26
Show Gist options
  • Save MinimumViablePerson/e96a14fd6ea7e3ff26fec20e6ab436fe to your computer and use it in GitHub Desktop.
Save MinimumViablePerson/e96a14fd6ea7e3ff26fec20e6ab436fe to your computer and use it in GitHub Desktop.
class Coords(tuple):
def __new__(cls, x, y):
return tuple.__new__(cls, (x, y))
def __add__(self, other):
return Coords(*([sum(x) for x in zip(self, other)]))
def __sub__(self, other):
return self.__add__(-i for i in other)
class Rover:
def __init__(self, x, y, orientation):
self.position = Coords(int(x), int(y))
self.orientation = orientation
self.ORIENTATIONS = 'NESW'
def turn_left(self):
self.orientation = self.ORIENTATIONS[self.ORIENTATIONS.index(self.orientation) -1]
def turn_right(self):
self.orientation = self.ORIENTATIONS[self.ORIENTATIONS.index(self.orientation) - (len(self.ORIENTATIONS) - 1)]
def move_forward(self):
directions = {
'N': (0, 1),
'E': (1, 0),
'S': (0, -1),
'W': (-1, 0)
}
self.position += directions[self.orientation]
def do_instructions(self, instructions):
for i in instructions:
if i == 'L':
self.turn_left()
if i == 'R':
self.turn_right()
if i == 'M':
self.move_forward()
print("{} {} {}".format(self.position[0],
self.position[1],
self.orientation))
file = open('input.txt')
PLATEU_SIZE = (file.readline().strip('\n').split())
ROVERS = []
x, y, orientation = file.readline().strip('\n').split()
instructions = file.readline()
rover = Rover(x, y, orientation)
rover.do_instructions(instructions)
x, y, orientation = file.readline().strip('\n').split()
instructions = file.readline()
rover2 = Rover(x, y, orientation)
rover2.do_instructions(instructions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment