-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaims.py
58 lines (49 loc) · 1.54 KB
/
claims.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from collections import defaultdict
from functools import partial
def fill(matrix, left, top, width, height):
for x in range(left, left+width):
for y in range(top, top+height):
matrix[x][y] += 1
def check(matrix, left, top, width, height):
for x in range(left, left+width):
for y in range(top, top+height):
if matrix[x][y] > 1:
return False
return True
def part1():
m = defaultdict(partial(defaultdict, int))
claims = []
with open("claims.txt") as f:
claims = f.readlines()
for claim in claims:
spatial = claim.split('@')[1].strip()
coords, dims = spatial.split(':')
top, left = coords.strip().split(',')
width, height = dims.strip().split('x')
fill(m, int(top), int(left), int(width), int(height))
ret = 0
for row in range(999):
for col in range(999):
if m[row][col] > 1:
ret += 1
print ret
def part2():
m = defaultdict(partial(defaultdict, int))
claims = []
with open("claims.txt") as f:
claims = f.readlines()
for claim in claims:
spatial = claim.split('@')[1].strip()
coords, dims = spatial.split(':')
top, left = coords.strip().split(',')
width, height = dims.strip().split('x')
fill(m, int(top), int(left), int(width), int(height))
for claim in claims:
claimid, spatial = claim.split('@')
coords, dims = spatial.strip().split(':')
top, left = coords.strip().split(',')
width, height = dims.strip().split('x')
if check(m, int(top), int(left), int(width), int(height)):
print claimid
#part1()
part2()