Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2015-12-04 - The Ideal Stocking Stuffer

(original .ipynb)

Puzzle input is a seven-character string. Part one requires us to find the lowest number n where the md5 hash of the input concatenated with n starts with five "0"s. Part two is the same but for six "0"s.

import hashlib

puzzle_input_str = open("./puzzle_input/day4.txt").read()


def adventcoin_hash(input_str_encoded, n):
    return hashlib.md5(input_str_encoded + str(n).encode())


def part_one(input_str, leading_zeroes=5):
    encoded = input_str.encode()
    n = 0
    target = leading_zeroes * "0"
    
    while True:
        result = adventcoin_hash(encoded, n)
        if result.hexdigest()[:leading_zeroes] == target:
            return n
        n += 1
    return -1


if False:
    assert 609043 == part_one("abcdef")
    assert 1048970 == part_one("pqrstuv")

print("part one:", part_one(puzzle_input_str))
part one: 282749
def part_two(input_str):
    return part_one(input_str, 6)

print("part two:", part_two(puzzle_input_str))
part two: 9962624