You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7KB

  1. def render(graph):
  2. xmin, *_, xmax = sorted(int(p.real) for p in walls)
  3. ymin, *_, ymax = sorted(int(p.imag) for p in walls)
  4. for y in range(ymin, ymax + 1):
  5. for x in range(xmin, xmax + 1):
  6. print('#' if complex(x, y) in graph else '.', end='')
  7. print()
  8. text = open(0).read()
  9. walls = {0}
  10. pos = 0
  11. for line in text.splitlines():
  12. d, n, rest = line.split()
  13. n, d = int(rest[1:-1][1:][:5], 16), 'RDLU'[int(rest[-2])]
  14. pos += {'R': 1, 'D': 1j, 'L': -1, 'U': -1j}[d] * int(n)
  15. walls.add(pos)
  16. smallx = {v: i * 2 for i, v in enumerate(sorted({p.real for p in walls}))}
  17. smally = {v: i * 2 for i, v in enumerate(sorted({p.imag for p in walls}))}
  18. walls = {complex(smallx[p.real], smally[p.imag]) for p in walls}
  19. walls = {0}
  20. pos = 0
  21. smallpos = 0
  22. for line in text.splitlines():
  23. d, n, rest = line.split()
  24. n, d = int(rest[1:-1][1:][:5], 16), 'RDLU'[int(rest[-2])]
  25. pos += {'R': 1, 'D': 1j, 'L': -1, 'U': -1j}[d] * int(n)
  26. goal = complex(smallx[pos.real], smally[pos.imag])
  27. dirx = (goal - smallpos).real or (goal - smallpos).imag
  28. while smallpos != goal:
  29. smallpos += dirx
  30. walls.add(smallpos)
  31. render(walls)
  32. exit()
  33. xmin, *_, xmax = sorted(int(p.real) for p in walls)
  34. ymin, *_, ymax = sorted(int(p.imag) for p in walls)
  35. void = {complex(x, y) for x in range(xmin, xmax + 1) for y in range(ymin, ymax + 1)} - walls
  36. groups = []
  37. while void:
  38. seen = set()
  39. edge = {void.pop()}
  40. while edge:
  41. seen |= edge
  42. edge = {pp + ss for pp in edge for ss in [1, -1, 1j, -1j]} & void - seen
  43. groups.append(seen)
  44. void -= seen
  45. inside = {p for g in groups if not any(p.real in {xmin, xmax} or p.imag in {ymin, ymax} for p in g) for p in g}
  46. print(len(inside | walls))