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.
68 lines
1.6 KiB
Python
68 lines
1.6 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])
|
||
|
))
|
||
|
|
||
|
for claim1 in claims:
|
||
|
intersects = False
|
||
|
for claim2 in claims:
|
||
|
if claim1 is claim2:
|
||
|
continue
|
||
|
tmp = claim1.intersections(claim2)
|
||
|
if len(tmp) != 0:
|
||
|
intersects = True
|
||
|
break
|
||
|
if not intersects:
|
||
|
break
|
||
|
|
||
|
|
||
|
print(claim1, claim1.claim_id)
|