Advertisement
HexTree

Advent of Code 2022 Day 17

Dec 17th, 2022
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | Source Code | 0 0
  1.  
  2. height = 80
  3. width = 7
  4. grid = [['.' for j in range(width)] for i in range(height)]
  5. highest_rock = -1
  6.  
  7.  
  8. shape1 = ((0, 0), (0, 1), (0, 2), (0, 3))
  9. shape2 = ((0, 1), (1, 0), (1, 1), (1, 2), (2, 1))
  10. shape3 = ((0, 0), (0, 1), (0, 2), (1, 2), (2, 2))
  11. shape4 = ((0, 0), (1, 0), (2, 0), (3, 0))
  12. shape5 = ((0, 0), (0, 1), (1, 0), (1, 1))
  13.  
  14.  
  15. shapes = (shape1, shape2, shape3, shape4, shape5)
  16.  
  17.  
  18. with open('input', 'r') as f:
  19.     moves = f.readline().strip()
  20.  
  21. shape_it = 0
  22. move_it = 0
  23.  
  24. resting_rocks = 0
  25. while resting_rocks < 2022:
  26.     # if resting_rocks > 0 and shape_it == 0 and move_it == 18:
  27.         # for row in grid[::-1]:
  28.         #     print(''.join(row))
  29.         # print("resting rocks:", resting_rocks, "highest rock:", highest_rock)
  30.         # break
  31.     # spawn rock
  32.     for highest_point in range(len(grid)-1, -1, -1):
  33.         if '#' in grid[highest_point]:
  34.             break
  35.     else:
  36.         highest_point = -1
  37.     spawn_point = (highest_point + 4, 2)
  38.     shape = tuple((spawn_point[0] + i, spawn_point[1] + j) for (i, j) in shapes[shape_it])
  39.     shape_it = (shape_it+1) % len(shapes)
  40.     done = False
  41.     while not done:
  42.         # gas movement
  43.         move = moves[move_it]
  44.         move_it = (move_it+1) % len(moves)
  45.         dj = 1 if move == '>' else -1
  46.         new_shape = tuple((i, j+dj) for (i, j) in shape)
  47.         for point in new_shape:
  48.             if point[1] < 0 or point[1] >= 7 or grid[point[0]][point[1]] == '#':
  49.                 break
  50.         else:
  51.             shape = new_shape
  52.  
  53.         # downwards movement
  54.         new_shape = tuple((i-1, j) for (i, j) in shape)
  55.         for point in new_shape:
  56.             if point[0] < 0 or grid[point[0]][point[1]] == '#':
  57.                 done = True
  58.                 resting_rocks += 1
  59.                 highest_point_on_shape = max(i for (i, _) in shape)
  60.                 height_increase = highest_point_on_shape - highest_point
  61.                 if height_increase > 0:
  62.                     highest_rock += height_increase
  63.                 for p in shape:
  64.                     grid[p[0]][p[1]] = '#'
  65.                 midpoint = height//2
  66.                 if highest_point_on_shape > midpoint:
  67.                     for _ in range(highest_point_on_shape-midpoint):
  68.                         del grid[0]
  69.                         grid.append(['.' for j in range(width)])
  70.                 break
  71.         else:
  72.             shape = new_shape
  73.  
  74. # for row in grid[::-1]:
  75. #     print(''.join(row))
  76. print("part 1:", highest_rock+1)
  77.  
  78. # manual stuff for part 2:
  79.  
  80. rocks = 1750
  81. rock_height = 2776
  82. num_rocks_increase = 1745
  83. rock_height_increase = 2752
  84.  
  85. target = 1000000000000
  86. intervals = target // 1745 -1
  87.  
  88. final_rocks = rocks + intervals*num_rocks_increase
  89. final_height = rock_height + intervals*rock_height_increase
  90.  
  91. print(final_rocks, final_height)
  92. gap = target - final_rocks
  93. print(gap)
  94.  
  95. print(final_height + (4362 - 2776) + 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement