Skip to content

Instantly share code, notes, and snippets.

@boone
Created December 7, 2015 03:31
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 boone/285424635823715a0889 to your computer and use it in GitHub Desktop.
Save boone/285424635823715a0889 to your computer and use it in GitHub Desktop.
// http://adventofcode.com/day/4
// key should be passed on the command line as the first argument
// number of leading zeroes is the second argument
package main
import "crypto/md5"
import "fmt"
import "os"
import "strconv"
import "strings"
func main() {
key := os.Args[1]
leading_zeroes, _ := strconv.Atoi(os.Args[2])
comparison := strings.Repeat("0", leading_zeroes)
// loop infinitely until we find an MD5 string that has so many leading zeroes
count := 0
for {
// let us know that this is running
if count % 10000 == 0 {
fmt.Printf(".")
}
// concatenate the key and the value
test := key + strconv.Itoa(count)
// determine the MD5 sum as a byte array
md5sum := md5.Sum([]byte(test))
// convert the byte array to a string
md5string := fmt.Sprintf("%x", md5sum)
// compare and break if we find a match
if md5string[:leading_zeroes] == comparison {
fmt.Printf("\n%d: %x\n", count, md5sum)
break
}
count += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment