Dazbo's Advent of Code solutions, written in Python
Day 5: How About a Nice Game of Chess?
MD5 HashinghashlibString ManipulationLooping
In this Advent of Code puzzle, we are tasked with finding an eight-character password for a security door. The password is generated by repeatedly hashing a combination of a “Door ID” (our puzzle input) and an incrementing integer (nonce). A character is revealed if the hexadecimal representation of the MD5 hash starts with five zeroes.
For example, if the Door ID is abc
:
abc3231929
, and its sixth character (the one after the five zeroes) would be the first character of the password.Our goal is to find this eight-character password.
The first part requires us to construct the password by taking the sixth character of each valid hash (those starting with five zeroes) in the order they are found.
nonce
of 0 and increment it in each iteration. We concatenate the door_id
with the current nonce
to form a string.hashlib
module in Python is used for this purpose.import hashlib
import logging
def main():
# ... (file reading and setup) ...
door_id = "your_door_id_from_input"
logging.info(f"Door ID: {door_id}")
# Part 1
pwd = ""
nonce = 0
while len(pwd) < 8:
data = door_id + str(nonce)
hash_hex = hashlib.md5(data.encode()).hexdigest()
if hash_hex.startswith("00000"):
pwd = pwd + hash_hex[5]
logging.debug(f"Found {hash_hex} with data {data}.")
logging.info(f"Pwd is: {pwd}")
nonce += 1
# ...
The second part introduces a twist: the password characters are placed at specific positions. The sixth character of a valid hash now indicates the position (0-7) in the password, and the seventh character is the actual character to be placed at that position. If a position is already filled, we ignore subsequent attempts to fill it.
"________"
.door_id
and incrementing nonce
.import hashlib
import logging
def main():
# ... (Part 1 code) ...
# Part 2
pwd = "________"
nonce = 0
while "_" in pwd:
data = door_id + str(nonce)
hash_hex = hashlib.md5(data.encode()).hexdigest()
if hash_hex.startswith("00000"):
position = hash_hex[5]
if position in "01234567": # Check if position is a valid digit 0-7
position = int(position)
if pwd[position] == "_": # Only fill if position is not already taken
pwd = pwd[:position] + hash_hex[6] + pwd[position+1:]
logging.debug(f"Found {hash_hex} with data {data}.")
logging.info(f"{pwd}")
nonce += 1
# ...
This puzzle is a straightforward application of MD5 hashing and string manipulation. The core challenge lies in efficiently generating and checking a large number of hashes. Part 2 adds a layer of complexity by introducing positional requirements and the need to handle already-filled positions, making it a good exercise in careful state management during iteration. The hashlib
module in Python provides a convenient way to perform the necessary cryptographic hashing operations.