]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day12/ship.py
Day 13 - forays in to using Diophantine did not go well.
[advent-of-code-2020.git] / day12 / ship.py
1 #!/usr/bin/python3
2
3 ship_direction="E"
4 ship_horz=0
5 ship_vert=0
6 directions=["E", "S", "W", "N"]
7
8 def do_move(direction, amount):
9     global ship_horz
10     global ship_vert
11     if direction in ["E", "W"]:
12         if direction == "W":
13             amount=amount*-1
14         ship_horz+=amount
15     elif direction in ["N", "S"]:
16         if direction == "S":
17             amount=amount*-1
18         ship_vert+=amount
19
20 def turn(amount,multiplier):
21     global ship_direction
22     global directions
23
24     dir_pos=0
25     for a,dir2 in enumerate(directions):
26         if dir2 == ship_direction:
27             dir_pos=a
28             break
29
30     print("Loc: {}".format(dir_pos))
31
32     dir_pos+=int((int(amount) / 90) * multiplier)
33     dir_pos=(dir_pos % len(directions))
34     print("New loc: {}".format(dir_pos))
35     print("Started in direction {}, changing to direction {}, after getting told to go {}".format(ship_direction, directions[dir_pos], (int(amount) * multiplier)))
36     ship_direction=directions[dir_pos]
37
38 def turn_right(amount):
39     turn(amount, 1)
40
41 def turn_left(amount):
42     turn(amount, -1)
43
44 for line in open("input.txt", "r"):
45     line=line.rstrip()
46     command=line[0]
47     amount=int(line[1:])
48
49     if command in [ "N", "S", "E", "W" ]:
50         do_move(command, amount)
51     elif command == "R":
52         turn_right(amount)
53     elif command == "L":
54         turn_left(amount)
55     elif command == "F":
56         do_move(ship_direction, amount)
57
58 print("Moved to {}, {} - MD: {}".format(ship_horz,ship_vert,(abs(ship_horz)+abs(ship_vert))))