Day 22 - Advent of Code 2022
Working solutions for the day 22 puzzles.
Part One
""" day_22_01.py """
# usage: python3 day_22_01.py < input
import sys
def parse(data):
""" extract board and path to follow """
rows = [list(row) for row in data.splitlines()]
directions = []
steps = ''
for i in rows[-1]:
if i in '0123456789':
steps = steps + i
else:
directions.extend([int(steps), i])
steps = ''
directions.append(int(steps))
return rows[:-2], directions
def start(data):
""" find first '.' from top-left of board """
return data[0].index('.'), 0
def get(pos, data):
""" retrieve value ignoring out of range """
try:
return data[pos[1]][pos[0]]
except IndexError:
return ' '
board, path = parse(sys.stdin.read())
width, height = max([len(i) for i in board]), len(board)
deltas = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)}
facing = 0
x, y = start(board)
for step in path:
if step == 'L':
facing = (facing - 1) % 4
elif step == 'R':
facing = (facing + 1) % 4
else:
dx, dy = deltas[facing]
for _ in range(step):
while True:
if get((x, y), board) != ' ':
x0, y0 = x, y
x1, y1 = (x + dx) % width, (y + dy) % height
if get((x1, y1), board) == ' ':
x, y = x1, y1
continue
break
if get((x1, y1), board) == '.':
x, y = x1, y1
continue
x, y = x0, y0
break
print(1000 * (y + 1) + 4 * (x + 1) + facing)
Part Two
# standby ...