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.

49 line
1.2KB

  1. def build_grid(text):
  2. grid = {}
  3. for ln in text.splitlines():
  4. ns = map(int, ''.join(n if n.isdigit() else ' ' for n in ln).split())
  5. nodes = [complex(x, y) for x, y in zip(*[ns] * 2)]
  6. for aa, bb in zip(nodes, nodes[1:]):
  7. step = (bb - aa) / abs(bb - aa)
  8. while aa != bb:
  9. grid[aa] = '#'
  10. aa += step
  11. grid[aa] = '#'
  12. return grid
  13. def drip(grid, start, floor, p1=False, dof=[1j, 1j - 1, 1j + 1]):
  14. pos = start
  15. while True:
  16. for new in (pos + step for step in dof):
  17. if new not in grid and new.imag != floor:
  18. pos = new
  19. break
  20. else:
  21. grid[pos] = 'o'
  22. yield True
  23. pos = start
  24. if p1:
  25. if pos.imag == floor - 1:
  26. break
  27. else:
  28. if grid.get(start) == 'o':
  29. break
  30. def main():
  31. text = open(0).read()
  32. grid = build_grid(text)
  33. start = complex(500, 0)
  34. floor = max(p.imag for p in grid) + 2
  35. ans1 = sum(drip(grid.copy(), start, floor, True))
  36. print(ans1)
  37. ans2 = sum(drip(grid.copy(), start, floor))
  38. print(ans2)
  39. main()