-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
62 lines (50 loc) · 1.9 KB
/
day8.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
#!/usr/bin/env python
import numpy as np
f = open("ressources/day8.txt", "r")
lines = f.readlines()
treeArray = np.zeros((99, 99))
i = 0
for line in lines:
treeSTR = line.replace("\n", "")
treeList = [int(tree) for tree in str(treeSTR)]
treeArray[i, :] = treeList
i += 1
isVisibleArray = np.zeros(treeArray.shape)
isVisibleArray[0,:] = 1
isVisibleArray[-1,:] = 1
isVisibleArray[:,0] = 1
isVisibleArray[:,-1] = 1
for i in range(1,isVisibleArray.shape[0]-1):
for j in range(1,isVisibleArray.shape[1]-1):
treeValue = treeArray[i,j]
maxUp = np.amax(treeArray[0:i,j])
maxDown = np.amax(treeArray[i+1:,j])
maxRight = np.amax(treeArray[i,j+1:])
maxLeft = np.amax(treeArray[i,0:j])
if treeValue > np.amin([maxUp,maxDown,maxRight,maxLeft]):
isVisibleArray[i,j] = 1
print("Number of visible trees is: {}".format(np.sum(isVisibleArray)))
VisiblityScoreMax = 0
for i in range(1,isVisibleArray.shape[0]-1):
for j in range(1,isVisibleArray.shape[1]-1):
treeValue = treeArray[i,j]
Up = treeArray[0:i,j][::-1]
tallerTrees = (Up>=treeValue)*1
tallerTrees[-1] = 1
firstUp = np.argmax(tallerTrees)+1
Down = treeArray[i+1:,j]
tallerTrees = (Down>=treeValue)*1
tallerTrees[-1] = 1
firstDown = np.argmax(tallerTrees)+1
Right = treeArray[i,j+1:]
tallerTrees = (Right>=treeValue)*1
tallerTrees[-1] = 1
firstRight = np.argmax(tallerTrees)+1
Left = treeArray[i,0:j][::-1]
tallerTrees = (Left>=treeValue)*1
tallerTrees[-1] = 1
firstLeft = np.argmax(tallerTrees)+1
VisiblityScore = firstUp*firstDown*firstLeft*firstRight
if VisiblityScoreMax < VisiblityScore:
VisiblityScoreMax = VisiblityScore
print("Biggest visibility score is: {}".format(VisiblityScoreMax))