-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (50 loc) · 2.07 KB
/
main.py
File metadata and controls
60 lines (50 loc) · 2.07 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import time
def main(input_file, stage):
with open(input_file, "r") as file:
forrest = [line.strip("\n") for line in file.readlines()]
directions = [(-1, 0), (1, 0), (0, 1), (0, -1)]
visible = 0
max_scenic_score = 0
for i in range(0, len(forrest)):
for j in range(0, len(forrest[i])):
if i == 0 or i == len(forrest)-1:
visible += 1
continue
if j == 0 or j == len(forrest[i])-1:
visible += 1
continue
scenic_score = 1
for direction in directions:
position = (i+direction[0], j+direction[1])
is_visible = False
viewing_distance = 1
while forrest[position[0]][position[1]] < forrest[i][j]:
if position[0] == 0 or position[0] == len(forrest)-1:
visible += 1
is_visible = True
break
if position[1] == 0 or position[1] == len(forrest[0])-1:
visible += 1
is_visible = True
break
viewing_distance += 1
position = (position[0] + direction[0], position[1] + direction[1])
if stage == 2:
scenic_score *= viewing_distance
continue
if is_visible:
break
max_scenic_score = max(scenic_score, max_scenic_score)
if stage == 2:
print(max_scenic_score)
else:
print(visible)
if __name__ == "__main__":
use_example = False
file_name = "example" if use_example else "input"
start = time.time()
main(file_name, 1)
print(f"Stage 1 time: {time.time() - start}")
start = time.time()
main(file_name, 2)
print(f"Stage 2 time: {time.time() - start}")