diff --git a/day05/part1.py b/day05/part1.py index 54d07f3..3d17619 100644 --- a/day05/part1.py +++ b/day05/part1.py @@ -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())) diff --git a/day05/part2.py b/day05/part2.py index a0135e8..3d37633 100644 --- a/day05/part2.py +++ b/day05/part2.py @@ -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()))