diff --git a/2023/10/10_2.py b/2023/10/10_2.py new file mode 100644 index 0000000..8225c0b --- /dev/null +++ b/2023/10/10_2.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# 2023 - Advent Of Code 10 - part 2 +# disable pylint since it's unfinished +# pylint: skip-file + +# 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 range(0, len(input_lines)): + for x in range(0, len(input_lines[y])): + if input_lines[y][x] == 'S': + start_pos = (x, y) + print(f'\033[1m{input_lines[y][x]}\033[0m', end='') + else: + print(input_lines[y][x], 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=}')