Created
February 15, 2017 15:28
-
-
Save MinimumViablePerson/4affa85655a6b33a4c34c08e4de64f1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
PLATEAU_SIZE = (file.readline().strip('\n').split()) | |
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