Skip to content

Instantly share code, notes, and snippets.

@akahn
Created December 3, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akahn/46cfcdd2e82e8e5b3031f87562c7d8f2 to your computer and use it in GitHub Desktop.
Save akahn/46cfcdd2e82e8e5b3031f87562c7d8f2 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/binary"
"encoding/hex"
"log"
"runtime"
"sync"
"sync/atomic"
"time"
)
func main() {
cores := runtime.NumCPU()
end := time.Now().Unix()
chunkSize := int64(float64(end) / float64(cores))
log.Printf("Dividing %d timestamps into %d chunks of size %d", end, cores, chunkSize)
wg := sync.WaitGroup{}
count := atomic.Int64{}
for i := 0; i < cores; i += 1 {
wg.Add(1)
go scanChunk(i, chunkSize, &count, &wg)
}
wg.Wait()
total := count.Load()
log.Printf("Total: %d/%d (%f)", total, end, float64(total)/float64(end))
}
func scanChunk(j int, chunkSize int64, count *atomic.Int64, wg *sync.WaitGroup) {
chunkStart := int64(j) * chunkSize
chunkEnd := chunkStart + chunkSize - 1
timestamp := uint32(chunkStart)
log.Printf("Spawned goroutine %d from %d to %d (difference %d)", j, chunkStart, chunkEnd, chunkEnd-chunkStart)
numericalCount := 0
passes := 0
var b [4]byte
for {
if timestamp >= uint32(chunkEnd) {
log.Printf("Goroutine %d reached the end (%d). Found numerical hexes in %d/%d (%f) passes.", j, timestamp, numericalCount, passes, float64(numericalCount)/float64(passes))
count.Add(int64(numericalCount))
wg.Done()
return
}
binary.BigEndian.PutUint32(b[0:4], timestamp)
hex := hex.EncodeToString(b[:])
if containsLetter([]byte(hex)) {
// Found an alphabetical character, skip this ID altogether
passes += 1
timestamp += 1
continue
}
// No alphabetical characters encountered
numericalCount += 1
passes += 1
timestamp += 1
}
}
func containsLetter(id []byte) bool {
for _, ch := range id {
if ch >= 97 && ch <= 122 {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment