Skip to the content.
import hashlib

Day 4: The Ideal Stocking Stuffer

Part 1

Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.

To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, …) that produces such a hash.

For example:

0. Set Puzzle Input

PUZZLE_INPUT = "ckczppom"

1. Calculate Hash

salt = 0

while True:
    testhash = PUZZLE_INPUT + str(salt)
    advent_coin = hashlib.md5(testhash.encode()).hexdigest()
    if str(advent_coin).startswith('00000'):
        break
    salt += 1

advent_coin

2. Print salt

salt

Part 2

Task 2: Now find one that starts with six zeroes.

3. Calculate Hash

salt = 0

while True:
    testhash = PUZZLE_INPUT + str(salt)
    advent_coin = hashlib.md5(testhash.encode()).hexdigest()
    if str(advent_coin).startswith('000000'):
        break
    salt += 1

advent_coin

4. Print salt

salt