advent_of_code/2023/10/10.py

139 lines
4 KiB
Python
Raw Normal View History

2024-03-06 00:30:58 +01:00
#!/usr/bin/env python
# 2023 - Advent Of Code 10
# 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")]
start_pos = (0, 0)
for y, _ in enumerate(input_lines):
for x, tile in enumerate(_):
#if input_lines[y][x] == 'S':
if tile == 'S':
start_pos = (x, y)
print(f'\033[1m{tile}\033[0m', end='')
else:
print(tile, end='')
print()
print(f'{start_pos=}')
x = start_pos[0]
y = start_pos[1]
pos_list = []
pos_list.append(start_pos)
next_pos = (-1, -1)
# find start direction
direction = -1
# direction 0: up, 1: right, 2: down, 3: left
if y > 0: # up
# print(f'checking {input_lines[y-1][x]=}')
if input_lines[y-1][x] == 'F':
next_pos = (x, y-1)
direction = 1
elif input_lines[y-1][x] == '7':
next_pos = (x, y-1)
direction = 2
elif input_lines[y-1][x] == '|':
next_pos = (x, y-1)
direction = 0
if direction == -1 and x < len(input_lines[y])-1: # right
# print(f'checking {input_lines[y][x+1]=}')
if input_lines[y][x+1] == 'J':
next_pos = (x+1, y)
direction = 0
elif input_lines[y][x+1] == '7':
next_pos = (x+1, y)
direction = 2
elif input_lines[y][x+1] == '-':
next_pos = (x+1, y)
direction = 1
# down
if direction == -1 and y < len(input_lines):
# print(f'checking {input_lines[y+1][x]}')
if input_lines[y+1][x] == 'L':
next_pos = (x, y+1)
direction = 1
elif input_lines[y+1][x] == 'J':
next_pos = (x, y+1)
direction = 3
elif input_lines[y+1][x] == '|':
next_pos = (x, y+1)
direction = 2
# left
if direction == -1 and x > 0:
# print(f'checking {input_lines[y][x-1]}')
if input_lines[y][x-1] == 'F':
next_pos = (x-1, y)
direction = 2
elif input_lines[y][x-1] == 'L':
next_pos = (x-1, y)
direction = 0
elif input_lines[y][x-1] == '-':
next_pos = (x-1, y)
direction = 3
print(f'{next_pos=} {direction=}')
pos_list.append(next_pos)
# loop follow directions - ends if next == 'S'
while input_lines[next_pos[1]][next_pos[0]] != 'S':
x = next_pos[0]
y = next_pos[1]
if direction == 0:
if input_lines[y - 1][x] == 'F':
next_pos = (x, y - 1)
direction = 1
elif input_lines[y - 1][x] == '7':
next_pos = (x, y - 1)
direction = 3
elif input_lines[y - 1][x] == '|':
next_pos = (x, y - 1)
direction = 0
elif input_lines[y - 1][x] == 'S':
break
elif direction == 1:
if input_lines[y][x + 1] == 'J':
next_pos = (x + 1, y)
direction = 0
elif input_lines[y][x + 1] == '7':
next_pos = (x + 1, y)
direction = 2
elif input_lines[y][x + 1] == '-':
next_pos = (x + 1, y)
direction = 1
elif input_lines[y][x + 1] == 'S':
break
elif direction == 2:
if input_lines[y + 1][x] == 'L':
next_pos = (x, y + 1)
direction = 1
elif input_lines[y + 1][x] == 'J':
next_pos = (x, y + 1)
direction = 3
elif input_lines[y + 1][x] == '|':
next_pos = (x, y + 1)
direction = 2
elif input_lines[y + 1][x] == 'S':
break
elif direction == 3:
if input_lines[y][x - 1] == 'F':
next_pos = (x - 1, y)
direction = 2
elif input_lines[y][x - 1] == 'L':
next_pos = (x - 1, y)
direction = 0
elif input_lines[y][x - 1] == '-':
next_pos = (x - 1, y)
direction = 3
elif input_lines[y][x - 1] == 'S':
break
pos_list.append(next_pos)
# print(f'{next_pos=} {direction=} {input_lines[next_pos[1]][next_pos[0]]}')
print(f'end found at {next_pos=}')
print(f'{pos_list=}')
print(f'{len(pos_list)/2=}')