You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.8 KiB
Python

6 years ago
#!/usr/bin/env python3
import re
with open('input.txt', 'r') as f:
claims_raw = [x.strip() for x in f.readlines()]
# print(claims_raw)
class Claim:
def __init__(self, claim_id, x, y, width, height):
self.claim_id = claim_id
self.x1 = x
self.y1 = y
self.width = width
self.height = height
self._claimed_patches = None
@property
def x2(self):
return self.x1 + self.width - 1
@property
def y2(self):
return self.y1 + self.height - 1
def claimed_patches(self):
if self._claimed_patches is not None:
return self._claimed_patches
patches = set()
for i in range(self.x1, self.x2 + 1):
for j in range(self.y1, self.y2 + 1):
patches.add((i, j))
self._claimed_patches = patches
return patches
def intersections(self, claim):
return self.claimed_patches().intersection(claim.claimed_patches())
claim_regex = re.compile(r'#(\d*) @ (\d*),(\d*): (\d*)x(\d*)')
claims = list()
for claim in claims_raw:
parse = claim_regex.match(claim)
claims.append(Claim(
int(parse[1]),
int(parse[2]),
int(parse[3]),
int(parse[4]),
int(parse[5])
))
# print(claims)
# print(claims[0].width, claims[0].height, len(claims[0].claimed_patches()))
# print(claims[0].intersections(claims[1]))
c1 = Claim(1, 1, 3, 4, 4)
c2 = Claim(1, 3, 1, 4, 4)
print(c1.intersections(c2))
# sqares that are claimed by two or more claims
all_interference = list()
for i, claim1 in enumerate(claims):
for claim2 in claims[i + 1:]:
tmp = claim1.intersections(claim2)
if len(tmp) != 0:
print('intersection found:', claim1.claim_id, claim2.claim_id, len(tmp))
all_interference.extend(tmp)
print(len(set(all_interference)))