Guest User

Untitled

a guest
Dec 10th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. move_table = {
  2.     '[-1, 2]': [1, -1],
  3.     '[0, 2]': [0, -1],
  4.     '[1, 2]': [-1, -1],
  5.     '[-2, 1]': [1, -1],
  6.     '[2, 1]': [-1, -1],
  7.     '[-2, 0]': [1, 0],
  8.     '[2, 0]': [-1, 0],
  9.     '[-2, -1]': [1, 1],
  10.     '[2, -1]': [-1, 1],
  11.     '[-1, -2]': [1, 1],
  12.     '[0, -2]': [0, 1],
  13.     '[1, -2]': [-1, 1]
  14. }
  15.  
  16.  
  17. def move_parser_one(move, head_pos, tail_pos, tail_table):
  18.     direction, extent = move.split()[0], int(move.split()[1])
  19.     for x in range(extent):
  20.         if direction == 'U':
  21.             head_pos[1] += 1
  22.         elif direction == 'D':
  23.             head_pos[1] -= 1
  24.         elif direction == 'R':
  25.             head_pos[0] += 1
  26.         elif direction == 'L':
  27.             head_pos[0] -= 1
  28.         relative_tail = [tail_pos[0] - head_pos[0], tail_pos[1] - head_pos[1]]
  29.         distance = [abs(tail_pos[0] - head_pos[0]), abs(tail_pos[1] - head_pos[1])]
  30.         if distance[0] > 1 or distance[1] > 1:
  31.             adj = move_table[str(relative_tail)]
  32.             tail_pos = [tail_pos[0] + adj[0], tail_pos[1] + adj[1]]
  33.             tail_table.append(tail_pos)
  34.     return head_pos, tail_pos, tail_table
  35.  
  36.  
  37. def move_parser_two(move, two_pos, tail_table_two):
  38.     direction, extent = move.split()[0], int(move.split()[1])
  39.     for x in range(extent):
  40.         if direction == 'U':
  41.             two_pos[0][1] += 1
  42.         elif direction == 'D':
  43.             two_pos[0][1] -= 1
  44.         elif direction == 'R':
  45.             two_pos[0][0] += 1
  46.         elif direction == 'L':
  47.             two_pos[0][0] -= 1
  48.         for i, entry in enumerate(two_pos):
  49.             if i > 0:
  50.                 relative_tail = [two_pos[i][0] - two_pos[i-1][0],
  51.                                  two_pos[i][1] - two_pos[i-1][1]]
  52.                 distance = [abs(two_pos[i][0] - two_pos[i-1][0]),
  53.                             abs(two_pos[i][1] - two_pos[i-1][1])]
  54.                 if distance[0] > 1 or distance[1] > 1:
  55.                     adj = move_table[str(relative_tail)]
  56.                     two_pos[i] = [two_pos[i][0] + adj[0],
  57.                                   two_pos[i][1] + adj[1]]
  58.                 tail_table_two.append(two_pos[-1])
  59.     return two_pos, tail_table_two
  60.  
  61.  
  62. with open("input_9.txt", "r+")as input_file:
  63.     head_pos = [0, 0]
  64.     tail_pos = [0, 0]
  65.     two_pos = [[0, 0] for x in range(10)]
  66.     tail_table = [tail_pos]
  67.     tail_table_two = [two_pos[-1]]
  68.     instructions = []
  69.     for line in input_file:
  70.         instructions.append(line.strip("\n"))
  71.     for instruction in instructions:
  72.         head_pos, tail_pos, tail_table = move_parser_one(line, head_pos, tail_pos, tail_table)
  73.         two_pos, tail_table_two = move_parser_two(line, two_pos, tail_table_two)
  74.     print(len(set([str(x) for x in tail_table])))
  75.     print(len(set([str(x) for x in tail_table_two])))
  76.  
  77.  
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment