Skip to content

Instantly share code, notes, and snippets.

@boyter
Created June 19, 2024 03:29
Show Gist options
  • Save boyter/8600199cc6f4073dc9da380f3224ff25 to your computer and use it in GitHub Desktop.
Save boyter/8600199cc6f4073dc9da380f3224ff25 to your computer and use it in GitHub Desktop.
Simple Go code for https://shallenge.quirino.net/
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math"
"runtime"
"sync"
"sync/atomic"
)
var name = "boyter/"
var zeroCount int = 0
var mutex = sync.Mutex{}
var semaphore = make(chan bool, runtime.NumCPU())
var totalHashCount int64
func main() {
jumpSize := int64(10_000_000)
for i := int64(0); i < math.MaxInt64; i += jumpSize {
semaphore <- true
go func(i int64) {
HashEm(i, i+jumpSize)
<-semaphore
}(i)
}
}
func HashEm(start, end int64) {
hash := sha256.New()
for i := start; i <= end; i++ {
atomic.AddInt64(&totalHashCount, 1)
hash.Reset()
hash.Write([]byte(fmt.Sprintf("%s%d", name, i)))
sum := hash.Sum(nil)
h := hex.EncodeToString(sum)
zc := HashZeroCount(h)
mutex.Lock()
if zc != 0 && zc >= zeroCount {
zeroCount = zc
fmt.Println(fmt.Sprintf("%s%d", name, i), h, zeroCount)
}
mutex.Unlock()
}
}
const zeroByte = 48
func HashZeroCount(hash string) int {
zCount := 0
for _, ch := range hash {
if ch != zeroByte {
return zCount
}
zCount++
}
return zCount
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment