From b9f5f0fc6649f34b4ebaf7f4d53217ec3d35444d Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Wed, 28 Jun 2023 15:32:58 +0530 Subject: [PATCH] d05 --- day_05.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ input/day_05 | 1 + 2 files changed, 47 insertions(+) create mode 100644 day_05.py create mode 100644 input/day_05 diff --git a/day_05.py b/day_05.py new file mode 100644 index 0000000..e77bba0 --- /dev/null +++ b/day_05.py @@ -0,0 +1,46 @@ +# https://adventofcode.com/2016/day/5 + +from helper import get_input, print_result +from hashlib import md5 + +DAY = 5 + +inp = get_input(DAY)[0] + + +def p1(): + res = "" + count = 0 + + def get_next_char(door_id, count): + while True: + h = md5(f"{door_id}{count}".encode("utf-8")).hexdigest() + if h[:5] == "00000": + return h[5], count + 1 + count += 1 + + for _ in range(8): + ch, count = get_next_char(inp, count) + res += ch + return res + + +def p2(): + res = [""] * 8 + count = 0 + + def get_next_char(door_id, count): + while True: + h = md5(f"{door_id}{count}".encode("utf-8")).hexdigest() + if h[:5] == "00000" and h[5] >= "0" and h[5] < "8": + return int(h[5]), h[6], count + 1 + count += 1 + + while res.count(""): + idx, ch, count = get_next_char(inp, count) + if res[idx] == "": + res[idx] = ch + return "".join(res) + + +print_result(DAY, p1(), p2()) diff --git a/input/day_05 b/input/day_05 new file mode 100644 index 0000000..a734d02 --- /dev/null +++ b/input/day_05 @@ -0,0 +1 @@ +cxdnnyjw