Skip to content

Commit

Permalink
d05
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaylatkar committed Jun 28, 2023
1 parent 0b0ec08 commit b9f5f0f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions day_05.py
Original file line number Diff line number Diff line change
@@ -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())
1 change: 1 addition & 0 deletions input/day_05
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cxdnnyjw

0 comments on commit b9f5f0f

Please sign in to comment.