13_claw.py 511 B

123456789101112131415
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. def mincost(ax, ay, bx, by, px, py):
  5. b, brem = divmod(ay * px - ax * py, ay * bx - ax * by)
  6. a, arem = divmod(px - b * bx, ax)
  7. return 0 if arem or brem else a * 3 + b
  8. machines = [tuple(map(int, re.findall(r'\d+', machine)))
  9. for machine in sys.stdin.read().split('\n\n')]
  10. print(sum(mincost(*m) for m in machines))
  11. base = 10000000000000
  12. print(sum(mincost(ax, ay, bx, by, base + px, base + py)
  13. for ax, ay, bx, by, px, py in machines))