Day 5 - Advent of Code 2016

Working solutions for the day 5 puzzles.

Part One

""" day_05_01.py """

# usage: python3 day_05_01.py < input

from hashlib import md5


door_id = input()

length = 8
password = ''
salt = 0
while length > 0:
    seed = door_id + str(salt)
    seed_hash = md5(seed.encode()).hexdigest()
    if seed_hash.startswith('00000'):
        password = password + seed_hash[5]
        length -= 1
    salt += 1

print(password)

Part Two

""" day_05_02.py """

# usage: python3 day_05_02.py < input

from hashlib import md5


door_id = input()

positions = range(8)
valid = [str(i) for i in positions]
password = ['-' for _ in positions]
salt = 0
while password.count('-') > 0:
    seed = door_id + str(salt)
    seed_hash = md5(seed.encode()).hexdigest()
    if seed_hash.startswith('00000'):
        pos = seed_hash[5]
        if pos in valid:
            ipos = int(pos)
            if password[ipos] == '-':
                password[ipos] = seed_hash[6]
    salt += 1

print(''.join(password))