Day 23 - Advent of Code 2023

Working solutions for the day 23 puzzles.

Part One

""" day_23_01.py """

# usage: python3 day_23_01.py < input

import sys


raw = sys.stdin.read()
width, height = raw.index('\n'), raw.count('\n')
raw = [i for i in raw if i != '\n']

maze = {}
for i, j in enumerate(raw):
    if j == '#':
        continue
    match j:
        case '^':
            k = [i - width]
        case '>':
            k = [i + 1]
        case 'v':
            k = [i + width]
        case '<':
            k = [i - 1]
        case _:
            k = []
            if i - width >= 0 and raw[i - width] != 'v':
                k.append(i - width)
            if raw[i + 1] != '<':
                k.append(i + 1)
            if i + width < width * height and raw[i + width] != '^':
                k.append(i + width)
            if raw[i - 1] != '>':
                k.append(i - 1)
    maze[i] = [m for m in k if 0 <= m < width * height and raw[m] != '#']

hikes = []
begin, end = list(maze)[0], list(maze)[-1]
explore = [begin]
visited = []
while explore:
    while explore and end not in visited:
        while visited and explore[-1] not in maze[visited[-1]]:
            visited.pop()
        tile = explore.pop()
        visited.append(tile)
        explore.extend([i for i in maze[tile]
                        if i not in visited and i not in explore])
    if end in visited:
        hikes.append(len(visited) - 1)
    if explore:
        while visited and explore[-1] not in maze[visited[-1]]:
            visited.pop()

print(max(hikes))

Part Two