-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
74 lines (63 loc) · 1.39 KB
/
common.go
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
package main
import (
"fmt"
"math"
"sort"
)
const salt = "yjdafjpo"
type Hash func(salt string, i int) string
func run(hashFn Hash) {
var approved []int
candidates := make(map[string][]int)
limit := math.MaxInt64
for i := 0; i <= limit; i++ {
hex := hashFn(salt, i)
for c := range quintuples(hex) {
for _, candidateID := range candidates[c] {
if i <= candidateID+1000 {
approved = append(approved, candidateID)
if len(approved) == 64 {
// When I found at least 64 items, I need to be sure that
// there aren't other items with a smaller ID and a validation
// in the next 1000 IDs, so I run other 1000 iterations
limit = i + 1000
}
}
}
candidates[c] = []int{}
}
if c, ok := triples(hex); ok {
candidates[c] = append(candidates[c], i)
}
}
// approved now could be greater than 64 items. I need to sort items and
// take the 64th
sort.Ints(approved)
fmt.Println(approved[63])
}
func triples(s string) (string, bool) {
for i := 0; i < len(s)-2; i++ {
if same(s[i : i+3]) {
return string(s[i]), true
}
}
return "", false
}
func quintuples(s string) map[string]bool {
res := make(map[string]bool)
for i := 0; i < len(s)-4; i++ {
if same(s[i : i+5]) {
res[string(s[i])] = true
i += 4
}
}
return res
}
func same(s string) bool {
for i := 1; i < len(s); i++ {
if s[i] != s[0] {
return false
}
}
return true
}