Advertisement
Guest User

YOM2 - Advent of Code 2024 Day 21

a guest
Dec 21st, 2024
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. from functools import cache
  2.  
  3. with open('input/Day21.txt', 'r') as file:
  4.     codes = file.read().splitlines()
  5.  
  6. # Coords are such that (0,0) is the empty space on both
  7. keypad = {
  8.     '7' : (0,-3), '8' : (1,-3), '9' : (2,-3),
  9.     '4' : (0,-2), '5' : (1,-2), '6' : (2,-2),
  10.     '1' : (0,-1), '2' : (1,-1), '3' : (2,-1),
  11.                   '0' : (1, 0), 'A' : (2, 0)
  12.     }
  13.  
  14. dpad = {
  15.                   '^' : (1, 0), 'A' : (2, 0),
  16.     '<' : (0, 1), 'v' : (1, 1), '>' : (2, 1)
  17.     }
  18.  
  19. def vectorDif(v1,v2):
  20.     return (v1[0]-v2[0], v1[1]-v2[1])
  21.  
  22. @cache
  23. def calcLength(target, cur_pos, max_depth):
  24.     target_pos, num_presses = target
  25.     if max_depth == 0:
  26.         return num_presses
  27.     length = 0
  28.     xdif, ydif = vectorDif(cur_pos, target_pos)
  29.     if xdif < 0:
  30.         xmov = (dpad['>'], -xdif)
  31.     else:
  32.         xmov = (dpad['<'],  xdif)
  33.     if ydif < 0:
  34.         ymov = (dpad['v'], -ydif)
  35.     else:
  36.         ymov = (dpad['^'],  ydif)
  37.     endmov = (dpad['A'], num_presses)
  38.  
  39.     if xdif == 0:
  40.         length += calcLength(  ymov, dpad['A'], max_depth-1)
  41.         length += calcLength(endmov,   ymov[0], max_depth-1)
  42.     elif ydif == 0:
  43.         length += calcLength(  xmov, dpad['A'], max_depth-1)
  44.         length += calcLength(endmov,   xmov[0], max_depth-1)
  45.     elif cur_pos[1] == 0 and target_pos[0] == 0:
  46.         # Avoiding (0,0) is the highst priority
  47.         length += calcLength(  ymov, dpad['A'], max_depth-1)
  48.         length += calcLength(  xmov,   ymov[0], max_depth-1)
  49.         length += calcLength(endmov,   xmov[0], max_depth-1)
  50.     elif (cur_pos[0] == 0 and target_pos[1] == 0) or xdif > 0:
  51.         # '<' is the furthest from 'A', and itself takes two '<' pushes to reach
  52.         # Move to it first when possible to avoid splitting the two '<'
  53.         length += calcLength(  xmov, dpad['A'], max_depth-1)
  54.         length += calcLength(  ymov,   xmov[0], max_depth-1)
  55.         length += calcLength(endmov,   ymov[0], max_depth-1)
  56.     else:
  57.         length += calcLength(  ymov, dpad['A'], max_depth-1)
  58.         length += calcLength(  xmov,   ymov[0], max_depth-1)
  59.         length += calcLength(endmov,   xmov[0], max_depth-1)
  60.     return length
  61.  
  62. complexity1 = 0
  63. complexity2 = 0
  64. for code in codes:
  65.     length1 = 0
  66.     length2 = 0
  67.     for i in range(len(code)):
  68.         length1 += calcLength((keypad[code[i]],1), keypad[code[i-1]], 3)
  69.         length2 += calcLength((keypad[code[i]],1), keypad[code[i-1]], 26)
  70.     complexity1 += int(code[:-1]) * length1
  71.     complexity2 += int(code[:-1]) * length2
  72.  
  73. print(complexity1)
  74. print(complexity2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement