From 0a6b2fc1339642e209cd511e3278b829d10309fa Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 20:09:41 +0100 Subject: [PATCH] [py] Port 2015_04 --- py/2015/04/solve.py | 33 --------------------------------- py/aoc/__init__.py | 3 ++- py/aoc/y2015/d04.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 34 deletions(-) delete mode 100644 py/2015/04/solve.py create mode 100644 py/aoc/y2015/d04.py diff --git a/py/2015/04/solve.py b/py/2015/04/solve.py deleted file mode 100644 index 8af9aae..0000000 --- a/py/2015/04/solve.py +++ /dev/null @@ -1,33 +0,0 @@ -import hashlib - -# PART 1 - -def leading_zeroes(amount, start): - n = 1 - what = "0"*amount - while True: - text = (start + str(n)).encode("utf-8") - h = hashlib.md5(text).hexdigest() - if h[:amount] == what: - return n - - if n % 100000 == 0: - print(f"{n:9} {text} {h}") - - n += 1 - -# PART 2 - -def main(hashstart): - print("Previously calculated:") - print("Part 1: 282749") - print("Part 2: 9962624") - print() - print(f"Solutions") - n = leading_zeroes(5, hashstart) - print(f"Part 1: {n}") - n_2 = leading_zeroes(6, hashstart) - print(f"Part 2: {n_2}") - -if __name__ == "__main__": - main("yzbqklnj") diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index d719098..930bb23 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,7 +2,7 @@ import sys import argparse from pathlib import Path -from .y2015 import d01, d02, d03 +from .y2015 import d01, d02, d03, d04 from .y2016 import d01, d02, d03, d04, d05 from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09 from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11 @@ -14,6 +14,7 @@ DAYS = { "2015_01": y2015.d01.solve, "2015_02": y2015.d02.solve, "2015_03": y2015.d03.solve, + "2015_04": y2015.d04.solve, "2016_01": y2016.d01.solve, "2016_02": y2016.d02.solve, "2016_03": y2016.d03.solve, diff --git a/py/aoc/y2015/d04.py b/py/aoc/y2015/d04.py new file mode 100644 index 0000000..2a5a769 --- /dev/null +++ b/py/aoc/y2015/d04.py @@ -0,0 +1,29 @@ +import hashlib + +# PART 1 + + +def leading_zeroes(amount, start): + n = 1 + what = "0" * amount + while True: + text = (start + str(n)).encode("utf-8") + h = hashlib.md5(text).hexdigest() + if h[:amount] == what: + return n + + # if n % 100000 == 0: + # print(f"{n:9} {text} {h}") + + n += 1 + + +# PART 2 + + +def solve(inputstr): + hashstart = inputstr.strip() + n = leading_zeroes(5, hashstart) + print(f"Part 1: {n}") + n_2 = leading_zeroes(6, hashstart) + print(f"Part 2: {n_2}")