52 lines
1.2 KiB
Python
52 lines
1.2 KiB
Python
|
#!/usr/bin/env python
|
||
|
# 2024 - Advent Of Code 6
|
||
|
|
||
|
# file = 'input_example.txt'
|
||
|
file = 'input.txt'
|
||
|
|
||
|
# pylint: disable=consider-using-with
|
||
|
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
|
||
|
|
||
|
MAX_X = len(input_lines[0])
|
||
|
MAX_Y = len(input_lines)
|
||
|
|
||
|
vector=(0, 0)
|
||
|
gx = 0
|
||
|
gy = 0
|
||
|
for y in range(0, MAX_Y):
|
||
|
for x in range(0, MAX_X):
|
||
|
g = input_lines[y][x]
|
||
|
if g not in ('.', '#'):
|
||
|
gx = x
|
||
|
gy = y
|
||
|
print(f'found guard at {x}, {y}')
|
||
|
if g == '^':
|
||
|
vector = (0, -1)
|
||
|
if g == 'v':
|
||
|
vector = (0, 1)
|
||
|
if g == '>':
|
||
|
vector = (1, 0)
|
||
|
if g == '<':
|
||
|
vector = (-1, 0)
|
||
|
print(f'{vector=}')
|
||
|
|
||
|
moves = [(0,-1), (1, 0), (0, 1), (-1, 0)]
|
||
|
move = moves.index(vector)
|
||
|
|
||
|
visited = set()
|
||
|
visited.add((gx, gy))
|
||
|
while 0 < gx < MAX_X and 0 < gy < MAX_Y:
|
||
|
nextx = gx + vector[0]
|
||
|
nexty = gy + vector[1]
|
||
|
if nextx < 0 or nextx >= MAX_X or nexty < 0 or nexty >= MAX_Y:
|
||
|
break
|
||
|
if input_lines[nexty][nextx] != '#':
|
||
|
gx += vector[0]
|
||
|
gy += vector[1]
|
||
|
visited.add((gx, gy))
|
||
|
else:
|
||
|
move += 1
|
||
|
vector = moves[move % 4]
|
||
|
|
||
|
print(f'{len(visited)=}')
|