From 5caa178c1ca0b387693e058c884352d9d8694499 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 20:00:02 +0100 Subject: [PATCH] [py] Port 2016_05 --- py/2016/05/solve.py | 54 --------------------------------------------- py/aoc/__init__.py | 3 ++- py/aoc/y2016/d05.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 55 deletions(-) delete mode 100644 py/2016/05/solve.py create mode 100644 py/aoc/y2016/d05.py diff --git a/py/2016/05/solve.py b/py/2016/05/solve.py deleted file mode 100644 index 2f01b7b..0000000 --- a/py/2016/05/solve.py +++ /dev/null @@ -1,54 +0,0 @@ -import hashlib - -# PART 1 - -def chars(start, amount): - n = 0 - chars = [] - while amount > 0: - text = (start + str(n)).encode("utf-8") - h = hashlib.md5(text).hexdigest() - if h[:5] == "00000": - print(h, "->", char) - char = h[5] - chars.append(char) - amount -= 1 - n += 1 - return "".join(chars) - -# PART 2 - -def chars_2(start, amount): - n = 0 - chars = [None]*amount - while amount > 0: - text = (start + str(n)).encode("utf-8") - h = hashlib.md5(text).hexdigest() - if h[:5] == "00000": - char = h[6] - pos = int(h[5], base=16) - if pos < len(chars): - if chars[pos] is None: - chars[pos] = char - pw = "".join("_" if x is None else x for x in chars) - print(h, "->", char, "in position", pos, "->", pw) - if None not in chars: - return "".join(chars) - else: - print(h, "->", chars[pos], "already in position", pos) - else: - print(h, "->", "invalid position", pos) - n += 1 - -def main(hashstart): - print("Previously calculated") - print("Part 1: 4543c154") - print("Part 2: 1050cbbd") - print(f"Solutions") - pw = chars(hashstart, 8) - print(f"Part 1: {pw}") - pw_2 = chars_2(hashstart, 8) - print(f"Part 2: {pw_2}") - -if __name__ == "__main__": - main("ojvtpuvg") diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index 45deed6..de29880 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,7 +2,7 @@ import sys import argparse from pathlib import Path -from .y2016 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 from .y2020 import d10 @@ -14,6 +14,7 @@ DAYS = { "2016_02": y2016.d02.solve, "2016_03": y2016.d03.solve, "2016_04": y2016.d04.solve, + "2016_05": y2016.d05.solve, "2017_01": y2017.d01.solve, "2017_02": y2017.d02.solve, "2017_03": y2017.d03.solve, diff --git a/py/aoc/y2016/d05.py b/py/aoc/y2016/d05.py new file mode 100644 index 0000000..85e2a1f --- /dev/null +++ b/py/aoc/y2016/d05.py @@ -0,0 +1,52 @@ +import hashlib + +# PART 1 + + +def chars(start, amount): + n = 0 + chars = [] + while amount > 0: + text = (start + str(n)).encode("utf-8") + h = hashlib.md5(text).hexdigest() + if h[:5] == "00000": + # print(h, "->", char) + char = h[5] + chars.append(char) + amount -= 1 + n += 1 + return "".join(chars) + + +# PART 2 + + +def chars_2(start, amount): + n = 0 + chars = [None] * amount + while amount > 0: + text = (start + str(n)).encode("utf-8") + h = hashlib.md5(text).hexdigest() + if h[:5] == "00000": + char = h[6] + pos = int(h[5], base=16) + if pos < len(chars): + if chars[pos] is None: + chars[pos] = char + pw = "".join("_" if x is None else x for x in chars) + # print(h, "->", char, "in position", pos, "->", pw) + if None not in chars: + return "".join(chars) + # else: + # print(h, "->", chars[pos], "already in position", pos) + # else: + # print(h, "->", "invalid position", pos) + n += 1 + + +def solve(inputstr): + hashstart = inputstr.strip() + pw = chars(hashstart, 8) + print(f"Part 1: {pw}") + pw_2 = chars_2(hashstart, 8) + print(f"Part 2: {pw_2}")