34 lines
753 B
Python
34 lines
753 B
Python
from collections import defaultdict
|
|
import re
|
|
|
|
|
|
def iter_claim(c):
|
|
ident, left, top, width, height = c
|
|
for v in range(height):
|
|
for h in range(width):
|
|
yield left + h, top + v
|
|
|
|
|
|
with open('data/03.txt') as f:
|
|
data = [r for r in f]
|
|
|
|
regex = re.compile(r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)')
|
|
pixels = defaultdict(set)
|
|
claims = []
|
|
for line in data:
|
|
claim = tuple(int(x) for x in regex.match(line).groups())
|
|
claims.append(claim)
|
|
|
|
for coords in iter_claim(claim):
|
|
pixels[coords].add(claim[0])
|
|
|
|
print(f'Part 1: {sum(1 for i in pixels.values() if len(i) > 1)}\n')
|
|
|
|
for claim in claims:
|
|
if all(len(pixels[coords]) == 1 for coords in iter_claim(claim)):
|
|
print(f'Part 2: {claim[0]}\n')
|
|
|
|
|
|
|
|
|