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
|
from functools import reduce
from itertools import islice
def nth(iterable, n):
return next(islice(iter(iterable), n, n + 1))
def mult(iterable):
return reduce(lambda x, y: x * y, iterable)
def count_trees(data, right, down):
return [nth(row, right * idx % len(row)) for idx, row in enumerate(data[::down])].count("#")
data = list(map(str.strip, open("input")))
# Part 1
down = 1
right = 3
print(count_trees(data, right, down))
# Part 2
slopes = [
# R, D
[1, 1],
[3, 1],
[5, 1],
[7, 1],
[1, 2],
]
print(mult([count_trees(data, right, down) for right, down in slopes]))
|