-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_05.py
46 lines (33 loc) · 1003 Bytes
/
day_05.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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())