Advent of Code 2015 Day 4

Author

Nathan Moore

— Day 4: The Ideal Stocking Stuffer —

Santa is mining some AdventCoins.

To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, …) that produces such a hash.

import hashlib

with open('data-2015-04.txt', 'r') as f:
    inp = f.read()

i = 0
chck = ''

while chck[:5] != '00000':
    i += 1
    chck = hashlib.md5((inp + str(i)).encode('utf-8')).hexdigest()

print(i)
346386
print(chck)
0000045c5e2b3911eb937d9d8c574f09

# 346386

Let’s try this in R as well!

library(openssl)
Linking to: OpenSSL 3.6.0 1 Oct 2025
i = 0
inp = readLines('data-2015-04.txt')
chck = ''

while (substr(chck, 1, 5) != '00000') {
    i = i + 1
    chck = md5(paste0(inp, as.character(i)))
}

print(i)
[1] 346386
print(chck)
[1] "0000045c5e2b3911eb937d9d8c574f09"

— Part Two —

Now find one that starts with six zeroes.

i = 0
chck = ''

while chck[:6] != '000000':
    i += 1
    chck = hashlib.md5((inp + str(i)).encode('utf-8')).hexdigest()

print(i)
9958218
print(chck)
00000094434e1914548b3a1af245fb27

# 9958218