-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday04.nim
41 lines (32 loc) · 941 Bytes
/
day04.nim
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
import md5, strutils, sequtils
import threadpool
{.experimental.}
const
salt = "ckczppom"
batchSize = 100_000
func firstCondition(hash: MD5Digest): bool =
hash[0] == 0 and hash[1] == 0 and hash[2] < 16
func secondCondition(hash: MD5Digest): bool =
hash[0] == 0 and hash[1] == 0 and hash[2] == 0
proc helper(start: int, isFirst: bool): int =
var
s: string
hash: MD5Digest
for i in start ..< start + batchSize:
s = salt & $i
hash = s.toMD5
if isFirst and firstCondition(hash):
return i
elif secondCondition(hash):
return i
proc solve(): tuple[first, second: int] =
var
first = newSeq[int](2)
second = newSeq[int](40)
parallel:
for i in 0 .. first.high:
first[i] = spawn helper(i * batchSize, true)
for i in 1 .. second.high:
second[i] = spawn helper(i * batchSize, false)
result = (first.filterIt(it > 0).min, second.filterIt(it > 0).min)
echo solve()