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.

2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
2 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import re
  2. import itertools
  3. text = open(0).read()
  4. graph = {}
  5. vals = {}
  6. for ln in text.splitlines():
  7. pos, val, *adjs = re.findall(r'[A-Z]{2}|\d+', ln)
  8. if int(val):
  9. vals[pos] = int(val)
  10. graph[pos] = {k: 1 for k in adjs}
  11. old = {k: v.copy() for k, v in graph.items()}
  12. for k in graph:
  13. graph[k][k] = 0
  14. edge = {k}
  15. seen = set()
  16. for step in range(1, 1000):
  17. edge = {n for k in edge for n in old[k]} - seen
  18. seen |= edge
  19. for n in edge:
  20. graph[k].setdefault(n, step)
  21. def tic(pending, start, left):
  22. out = []
  23. for end in pending:
  24. rem = max(0, left - graph[start][end] - 1)
  25. out.append([vals[end] * rem, end, rem])
  26. return sorted(out, reverse=True)
  27. def solve1(pending, total, pos, t_left):
  28. if not pending:
  29. yield total
  30. else:
  31. if ans1 > total + sum(vals[p] for p in pending) * t_left:
  32. return
  33. for contrib, pos, t_left in tic(pending, pos, t_left):
  34. yield from solve1(pending - {pos}, total + contrib, pos, t_left)
  35. ans1 = 0
  36. for ans in solve1(set(vals), 0, 'AA', 30):
  37. if ans > ans1:
  38. ans1 = ans
  39. print(ans1)
  40. def solve2(pending, total, pos_1, t_left_1, pos_2, t_left_2):
  41. if not pending:
  42. yield total
  43. else:
  44. if ans2 > total + sum(vals[p] for p in pending) * max(t_left_1, t_left_2):
  45. return
  46. if t_left_1 >= t_left_2:
  47. # move 1
  48. for contrib, pos, t_left in tic(pending, pos_1, t_left_1):
  49. yield from solve2(pending - {pos}, total + contrib, pos, t_left, pos_2, t_left_2)
  50. else:
  51. # move 2
  52. for contrib, pos, t_left in tic(pending, pos_2, t_left_2):
  53. yield from solve2(pending - {pos}, total + contrib, pos_1, t_left_1, pos, t_left)
  54. ans2 = 0
  55. for ans in solve2(set(vals), 0, 'AA', 26, 'AA', 26):
  56. if ans > ans2:
  57. ans2 = ans
  58. print(ans2)