Day 12
[advent-of-code-2020.git] / day12 / ship.py
diff --git a/day12/ship.py b/day12/ship.py
new file mode 100755 (executable)
index 0000000..6219442
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/python3
+
+ship_direction="E"
+ship_horz=0
+ship_vert=0
+directions=["E", "S", "W", "N"]
+
+def do_move(direction, amount):
+    global ship_horz
+    global ship_vert
+    if direction in ["E", "W"]:
+        if direction == "W":
+            amount=amount*-1
+        ship_horz+=amount
+    elif direction in ["N", "S"]:
+        if direction == "S":
+            amount=amount*-1
+        ship_vert+=amount
+
+def turn(amount,multiplier):
+    global ship_direction
+    global directions
+
+    dir_pos=0
+    for a,dir2 in enumerate(directions):
+        if dir2 == ship_direction:
+            dir_pos=a
+            break
+
+    print("Loc: {}".format(dir_pos))
+
+    dir_pos+=int((int(amount) / 90) * multiplier)
+    dir_pos=(dir_pos % len(directions))
+    print("New loc: {}".format(dir_pos))
+    print("Started in direction {}, changing to direction {}, after getting told to go {}".format(ship_direction, directions[dir_pos], (int(amount) * multiplier)))
+    ship_direction=directions[dir_pos]
+
+def turn_right(amount):
+    turn(amount, 1)
+
+def turn_left(amount):
+    turn(amount, -1)
+
+for line in open("input.txt", "r"):
+    line=line.rstrip()
+    command=line[0]
+    amount=int(line[1:])
+
+    if command in [ "N", "S", "E", "W" ]:
+        do_move(command, amount)
+    elif command == "R":
+        turn_right(amount)
+    elif command == "L":
+        turn_left(amount)
+    elif command == "F":
+        do_move(ship_direction, amount)
+
+print("Moved to {}, {} - MD: {}".format(ship_horz,ship_vert,(abs(ship_horz)+abs(ship_vert))))