-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.py
53 lines (41 loc) · 1.16 KB
/
3.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
# https://adventofcode.com/2020/day/3
from math import prod
def read_input(path: str):
with open(path) as f:
lines = [line.strip() for line in f.readlines()]
return lines
def traverse(down, side, map):
width = len(map[0]) - 1
length = len(map) - 1
trees = 0
row, col = down, side
while row <= length:
if map[row][col] == '#':
trees += 1
row, col = row+down, col+side
# if needed, expand the map to the side
if col > width:
col = (col - width) - 1
return trees
if __name__ == "__main__":
path = 'res/3.txt'
test_m = ['..##.......',
'#...#...#..',
'.#....#..#.',
'..#.#...#.#',
'.#...##..#.',
'..#.##.....',
'.#.#.#....#',
'.#........#',
'#.##...#...',
'#...##....#',
'.#..#...#.#'
]
# part one
map = read_input(path)
trees = traverse(1, 3, map)
print(trees)
# part two
slopes = [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)]
trees = [traverse(d, s, map) for d, s in slopes]
print(prod(trees))