Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

51 lines
1007B

  1. import itertools
  2. def is_win(p_hp, p_atk, p_def, b_hp, b_atk, b_def):
  3. while True:
  4. b_hp -= max(1, p_atk - b_def)
  5. if b_hp <= 0:
  6. return True
  7. p_hp -= max(1, b_atk - p_def)
  8. if p_hp <= 0:
  9. return False
  10. weapons = [
  11. (8, 4, 0),
  12. (10, 5, 0),
  13. (25, 6, 0),
  14. (40, 7, 0),
  15. (74, 8, 0),
  16. ]
  17. armors = [
  18. (0, 0, 0),
  19. (13, 0, 1),
  20. (31, 0, 2),
  21. (53, 0, 3),
  22. (75, 0, 4),
  23. (102, 0, 5),
  24. ]
  25. rings = [
  26. (0, 0, 0),
  27. (0, 0, 0),
  28. (25, 1, 0),
  29. (50, 2, 0),
  30. (100, 3, 0),
  31. (20, 0, 1),
  32. (40, 0, 2),
  33. (80, 0, 3),
  34. ]
  35. opts = [
  36. [sum(vs) for vs in zip(*[w, a, r1, r2])]
  37. for w, a in itertools.product(weapons, armors)
  38. for r1, r2 in itertools.combinations(rings, 2)
  39. ]
  40. p_hp = 100
  41. b_hp, b_atk, b_def = map(int, re.findall(r'(\d+)', text))
  42. wins = {True: set(), False: set()}
  43. for cost, p_atk, p_def in opts:
  44. wins[is_win(p_hp, p_atk, p_def, b_hp, b_atk, b_def)].add(cost)
  45. ans1 = min(wins[True])
  46. ans2 = max(wins[False])