Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- instructions = []
- with open("12 - input", 'r') as file:
- lines = file.readlines()
- for line in lines:
- instructions.append(line.strip("\n"))
- class Ship:
- def __init__(self, lat=0, lon=0, bearing=0):
- self.lat = lat # N = +, S = -
- self.lon = lat # E = +, W = -
- self.bearing = bearing # N = 0, E = 90, S = 180, W = 270 0-359, L = -, R = +
- #self.latDist = 0
- #self.lonDist = 0
- self.print = False
- def getBearing(self):
- dir = ""
- if self.bearing == 0:
- return "N"
- elif self.bearing == 90:
- return "E"
- elif self.bearing == 180:
- return "S"
- elif self.bearing == 270:
- return "W"
- else:
- return self.bearing
- def getLocation(self):
- return f"Lat: {self.lat}, Lon: {self.lon}, Bear: {self.getBearing()}"
- def navigate(self, bearing, distance):
- if bearing == "N":
- self.lat += distance
- if self.print:
- print(f"Moving N\t{self.getLocation()}")
- elif bearing == "S":
- self.lat -= distance
- if self.print:
- print(f"Moving W\t{self.getLocation()}")
- elif bearing == "E":
- self.lon += distance
- if self.print:
- print(f"Moving E\t{self.getLocation()}")
- elif bearing == "W":
- self.lon -= distance
- if self.print:
- print(f"Moving W\t{self.getLocation()}")
- def parseOrder(self, instruction):
- cmd = instruction[0]
- param = int(instruction[1:])
- compassDirs = ["N", "S", "E", "W"]
- if cmd in compassDirs:
- self.navigate(cmd, param)
- elif cmd == "L":
- self.bearing -= param
- if self.bearing < 0:
- self.bearing = 360-abs(self.bearing)
- if self.print:
- print(f"New bearing\t{self.getBearing()}")
- elif cmd == "R":
- self.bearing += param
- if self.bearing > 359:
- self.bearing = self.bearing - 360
- if self.print:
- print(f"New bearing\t{self.getBearing()}")
- elif cmd == "F":
- self.navigate(self.getBearing(), param)
- boat = Ship(0, 0, 90)
- boat.print = True
- print(boat.getLocation())
- print("wtf")
- for instruction in instructions:
- boat.parseOrder(instruction)
- print(f"Manhattan distance = {abs(boat.lat)+abs(boat.lon)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement