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.

20 lines
678B

  1. from collections import Counter
  2. from itertools import cycle
  3. from math import copysign
  4. sign = lambda n: int(copysign(1, n))
  5. points = {'flat': Counter(), 'diag': Counter()}
  6. for line in text.splitlines():
  7. x1, y1, x2, y2 = [int(n) for n in re.findall(r'\d+', line)]
  8. xs = [*range(x1, x2, sign(x2 - x1)), x2]
  9. ys = [*range(y1, y2, sign(y2 - y1)), y2]
  10. if len(xs) == 1:
  11. points['flat'].update(zip(cycle(xs), ys))
  12. elif len(ys) == 1:
  13. points['flat'].update(zip(xs, cycle(ys)))
  14. else:
  15. points['diag'].update(zip(xs, ys))
  16. ans1 = sum(v > 1 for v in (points['flat']).values())
  17. ans2 = sum(v > 1 for v in (points['flat'] + points['diag']).values())