Day 05 (glamorized)

This commit is contained in:
Sebastian Seedorf
2021-12-13 10:36:41 +01:00
parent c217d7b24a
commit 7a154ed958
2 changed files with 5 additions and 10 deletions

View File

@@ -1,19 +1,16 @@
#!/usr/bin/env python3
import re
from collections import defaultdict
lines = (x.strip() for x in open("input.txt"))
points = defaultdict(int)
points = {}
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
points[(x1, y)] = points.get((x1, y), 0) + 1
elif y1 == y2:
for x in range(min(x1, x2), max(x1, x2)+1):
key = '{},{}'.format(x, y1)
points[key] = points[key] + 1
points[(x, y1)] = points.get((x, y1), 0) + 1
print(sum(1 if val >= 2 else 0 for val in points.values()))

View File

@@ -1,10 +1,9 @@
#!/usr/bin/env python3
import re
from collections import defaultdict
from itertools import repeat
lines = (x.strip() for x in open("input.txt"))
points = defaultdict(int)
points = {}
for line in lines:
[x1, y1, x2, y2] = list(map(int, re.match(r"(\d+),(\d+) -> (\d+),(\d+)", line).groups()))
@@ -12,7 +11,6 @@ for line in lines:
range(x1, x2+(x1 <= x2)*2-1, (x1 <= x2)*2-1) if x1 != x2 else repeat(x1),
range(y1, y2+(y1 <= y2)*2-1, (y1 <= y2)*2-1) if y1 != y2 else repeat(y1)
):
key = '{},{}'.format(x, y)
points[key] = points[key] + 1
points[(x, y)] = points.get((x, y), 0) + 1
print(sum(val >= 2 for val in points.values()))