Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

49 lines
1.3KB

  1. def around(x, graph, adj):
  2. if x in graph:
  3. for dx in adj[graph[x]]:
  4. y = x + dx
  5. if x in {y + dy for dy in adj[graph[y]]}:
  6. yield y
  7. yield x + (y - x) / 2 # this extends the pipeline to include mid-joints
  8. text = open(0).read()
  9. graph = {complex(x, y): val
  10. for y, row in enumerate(text.splitlines())
  11. for x, val in enumerate(row)
  12. }
  13. adj = {
  14. 'S': [1, -1, 1j, -1j],
  15. '-': [1, -1],
  16. '.': [],
  17. '|': [1j, -1j],
  18. 'L': [1, -1j],
  19. '7': [1j, -1],
  20. 'J': [-1j, -1],
  21. 'F': [1j, 1],
  22. }
  23. state = {k for k in graph if graph[k] == 'S'}
  24. seen = {}
  25. n = 0
  26. while state:
  27. seen |= {k: n for k in state}
  28. state = {y for x in state for y in around(x, graph, adj) if y not in seen}
  29. n += 1
  30. print(max(seen.values()))
  31. def connected(nodes, steps):
  32. while nodes:
  33. edge = {nodes.pop()}
  34. seen = edge.copy()
  35. while edge:
  36. edge = {node + step for node in edge for step in steps} & nodes - seen
  37. seen |= edge
  38. nodes -= seen
  39. yield seen
  40. steps = {(dx + dy) / 2 for node in graph for dx in [-1, 0, 1] for dy in [-1j, 0, 1j]}
  41. nodes = {node + step for node in graph for step in steps} - set(seen)
  42. print(sum(len(group & set(graph)) for group in connected(nodes, steps) if not 0 in group))