45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
import functools
|
|
|
|
slope_map = []
|
|
|
|
with open('day3/input') as reader:
|
|
slope_map.extend(reader.read().splitlines())
|
|
|
|
|
|
def check_slope(right, down):
|
|
global slope_map
|
|
pos = 0
|
|
y = 1
|
|
trees = 0
|
|
for l in slope_map[1:]:
|
|
if y % down == 0:
|
|
pos = (pos + right) % len(l)
|
|
if l[pos] == '#':
|
|
trees += 1
|
|
y += 1
|
|
|
|
return trees
|
|
|
|
|
|
def check_slope_short(right, down):
|
|
global slope_map
|
|
trees = 0
|
|
for (i, l) in enumerate(slope_map):
|
|
if i % down == 0:
|
|
pos = (i // down * right) % len(l)
|
|
if l[pos] == '#':
|
|
trees += 1
|
|
|
|
return trees
|
|
|
|
|
|
def check_slope_even_short(right, down):
|
|
global slope_map
|
|
return len(list(filter(lambda b: b, map(lambda en: en[1][(en[0] // down * right) % len(en[1])] == '#', filter(lambda en: en[0] % down == 0, enumerate(slope_map))))))
|
|
|
|
slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
|
|
|
|
total_trees = functools.reduce(lambda prev, slope: prev * check_slope_even_short(*slope), slopes, 1)
|
|
|
|
print(total_trees)
|