Advertisement
Guest User

AOC2022_Day17

a guest
Dec 17th, 2022
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | Source Code | 0 0
  1. tower = []
  2. highest = 0
  3.  
  4. shapes = list(patterns.keys())
  5. wind_pattern_count = len(line)
  6.  
  7. iteration_count = 0
  8. cache = {}
  9. rock_height_cache = {}
  10. break_outer_loop = False
  11. rocks_to_drop = 1000000000000
  12. for rock_number in tqdm(range(rocks_to_drop)):
  13. to_add = 10 - (len(tower) - highest)
  14. for k in range(to_add):
  15. tower.append([0 for i in range(7)])
  16.  
  17. rock = shapes[rock_number % 5]
  18. rock_pattern = patterns[rock]
  19. left_edge = left_edges[rock]
  20. right_edge = right_edges[rock]
  21. bottom_edge = bottom_edges[rock]
  22.  
  23. rock_y = highest + 3
  24. rock_x = 2
  25. rock_height = len(rock_pattern)
  26. rock_width = len(rock_pattern[0])
  27. start_wind = iteration_count % wind_pattern_count
  28. while True:
  29. end_wind = iteration_count % wind_pattern_count
  30. wind_pattern = line[end_wind]
  31. wind_pattern = 1 if wind_pattern == '>' else -1
  32. iteration_count += 1
  33.  
  34. is_safe = True
  35. if wind_pattern == 1:
  36. if rock_x + rock_width > 6:
  37. is_safe = False
  38. else:
  39. is_safe = not any(tower[rock_y + p][rock_x + x_offset] == 1 for p, x_offset in enumerate(right_edge))
  40. else:
  41.  
  42. if rock_x - 1 < 0:
  43. is_safe = False
  44. else:
  45. is_safe = not any(tower[rock_y + p][rock_x + x_offset] == 1 for p, x_offset in enumerate(left_edge))
  46.  
  47. if is_safe:
  48. rock_x += wind_pattern
  49.  
  50. is_settled = False
  51. if rock_y == 0:
  52. is_settled = True
  53. else:
  54. is_settled = any(tower[rock_y + y_offset][rock_x + p] == 1 for p, y_offset in enumerate(bottom_edge))
  55.  
  56. if is_settled:
  57. for i in range(rock_height):
  58. for j in range(rock_width):
  59. if rock_pattern[i][j] == 1:
  60. tower[i+rock_y][j+rock_x] = rock_pattern[i][j]
  61. highest = max(highest, rock_y + rock_height)
  62. if highest == rock_height + rock_y:
  63. if (rock, start_wind, end_wind) in cache:
  64. if (rock, start_wind, end_wind) in cache:
  65. cache_value = cache[(rock, start_wind, end_wind)]
  66. if len(cache_value) > 1:
  67. # stable loop
  68. if cache_value[-1] - cache_value[-2] == rock_number - cache_value[-1]:
  69. break_outer_loop = True
  70. break
  71. cache[(rock, start_wind, end_wind)] = cache.get((rock, start_wind, end_wind), [])
  72. cache[(rock, start_wind, end_wind)].append(rock_number)
  73. rock_height_cache[rock_number] = highest
  74. break
  75. else:
  76. rock_y -= 1
  77. if break_outer_loop:
  78. break
  79.  
  80. if break_outer_loop:
  81. rocks_fallen = rock_number - earlier_rock_number
  82. height_of_loop = highest - rock_height_cache[earlier_rock_number]
  83. remaining = (rocks_to_drop - rock_number) % rocks_fallen
  84. highest = highest + ((rocks_to_drop - rock_number) // rocks_fallen) * height_of_loop + rock_height_cache[remaining + earlier_rock_number] - rock_height_cache[earlier_rock_number] - 1
  85.  
  86. print(highest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement