adventofcode/2015/day04/main.go

48 lines
810 B
Go
Raw Permalink Normal View History

2016-12-16 22:21:15 +00:00
package main
import (
"crypto/md5"
"fmt"
"io"
"log"
"strconv"
)
func main() {
var readInp string
for {
_, err := fmt.Scan(&readInp)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
}
firstCoin := searchChunk(readInp, 0, 1000000000)
fmt.Printf("\n%s%d :: %d\n", readInp, firstCoin, firstCoin)
}
func searchChunk(key string, start, end int) int {
// So we know it's still running
for i := start; i <= end; i++ {
coin := key + strconv.Itoa(i)
coinMd5 := fmt.Sprintf("%x", md5.Sum([]byte(coin)))
if isCoin(coinMd5, "000000") {
return i
}
}
return -1
}
func isCoin(s string, moneyMaker string) bool {
if len(s) < len(moneyMaker) {
return false
}
for i := 0; i < len(moneyMaker); i++ {
if s[i] != moneyMaker[i] {
return false
}
}
return true
}