advent-of-code-2024/day6.1.py

40 lines
817 B
Python
Raw Permalink Normal View History

2024-12-07 00:05:22 -08:00
""" day 6 part 1 """
import fileinput
room = [line for line in fileinput.input()]
guard_x = None
guard_y = None
for y,line in enumerate(room):
for x,cell in enumerate(line):
if cell in '^<>v':
guard_x = x
guard_y = y
guard_d = cell
dirs = {
'^': (0,-1,'>'),
'>': (1,0,'v'),
'v': (0,1,'<'),
'<': (-1,0,'^')
}
visited = {}
while 0 <= guard_y < len(room) and 0 <= guard_x < len(room[guard_y]):
dx, dy, turn = dirs[guard_d]
print(guard_x,guard_y,guard_d)
step_x = guard_x + dx
step_y = guard_y + dy
if 0 <= step_y < len(room) and 0 <= step_x < len(room[step_y]) and room[step_y][step_x] == '#':
guard_d = turn
else:
visited.add((guard_x,guard_y))
guard_x,guard_y = step_x,step_y
print(len(visited))