20.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from util import get_input
  2. input = get_input("20.input")
  3. enhancement = [c == '#' for c in input[0]]
  4. img = set()
  5. size = (len(input[2:][0]) - 1, len(input[2:]) - 1)
  6. for y, line in enumerate(input[2:]):
  7. for x, c in enumerate(line):
  8. if c == '#':
  9. img.add((x, y))
  10. def get_surrounding(i, size, stp, xin, yin):
  11. bnum = ""
  12. for y in [yin-1, yin, yin+1]:
  13. for x in [xin-1, xin, xin+1]:
  14. if (x, y) in i:
  15. bnum += "1"
  16. else:
  17. bnum += "0"
  18. res = int(bnum, 2)
  19. return res
  20. def step(i, size, stp):
  21. newimg = set()
  22. for y in range(-stp - 1, size[1] + stp + 2):
  23. for x in range(-stp - 1, size[0] + stp + 2):
  24. idx = get_surrounding(i, size, stp, x, y)
  25. if enhancement[idx]:
  26. newimg.add((x, y))
  27. return newimg
  28. def print_img(i, size, stp):
  29. for y in range(-stp - 1, size[1] + stp + 2):
  30. for x in range(-stp - 1, size[0] + stp + 2):
  31. print('#' if (x, y) in i else '.', end='')
  32. print("")
  33. for i in range(50):
  34. #print_img(img, size, 50)
  35. #print("-" * 20)
  36. if i % 2 == 1:
  37. for y in range(-i - 2, size[1] + i + 1 + 2):
  38. img.add((-i - 2, y))
  39. img.add((-i - 1, y))
  40. img.add((size[0] + i + 1, y))
  41. img.add((size[0] + i + 2, y))
  42. for x in range(-i - 2, size[0] + i + 1 + 2):
  43. img.add((x, -i - 2))
  44. img.add((x, -i - 1))
  45. img.add((x, size[1] + i + 1))
  46. img.add((x, size[1] + i + 2))
  47. img = step(img, size, i)
  48. if i == 1:
  49. print("Part 1:", len(img))
  50. #print_img(img, size, 50)
  51. print("Part 2:", len(img))