Why on earch
This commit is contained in:
parent
622c3557b4
commit
8479da896e
@ -1,3 +1,4 @@
|
||||
import functools
|
||||
import sys
|
||||
import typing
|
||||
|
||||
@ -11,21 +12,39 @@ 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)
|
||||
loc = Loc(x=0, y=0)
|
||||
|
||||
trees_encountered = 0
|
||||
all_trees_encountered = []
|
||||
|
||||
while loc.y <= (frame_height - 1):
|
||||
at_loc = forest_frame[loc.y][loc.x]
|
||||
if at_loc == "#":
|
||||
trees_encountered += 1
|
||||
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)
|
||||
|
||||
next_x = (loc.x + 3) % frame_width
|
||||
next_y = loc.y + 1
|
||||
next_loc = Loc(x=next_x, y=next_y)
|
||||
loc = next_loc
|
||||
trees_encountered = 0
|
||||
|
||||
print(f"trees encountered: {trees_encountered}")
|
||||
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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user