diff --git a/12/12-1.py b/12/12-1.py new file mode 100644 index 0000000..ab6147a --- /dev/null +++ b/12/12-1.py @@ -0,0 +1,57 @@ +#!/usr/bin/python3 + +import sys + +class Ship: + + def __init__(self): + self.facing = 'E' + self.x = 0 + self.y = 0 + + self.moves = {'E': [1,0], + 'S': [0,-1], + 'W': [-1,0], + 'N': [0,1]} + + self.turns = {'E': ['N','S','W'], + 'S': ['E','W','N'], + 'W': ['S','N','E'], + 'N': ['W','E','S']} + + def rotate(self, direction, degrees): + if degrees == 180: + self.facing = self.turns[self.facing][2] + elif degrees == 90 and direction == 'L': + self.facing = self.turns[self.facing][0] + elif degrees == 90 and direction == 'R': + self.facing = self.turns[self.facing][1] + elif degrees == 270 and direction == 'L': + self.facing = self.turns[self.facing][1] + elif degrees == 270 and direction == 'R': + self.facing = self.turns[self.facing][0] + else: + raise Exception("Invalid Turn") + + def move(self, direction, num): + if direction == 'F': + self.x = self.x + (self.moves[self.facing][0] * num) + self.y = self.y + (self.moves[self.facing][1] * num) + else: + self.x = self.x + (self.moves[direction][0] * num) + self.y = self.y + (self.moves[direction][1] * num) + + def do(self, instruction, unit): + if instruction in ['L', 'R']: + self.rotate(instruction, unit) + else: + self.move(instruction, unit) + + +ship = Ship() +for line in sys.stdin: + instruction = line[0] + unit = int(line[1:]) + ship.do(instruction, unit) + +print(1,abs(ship.x) + abs(ship.y)) diff --git a/12/12-2.py b/12/12-2.py new file mode 100644 index 0000000..3a2d9d4 --- /dev/null +++ b/12/12-2.py @@ -0,0 +1,62 @@ +#!/usr/bin/python3 + +import sys + +class Ship: + + def __init__(self): + self.facing = 'E' + self.wp_x = 10 + self.wp_y = 1 + self.x = 0 + self.y = 0 + + self.moves = {'E': [1,0], + 'S': [0,-1], + 'W': [-1,0], + 'N': [0,1]} + + self.turns = {'E': ['N','S','W'], + 'S': ['E','W','N'], + 'W': ['S','N','E'], + 'N': ['W','E','S']} + + def rotate(self, direction, degrees): + if degrees == 180: + self.wp_x = self.wp_x * -1 + self.wp_y = self.wp_y * -1 + elif (degrees == 90 and direction == 'L') or (degrees == 270 and direction == 'R'): + t = self.wp_x + self.wp_x = self.wp_y * -1 + self.wp_y = t + elif (degrees == 90 and direction == 'R') or (degrees == 270 and direction == 'L'): + t = self.wp_x + self.wp_x = self.wp_y + self.wp_y = t * -1 + else: + raise Exception("Invalid Turn") + + def movewp(self, direction, num): + self.wp_x = self.wp_x + (self.moves[direction][0] * num) + self.wp_y = self.wp_y + (self.moves[direction][1] * num) + + def move(self, num): + self.x = self.x + (self.wp_x * num) + self.y = self.y + (self.wp_y * num) + + def do(self, instruction, unit): + if instruction == 'F': + self.move(unit) + elif instruction in ['L', 'R']: + self.rotate(instruction, unit) + else: + self.movewp(instruction, unit) + + +ship = Ship() +for line in sys.stdin: + instruction = line[0] + unit = int(line[1:]) + ship.do(instruction, unit) + +print(2,abs(ship.x) + abs(ship.y))