Files
python-aoc-2021/day05/part2.py
Sebastian Seedorf aea44ca7aa Day 05
2021-12-06 11:11:21 +01:00

27 lines
874 B
Python

#!/usr/bin/env python3
import re
from collections import defaultdict
lines = (x.strip() for x in open("input.txt"))
points = defaultdict(int)
for line in lines:
[x1, y1, x2, y2] = list(map(int, re.match(r"(\d+),(\d+) -> (\d+),(\d+)", line).groups()))
if x1 == x2:
for y in range(min(y1, y2), max(y1, y2) + 1):
key = '{},{}'.format(x1, y)
points[key] = points[key] + 1
elif y1 == y2:
for x in range(min(x1, x2), max(x1, x2) + 1):
key = '{},{}'.format(x, y1)
points[key] = points[key] + 1
else:
for i in range(0, abs(x1 - x2) + 1):
key = '{},{}'.format(
(x1 + i) if x1 <= x2 else (x1 - i),
(y1 + i) if y1 <= y2 else (y1 - i)
)
points[key] = points[key] + 1
print(sum(1 if val >= 2 else 0 for val in points.values()))