-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.py
74 lines (47 loc) · 1.12 KB
/
14.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from lib import *
input = read_input(2016, 14).strip()
def hash(idx):
return hashlib.md5(f"{input}{idx}".encode()).hexdigest()
def first(idx):
if match := re.match(r"^.*?((.)\2{2}).*$", hash(idx)):
return match.group(2)
def second(idx, m):
for i in range(1, 1001):
if m * 5 in hash(idx + i):
return True
return False
idx = 0
cnt = 0
while True:
if (x := first(idx)) and second(idx, x):
cnt += 1
if cnt == 64:
break
idx += 1
print(idx)
dp = {}
def hash(idx):
if idx in dp:
return dp[idx]
out = hashlib.md5(f"{input}{idx}".encode()).hexdigest()
for _ in range(2016):
out = hashlib.md5(out.encode()).hexdigest()
dp[idx] = out
return out
def first(idx):
if match := re.match(r"^.*?((.)\2{2}).*$", hash(idx)):
return match.group(2)
def second(idx, m):
for i in range(1, 1001):
if m * 5 in hash(idx + i):
return True
return False
idx = 0
cnt = 0
while True:
if (x := first(idx)) and second(idx, x):
cnt += 1
if cnt == 64:
break
idx += 1
print(idx)