Advertisement
Guest User

Day 20

a guest
Dec 20th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. from collections import Counter
  2. from copy import copy
  3.  
  4.  
  5. def generate_row(i, j, dirs, image, border_pixel):
  6. row = []
  7.  
  8. for di, dj in dirs:
  9. ni = di + i
  10. nj = dj + j
  11.  
  12. if (ni, nj) not in image:
  13. row.append(border_pixel)
  14. else:
  15. row.append(image[ni, nj])
  16.  
  17. return row
  18.  
  19.  
  20. def pixels_to_decimal(pixels):
  21. binary = ['0' if pixel == '.' else '1' for pixel in pixels]
  22. return int(''.join(binary), 2)
  23.  
  24.  
  25. def add_layer(image, border_pixel):
  26. new = copy(image)
  27. min_m = min_n = float('inf')
  28. max_m = max_n = float('-inf')
  29.  
  30. for i, j in image:
  31. min_m = min(min_m, i)
  32. min_n = min(min_n, j)
  33.  
  34. max_m = max(max_m, i)
  35. max_n = max(max_n, j)
  36.  
  37. for j in range(min_n, max_n+1):
  38. new[min_m-1, j] = border_pixel
  39. new[max_m+1, j] = border_pixel
  40.  
  41. for i in range(min_m, max_m+1):
  42. new[i, min_n-1] = border_pixel
  43. new[i, max_n+1] = border_pixel
  44.  
  45. # top left
  46. new[min_m-1, min_n-1] = border_pixel
  47.  
  48. # top right
  49. new[min_m-1, max_n+1] = border_pixel
  50.  
  51. # bottom left
  52. new[max_m+1, min_n-1] = border_pixel
  53.  
  54. # bottom right
  55. new[max_m+1, max_n+1] = border_pixel
  56.  
  57. return new
  58.  
  59.  
  60. def enhance(algorithm, image, border_pixel):
  61. enhanced = {}
  62. top_dirs = [(-1, -1), (-1, 0), (-1, 1)]
  63. mid_dirs = [(0, -1), (0, 0), (0, 1)]
  64. bot_dirs = [(1, -1), (1, 0), (1, 1)]
  65.  
  66. for i, j in image:
  67. pixels = []
  68.  
  69. for dirs in [top_dirs, mid_dirs, bot_dirs]:
  70. pixels.extend(generate_row(i, j, dirs, image, border_pixel))
  71.  
  72. enhanced[i, j] = algorithm[pixels_to_decimal(pixels)]
  73.  
  74. return enhanced
  75.  
  76.  
  77. def solve_part_1(algorithm, image, steps):
  78. for i in range(steps):
  79. border_pixel = '.' if i % 2 == 0 else algorithm[0]
  80. image = add_layer(image, border_pixel)
  81. image = enhance(algorithm, image, border_pixel)
  82.  
  83. return Counter(image.values())['#']
  84.  
  85.  
  86. def solve_part_2(algorithm, image, steps):
  87. return solve_part_1(algorithm, image, steps)
  88.  
  89.  
  90. if __name__ == '__main__':
  91. with open('input_01.txt') as f:
  92. algorithm, grid = f.read().split('\n\n')
  93. algorithm = ''.join([line for line in algorithm.split('\n')])
  94. grid = [line for line in grid.split('\n')]
  95.  
  96. image = {}
  97.  
  98. for i in range(len(grid)):
  99. for j in range(len(grid[0])):
  100. image[i, j] = grid[i][j]
  101.  
  102. print(solve_part_1(algorithm, image, 2))
  103. print(solve_part_2(algorithm, image, 50))
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement