-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_3_part_2.py
82 lines (66 loc) · 2.35 KB
/
day_3_part_2.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''This code is for Day three part one of the Advent of Code problems'''
import re
from collections import namedtuple
from shapely.ops import cascaded_union
from shapely.geometry import Polygon, MultiPolygon
CLAIMTXT = []
with open("input_claims.txt", "r") as file:
for line in file:
CLAIMTXT.append(line)
Claim = namedtuple('Claim', 'a b c d')
CLAIMS = []
# Pull data apart from input file for future calcs.
for claimtx in CLAIMTXT:
claimID = (re.findall(r'(?<=#)\d*', claimtx))
A1 = (re.findall(r'(?<=@ )\d*', claimtx))
B1 = (re.findall(r'(?<=,)\d*', claimtx))
C1 = (re.findall(r'(?<=: )\d*', claimtx))
D1 = (re.findall(r'(?<=x)\d*', claimtx))
CLAIMS.append([A1, B1, C1, D1])
A1 = [item[0] for item in CLAIMS]
B1 = [item[1] for item in CLAIMS]
C1 = [item[2] for item in CLAIMS]
D1 = [item[3] for item in CLAIMS]
def polyshapes(corner_a, corner_b, corner_c, corner_d):
'''This function converts rectangle location data into a format we can
use later'''
left, top, right, bottom = sum(corner_a), sum(corner_b),\
sum(corner_a+corner_c), sum(corner_d+corner_b)
return left, top, right, bottom
CORNERS = []
CLAIMS = []
RECTANGLES = []
A2 = [list(map(int, x)) for x in A1]
B2 = [list(map(int, x)) for x in B1]
C2 = [list(map(int, x)) for x in C1]
D2 = [list(map(int, x)) for x in D1]
POLYS = []
for i in range(len(A1)):
#creates polygons after changing format of input
CORNERS = (polyshapes(A2[i], B2[i], C2[i], D2[i]))
POLYS.append(Polygon([(CORNERS[0], CORNERS[3]), (CORNERS[2], CORNERS[3]),\
(CORNERS[2], CORNERS[1]), (CORNERS[0], CORNERS[1])]))
POLYMULTI = []
for ITEM_POLY in POLYS:
compare = ITEM_POLY
for ITEM_POL in POLYS:
if compare == ITEM_POL:
break
elif ITEM_POL.intersects(compare) is True:
overlap = ITEM_POL.intersection(compare)
if overlap.area > 0:
POLYMULTI.append(overlap)
#print(overlap.area)
FINALPOL = MultiPolygon(POLYMULTI)
# Following code added to solve for day 2. Previous same as day 1.
for ITEM_POLY in POLYS:
if ANSWER.intersects(ITEM_POLY) is False:
nonoverlap = ITEM_POLY
coords = nonoverlap.exterior.coords
x = []
for coord in coords:
x.append(coord)
set = x
minvals = min(set)
x = str(int(minvals[0])) + ','+ str(int(minvals[1]))
"\n".join(s for s in CLAIMTXT if x in s)