7 directions=["E", "S", "W", "N"]
11 def do_waypoint_move(direction, amount):
14 if direction in ["E", "W"]:
18 elif direction in ["N", "S"]:
23 def turn(amount,multiplier):
24 global waypoint_horz,waypoint_vert
25 steps=int(amount / 90)
26 steps=steps * multiplier
28 print("Started at: {},{} rotating {}".format(waypoint_horz,waypoint_vert,amount*multiplier))
29 print("Steps: {}".format(steps))
30 # translate left in to moves right
34 if steps == 4 or steps == 0:
37 print("Steps: {}".format(steps))
39 # and now we just step round that many steps to the right
40 # now 1,2 will go round to 2,-1, i.e. x,y -> y,-x
41 for step in range(steps):
43 waypoint_vert=(waypoint_horz*-1)
46 print("Ended at: {},{}".format(waypoint_horz,waypoint_vert))
48 def do_ship_move(amount):
49 global waypoint_horz,waypoint_vert,ship_x,ship_y
50 print("Waypoint {},{}, Ship {},{}, Amount {}".format(waypoint_horz, waypoint_vert, ship_x, ship_y, amount))
51 ship_x+=(amount*waypoint_horz)
52 ship_y+=(amount*waypoint_vert)
53 print("Ship {},{}".format(ship_x, ship_y))
55 def turn_right(amount):
58 def turn_left(amount):
66 for line in open(filename, "r"):
71 if command in [ "N", "S", "E", "W" ]:
72 do_waypoint_move(command, amount)
80 print("Moved to {}, {} - MD: {}".format(ship_x,ship_y,(abs(ship_x)+abs(ship_y))))