Advertisement
Guest User

Untitled

a guest
Dec 9th, 2022
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | Software | 0 0
  1. def read_file(filename):
  2.     with open(f"{filename}", "r") as infile:
  3.         output = infile.read().splitlines()
  4.     return output
  5.  
  6. class Location:
  7.     def __init__(self, x, y):
  8.         self.x = x
  9.         self.y = y
  10.  
  11.     @property
  12.     def location(self):
  13.         return (self.x, self.y)
  14.  
  15.     def move(self, direction, number=1):
  16.         if direction.upper() == "L":
  17.             self.move_left()
  18.         elif direction.upper() == "R":
  19.             self.move_right()
  20.         elif direction.upper() == "U":
  21.             self.move_up()
  22.         elif direction.upper() == "D":
  23.             self.move_down()
  24.         else:
  25.             raise ValueError
  26.  
  27.     def move_left(self):
  28.         self.x = self.x - 1
  29.  
  30.     def move_right(self):
  31.         self.x = self.x + 1
  32.    
  33.     def move_up(self):
  34.         self.y = self.y + 1
  35.  
  36.     def move_down(self):
  37.         self.y = self.y - 1
  38.  
  39.  
  40. def main(knots_to_track):
  41.     lines = read_file("inputs/9.csv")
  42.    
  43.     knots = []
  44.     for i in range(knots_to_track):
  45.         knots.append(Location(0,0))
  46.  
  47.     tail_locations = [knots[-1:][0].location]
  48.  
  49.     for line in lines:
  50.         direction_to_move = line.split(" ")[0]
  51.         spaces_to_move = int(line.split(" ")[1])
  52.         for i in range(spaces_to_move):
  53.             knots[0].move(direction_to_move)
  54.             for j in range(1,len(knots)):
  55.                 delta = Location(knots[j-1].x - knots[j].x, knots[j-1].y - knots[j].y)
  56.                 if abs(delta.x) <= 1 and abs(delta.y) <= 1:
  57.                     pass
  58.                 else:
  59.                     if delta.location == (2,0):
  60.                         knots[j].move_right()
  61.                     elif delta.location == (-2,0):
  62.                         knots[j].move_left()
  63.                     elif delta.location == (0,2):
  64.                         knots[j].move_up()
  65.                     elif delta.location == (0,-2):
  66.                         knots[j].move_down()
  67.                     elif delta.x > 0 and delta.y > 0:
  68.                         knots[j].move_right()
  69.                         knots[j].move_up()
  70.                     elif delta.x > 0 and delta.y < 0:
  71.                         knots[j].move_right()
  72.                         knots[j].move_down()  
  73.                     elif delta.x < 0 and delta.y > 0:
  74.                         knots[j].move_left()
  75.                         knots[j].move_up()  
  76.                     elif delta.x < 0 and delta.y < 0:
  77.                         knots[j].move_left()
  78.                         knots[j].move_down()
  79.  
  80.             tail_locations.append(knots[-1:][0].location)
  81.  
  82.     print(len(set(tail_locations)))
  83.  
  84. if __name__ == "__main__":
  85.     main(2)
  86.     main(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement