Day 17 - Advent of Code 2016
Working solution for the day 17 puzzles.
Parts One and Two
""" day_17_01_02.py """
# usage: python3 day_17_01_02.py udskfozm
import sys
from hashlib import md5
def status(secret, salt):
""" what's the door status """
digest = md5()
digest.update((secret + salt).encode())
return [i not in 'bcdef' for i in digest.hexdigest()[:4]]
def move(pos, goal, secret, steps=''):
""" move to goal """
global solution
if pos == goal:
return pos, steps
deltas = [(-1, 0), (1, 0), (0, -1), (0, 1)]
symbols = ['U', 'D', 'L', 'R']
for i, locked in enumerate(status(secret, steps)):
if locked:
continue
row = pos[0] + deltas[i][0]
col = pos[1] + deltas[i][1]
if 0 <= row <= 3 and 0 <= col <= 3:
next_pos = row, col
next_steps = steps + symbols[i]
end_pos, end_steps = move(next_pos, goal, secret, next_steps)
if end_pos == goal:
if solution[0] == '' or len(end_steps) < len(solution[0]):
solution[0] = end_steps
if solution[1] == '' or len(end_steps) > len(solution[1]):
solution[1] = end_steps
return pos, steps
passcode = sys.argv[1]
start = 0, 0
end = 3, 3
solution = ['', '']
move(start, end, passcode)
print(solution[0])
print(len(solution[1]))