|
@@ -2,18 +2,29 @@ import re
|
2
|
2
|
from collections import defaultdict
|
3
|
3
|
|
4
|
4
|
with open('data/03.txt', 'r') as file:
|
5
|
|
- cells = defaultdict(list)
|
|
5
|
+ cells = defaultdict(lambda: 0)
|
6
|
6
|
claims = map(lambda l: map(int, re.findall(r'\d+', l)), file.readlines())
|
7
|
7
|
ids = set()
|
|
8
|
+ overlaps = 0
|
8
|
9
|
|
9
|
10
|
for (cid, x, y, width, height) in claims:
|
10
|
|
- ids.add(cid)
|
|
11
|
+ collision = False
|
11
|
12
|
for i in range(width):
|
12
|
13
|
for j in range(height):
|
13
|
|
- cells[(x + i, y + j)].append(cid)
|
|
14
|
+ previous = cells[(x + i, y + j)]
|
|
15
|
+ if previous == 0:
|
|
16
|
+ cells[(x + i, y + j)] = cid
|
|
17
|
+ elif previous == -1:
|
|
18
|
+ collision = True
|
|
19
|
+ else:
|
|
20
|
+ collision = True
|
|
21
|
+ overlaps += 1
|
|
22
|
+ if previous in ids:
|
|
23
|
+ ids.remove(previous)
|
|
24
|
+ cells[(x + i, y + j)] = -1
|
14
|
25
|
|
15
|
|
- overlaps = [c for c in cells.values() if len(c) > 1]
|
16
|
|
- print(len(overlaps))
|
|
26
|
+ if not collision:
|
|
27
|
+ ids.add(cid)
|
17
|
28
|
|
18
|
|
- dupes = set(cid for sublist in overlaps for cid in sublist)
|
19
|
|
- print(list(ids - dupes)[0])
|
|
29
|
+ print(overlaps)
|
|
30
|
+ print(list(ids)[0])
|