Add solution for day 08
This commit is contained in:
1 parent
f622a647c7
commit
077485197b
4 files changed
+240
No files matched your search
Executable
+60
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def visible(t,x,y):
|
||||
if x == 0 or y == 0 or x == (len(t[0]) - 1) or y == (len(t) - 1):
|
||||
return 1
|
||||
h = t[y][x]
|
||||
v = 1
|
||||
for r in [(0,x),(x+1,len(t[y]))]: # left, right
|
||||
for i in range(*r):
|
||||
if h <= t[y][i]: v = 0
|
||||
if v == 1: return v
|
||||
else: v = 1
|
||||
for r in [(0,y),(y+1,len(t))]: # up, down
|
||||
for i in range(*r):
|
||||
if h <= t[i][x]: v = 0
|
||||
if v == 1: return v
|
||||
else: v = 1
|
||||
return 0
|
||||
|
||||
def scenic(t,x,y):
|
||||
if x == 0 or y == 0 or x == (len(t[0]) - 1) or y == (len(t) - 1):
|
||||
return 0
|
||||
h = t[y][x]
|
||||
score = 1
|
||||
cur = 0
|
||||
for r in [(x-1,-1,-1),(x+1,len(t[y]))]: # left, right
|
||||
for i in range(*r):
|
||||
cur = cur + 1
|
||||
if h <= t[y][i]: break
|
||||
score = score * cur
|
||||
cur = 0
|
||||
for r in [(y-1,-1,-1),(y+1,len(t))]: # up, down
|
||||
for i in range(*r):
|
||||
cur = cur + 1
|
||||
if h <= t[i][x]: break
|
||||
score = score * cur
|
||||
cur = 0
|
||||
return score
|
||||
|
||||
if __name__ == '__main__':
|
||||
trees = [[int(l) for l in list(line.strip('\n'))] for line in open(sys.argv[1])]
|
||||
vis = 0
|
||||
scene = 0
|
||||
for i in range(len(trees)):
|
||||
for j in range(len(trees[i])):
|
||||
vis = vis + visible(trees,j,i)
|
||||
sc_score = scenic(trees,j,i)
|
||||
if sc_score > scene: scene = sc_score
|
||||
|
||||
# challenge 1
|
||||
res1 = str(vis)
|
||||
print("challenge 1:" + "\n" + res1 + "\n")
|
||||
|
||||
# challenge 2
|
||||
res2 = str(scene)
|
||||
print("challenge 2:" + "\n" + res2 + "\n")
|
||||
|
||||
Reference in new issue
Block a user