From 47d9ff38d4a45aa31e4919b30ade6151df997d5a Mon Sep 17 00:00:00 2001 From: Fabian Tessmer Date: Sat, 22 Mar 2025 08:36:27 +0100 Subject: [PATCH] Advent of Code 2015 day 4 part2: solved --- 2015/day4/input.txt | 1 + 2015/day4/problem.txt | 17 +++++++++++++++++ 2015/day4/solution.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 2015/day4/input.txt create mode 100644 2015/day4/problem.txt create mode 100644 2015/day4/solution.py diff --git a/2015/day4/input.txt b/2015/day4/input.txt new file mode 100644 index 0000000..203cf7c --- /dev/null +++ b/2015/day4/input.txt @@ -0,0 +1 @@ +yzbqklnj \ No newline at end of file diff --git a/2015/day4/problem.txt b/2015/day4/problem.txt new file mode 100644 index 0000000..498717b --- /dev/null +++ b/2015/day4/problem.txt @@ -0,0 +1,17 @@ +--- Day 4: The Ideal Stocking Stuffer --- +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: + +If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so. +If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef.... +Your puzzle answer was 282749. + +--- Part Two --- +Now find one that starts with six zeroes. + +Your puzzle answer was 9962624. + +Both parts of this puzzle are complete! They provide two gold stars: ** \ No newline at end of file diff --git a/2015/day4/solution.py b/2015/day4/solution.py new file mode 100644 index 0000000..ee3e6fa --- /dev/null +++ b/2015/day4/solution.py @@ -0,0 +1,29 @@ +import hashlib + + +def get_first_md5_with_leading_zeroes(salt: str, num_zeros: int = 5) -> int: + pepper = 0 + solution_found = False + leading_zeros = '0' * num_zeros + + while not solution_found: + whole_string = salt + str(pepper) + if hashlib.md5(whole_string.encode()).hexdigest()[:num_zeros] == leading_zeros: + solution_found = True + pass + else: + pepper += 1 + + return pepper + + +if __name__ == "__main__": + assert get_first_md5_with_leading_zeroes("abcdef") == 609043, "Error: Example 1 couldn't be solved" + assert get_first_md5_with_leading_zeroes("pqrstuv") == 1048970, "Error: Example 2 couldn't be solved" + print("All tests passed") + + puzzle_input = open("input.txt", "r").readline() + print("solution: ", get_first_md5_with_leading_zeroes(puzzle_input)) + + print("Part 2: ") + print("solution: ", get_first_md5_with_leading_zeroes(puzzle_input, 6))