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]))