18_digplan.py 599 B

12345678910111213141516171819
  1. #!/usr/bin/env python3
  2. import sys
  3. from itertools import accumulate, pairwise
  4. def parse(line):
  5. direction, distance, color = line.split()
  6. d = int(distance)
  7. yield ((d, 0), (0, d), (-d, 0), (0, -d))['RDLU'.index(direction)]
  8. d = int(color[2:7], 16)
  9. yield ((d, 0), (0, d), (-d, 0), (0, -d))[int(color[7])]
  10. def dig(steps):
  11. xy = list(zip(*map(accumulate, zip(*steps))))
  12. return sum(xa * yb - ya * xb + abs(xb - xa + yb - ya)
  13. for (xa, ya), (xb, yb) in pairwise(xy + xy[:1])) // 2 + 1
  14. wrong, color = zip(*map(parse, sys.stdin))
  15. print(dig(wrong))
  16. print(dig(color))