You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

import functools
import sys
import typing
class Loc(typing.NamedTuple):
x: int
y: int
def main() -> int:
forest_frame = [list(line.strip()) for line in sys.stdin.readlines()]
frame_width = len(forest_frame[0])
frame_height = len(forest_frame)
all_trees_encountered = []
for slope in [
Loc(x=1, y=1),
Loc(x=3, y=1),
Loc(x=5, y=1),
Loc(x=7, y=1),
Loc(x=1, y=2),
]:
loc = Loc(x=0, y=0)
trees_encountered = 0
while loc.y <= (frame_height - 1):
at_loc = forest_frame[loc.y][loc.x]
if at_loc == "#":
trees_encountered += 1
next_x = (loc.x + slope.x) % frame_width
next_y = loc.y + slope.y
next_loc = Loc(x=next_x, y=next_y)
loc = next_loc
print(
f"(slope right={slope.x} down={slope.y}) trees encountered: {trees_encountered}"
)
all_trees_encountered.append(trees_encountered)
trees_encountered_product = functools.reduce(
lambda x, y: x * y, all_trees_encountered
)
print(f"trees encountered product: {trees_encountered_product}")
return 0
if __name__ == "__main__":
sys.exit(main())