-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-1.py
80 lines (50 loc) · 1.8 KB
/
3-1.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
# **how many units of fabric are within 2 or more of the claims?** (puzzle answer)
# a,b is the offset from the fabric origin.
# x,y is the area of the rectangle to be drawn.
# fabric is >= 1000,1000.
#claim is in the format '#id_int @ a,b: (w)x(h)', e.g. '#123 @ 3,2: 5x4'
import re
def getFabricSize (a, b, w, h):
if (a + w) > fabricSize[0]:
fabricSize[0] = (a + w)
if (b + h) > fabricSize[1]:
fabricSize[1] = (b + h)
return fabricSize
def markFabric (a, b, w, h, fabric):
for x in range (a, (a + w)):
for y in range (b, (b + h)):
fabric[x][y] += 1
def findSquares (fabricSize, fabric):
squares = 0
for i in range(0,fabricSize[0]):
for j in range(0,fabricSize[1]):
if fabric[i][j] >= 2:
squares += 1
return squares
fabricSize = [0,0]
#read the data in
claims = []
file = open('3-1-input.txt','r')
for line in file.readlines():
getClaim = re.search('(#\d+)\s@\s(\d+),(\d+):\s(\d+)x(\d+)' , line.rstrip())
if getClaim == None:
print(line.rstrip() + ' is not a valid claim.')
else:
claimID = getClaim[1]
a = int(getClaim[2])
b = int(getClaim[3])
w = int(getClaim[4])
h = int(getClaim[5])
claim = [claimID, a, b, w, h]
claims.append(claim)
#get the fabric max size
fabricSize = getFabricSize(a, b, w, h)
fabric = [[0 for i in range (fabricSize[0])] for j in range (fabricSize[1])]
#mark the fabric
for claim in claims:
markFabric(claim[1], claim[2], claim[3], claim[4], fabric)
#find squares with more than one claim
squares = findSquares(fabricSize, fabric)
print('\nThere are ' + str(len(claims)) + ' claims.')
print('The claims use fabric that is max. ' + str(fabricSize[0]) + ',' + str(fabricSize[1]) + ' square inches in size.')
print('There are ' + str(squares) + ' square inches of overlapping claims.\n')