Advertisement
Armandur

Untitled

Dec 17th, 2020
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. instructions = []
  2. with open("12 - input", 'r') as file:
  3.     lines = file.readlines()
  4.     for line in lines:
  5.         instructions.append(line.strip("\n"))
  6.  
  7.  
  8. class Ship:
  9.     def __init__(self, lat=0, lon=0, bearing=0):
  10.         self.lat = lat        #   N = +, S = -
  11.         self.lon = lat        #   E = +, W = -
  12.         self.bearing = bearing    #   N = 0, E = 90, S = 180, W = 270 0-359, L = -, R = +
  13.  
  14.         #self.latDist = 0
  15.         #self.lonDist = 0
  16.  
  17.         self.print = False
  18.  
  19.     def getBearing(self):
  20.         dir = ""
  21.         if self.bearing == 0:
  22.             return "N"
  23.         elif self.bearing == 90:
  24.             return "E"
  25.         elif self.bearing == 180:
  26.             return "S"
  27.         elif self.bearing == 270:
  28.             return "W"
  29.         else:
  30.             return self.bearing
  31.  
  32.     def getLocation(self):
  33.         return f"Lat: {self.lat}, Lon: {self.lon}, Bear: {self.getBearing()}"
  34.  
  35.     def navigate(self, bearing, distance):
  36.         if bearing == "N":
  37.             self.lat += distance
  38.  
  39.             if self.print:
  40.                 print(f"Moving N\t{self.getLocation()}")
  41.  
  42.         elif bearing == "S":
  43.             self.lat -= distance
  44.  
  45.             if self.print:
  46.                 print(f"Moving W\t{self.getLocation()}")
  47.  
  48.         elif bearing == "E":
  49.             self.lon += distance
  50.  
  51.             if self.print:
  52.                 print(f"Moving E\t{self.getLocation()}")
  53.  
  54.         elif bearing == "W":
  55.             self.lon -= distance
  56.             if self.print:
  57.                 print(f"Moving W\t{self.getLocation()}")
  58.  
  59.     def parseOrder(self, instruction):
  60.         cmd = instruction[0]
  61.         param = int(instruction[1:])
  62.  
  63.         compassDirs = ["N", "S", "E", "W"]
  64.  
  65.         if cmd in compassDirs:
  66.             self.navigate(cmd, param)
  67.  
  68.         elif cmd == "L":
  69.             self.bearing -= param
  70.             if self.bearing < 0:
  71.                 self.bearing = 360-abs(self.bearing)
  72.  
  73.             if self.print:
  74.                 print(f"New bearing\t{self.getBearing()}")
  75.  
  76.         elif cmd == "R":
  77.             self.bearing += param
  78.             if self.bearing > 359:
  79.                 self.bearing = self.bearing - 360
  80.  
  81.             if self.print:
  82.                 print(f"New bearing\t{self.getBearing()}")
  83.  
  84.         elif cmd == "F":
  85.             self.navigate(self.getBearing(), param)
  86.  
  87.  
  88. boat = Ship(0, 0, 90)
  89. boat.print = True
  90.  
  91. print(boat.getLocation())
  92. print("wtf")
  93.  
  94. for instruction in instructions:
  95.     boat.parseOrder(instruction)
  96.  
  97. print(f"Manhattan distance = {abs(boat.lat)+abs(boat.lon)}")
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement