-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.py
65 lines (51 loc) · 1.44 KB
/
08.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
from typing import List
with open("input/08.txt") as f:
lines = f.read().split("\n")
rows = []
for line in lines:
row = [int(i) for i in line]
rows.append(row)
columns = list(map(list, zip(*rows)))
def check(row: List[int], index):
element = row[index]
before = row[:index]
if index != len(row):
after = row[index + 1:]
else:
after = []
return all([element > tree for tree in before]) or all([element > tree for tree in after])
pairs = []
for i, row in enumerate(rows):
for j, column in enumerate(columns):
if check(row, j) or check(column, i):
pairs.append((i, j))
# Part A
pairs = list(set(pairs))
print(len(pairs))
# Part B
def calculate(row: List[int], index):
element = row[index]
before = row[:index]
if index != len(row):
after = row[index + 1:]
else:
after = []
before_score = after_score = 1
for tree in before[::-1]:
if tree >= element:
break
before_score += 1
for tree in after:
if tree >= element:
break
after_score += 1
if all([element > tree for tree in before]):
before_score -= 1
if all([element > tree for tree in after]):
after_score -= 1
return before_score * after_score
scores = []
for i, row in enumerate(rows):
for j, column in enumerate(columns):
scores.append(calculate(row, j) * calculate(column, i))
print(max(scores))