Days 7 and 8 completed
This commit is contained in:
3
day8/common.py
Normal file
3
day8/common.py
Normal file
@@ -0,0 +1,3 @@
|
||||
def parse(filename: str) -> list[list[int]]:
|
||||
with open(filename) as f:
|
||||
return [[int(i) for i in line.strip("\n")] for line in f.readlines()]
|
||||
27
day8/part1.py
Executable file
27
day8/part1.py
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python
|
||||
from common import parse
|
||||
|
||||
|
||||
def is_visible(forest: list[list[int]], x: int, y: int) -> bool:
|
||||
height = forest[y][x]
|
||||
west = forest[y][0:x]
|
||||
east = forest[y][x + 1 :]
|
||||
north = [forest[i][x] for i in range(y)]
|
||||
south = [forest[i][x] for i in range(y + 1, len(forest))]
|
||||
return any(map(lambda x: x == [] or max(x) < height, (west, east, north, south)))
|
||||
|
||||
|
||||
def solve(input: list[list[int]]) -> int:
|
||||
count = 0
|
||||
for y, line in enumerate(input):
|
||||
for x, tree in enumerate(line):
|
||||
count += 1 if is_visible(input, x, y) else 0
|
||||
return count
|
||||
|
||||
|
||||
def main():
|
||||
print(solve(parse("input")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
57
day8/part2.py
Executable file
57
day8/part2.py
Executable file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python
|
||||
from common import parse
|
||||
|
||||
|
||||
def scenic_score(forest: list[list[int]], x: int, y: int) -> int:
|
||||
height = forest[y][x]
|
||||
sw = se = sn = ss = 0
|
||||
west = reversed(forest[y][0:x])
|
||||
for w in west:
|
||||
if w < height:
|
||||
sw += 1
|
||||
continue
|
||||
elif w == height:
|
||||
sw += 1
|
||||
break
|
||||
east = forest[y][x + 1 :]
|
||||
for e in east:
|
||||
if e < height:
|
||||
se += 1
|
||||
continue
|
||||
elif e == height:
|
||||
se += 1
|
||||
break
|
||||
north = reversed([forest[i][x] for i in range(y)])
|
||||
for n in north:
|
||||
if n < height:
|
||||
sn += 1
|
||||
continue
|
||||
elif n == height:
|
||||
sn += 1
|
||||
break
|
||||
south = [forest[i][x] for i in range(y + 1, len(forest))]
|
||||
for s in south:
|
||||
if s < height:
|
||||
ss += 1
|
||||
continue
|
||||
elif s == height:
|
||||
ss += 1
|
||||
break
|
||||
|
||||
return sw * se * sn * ss
|
||||
|
||||
|
||||
def solve(input: list[list[int]]) -> int:
|
||||
maxi = 0
|
||||
for y, line in enumerate(input):
|
||||
for x, tree in enumerate(input):
|
||||
maxi = max(maxi, scenic_score(input, x, y))
|
||||
return maxi
|
||||
|
||||
|
||||
def main():
|
||||
print(solve(parse("input")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
5
day8/toy_input
Normal file
5
day8/toy_input
Normal file
@@ -0,0 +1,5 @@
|
||||
30373
|
||||
25512
|
||||
65332
|
||||
33549
|
||||
35390
|
||||
Reference in New Issue
Block a user