Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 1 gada
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. text = open(0).read()
  2. graph = {
  3. complex(x, y): int({'.': 1, '#': 10000}.get(val, val))
  4. for y, row in enumerate(text.splitlines())
  5. for x, val in enumerate(row)
  6. }
  7. xmax = max(int(p.real) for p in graph)
  8. ymax = max(int(p.imag) for p in graph)
  9. end = complex(xmax, ymax)
  10. mapping = {1: '>', -1: '<', 1j: 'v', -1j: '^'}
  11. # a position mapped to the lowest effort taken to reach it
  12. edge = {(0, ''): (0, '')}
  13. exhausted = {}
  14. while end not in exhausted:
  15. pos = min(edge, key=edge.get)
  16. parent = edge.pop(pos)
  17. if pos[0] == end:
  18. break
  19. exhausted[pos] = parent
  20. for step in [1, -1, 1j, -1j]:
  21. # no reverse
  22. if pos[1] and pos[1][-1] == mapping[-step]: continue
  23. # turn condition
  24. bop = pos[1] + mapping[step]
  25. if len(bop) == 4 and len(set(bop)) == 1: continue
  26. adj = pos[0] + step, bop[-3:]
  27. # out of bounds check
  28. if adj[0] not in graph: continue
  29. # some positions are truly dead
  30. if adj in exhausted: continue
  31. score = parent[0] + graph[adj[0]], parent[1] + mapping[step]
  32. if adj not in edge:
  33. edge[adj] = score
  34. else:
  35. edge[adj] = min(edge[adj], score)
  36. def scorer(seq):
  37. pos = 0
  38. out = 0
  39. for char in seq:
  40. pos += {'>': 1, '<': -1, '^': -1j, 'v': 1j}[char]
  41. out += graph[pos]
  42. return out
  43. print(parent[1], scorer(parent[1]))
  44. test = '>>>v>>>^>>>vv>vv>vvv>vvv<vv>'
  45. print(test, scorer(test))