-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay6.py
More file actions
39 lines (32 loc) · 1.2 KB
/
Day6.py
File metadata and controls
39 lines (32 loc) · 1.2 KB
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
import numpy as np
Coord = np.genfromtxt('Puzzle6', delimiter =', ', dtype=np.int)
maxX, maxY = 500, 500
host = np.zeros(shape=(len(Coord), maxY, maxX), dtype =np.int)
for i in range(len(Coord)):
for j in range(maxY):
host[i,j, Coord[i, 0]:] += range(maxX-Coord[i, 0])
host[i,j, :Coord[i, 0]+1] += range(Coord[i, 0]+1)[::-1]
for k in range(maxX):
host[i, Coord[i, 1]:, k] += range(maxX-Coord[i, 1])
host[i, :Coord[i, 1]+1, k] += range(0, Coord[i, 1]+1)[::-1]
map = np.ma.zeros(shape=(maxY, maxX), dtype =np.int)
for j in range (len(map)):
for i in range(len(map[0])):
Min = np.min(host[:,j,i])
IdX = np.where(host[:,j,i] == Min)[0]
if len(IdX) <2 :
map[j,i] = IdX
else:
map[j,i] = np.ma.masked
#Shrink
edges = np.unique(map[0]).data
edges = np.hstack((edges, np.unique(map[-1])))
edges = np.hstack((edges, np.unique(map[:,0])))
edges = np.hstack((edges, np.unique(map[:,-1])))
for v in edges:
map = np.ma.masked_values(map, v)
a,b = np.unique(map, return_counts=True)
print 'the largest area is ', b[:-1].max()
Zone = host.sum(axis=0, keepdims=True)[0]
Zone = np.ma.masked_where(Zone >=10000, Zone)
print 'the safe area is ', maxX*maxY - np.sum(Zone.mask), 'large'