aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-04 14:44:02 +0000
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-04 14:44:02 +0000
commitbac9d753650e22575602488fc2517e4bd8856203 (patch)
tree4b9c4471494c76a25faa988123ff28371f93e96c
parent22f3e6b8b5224be4a482c983c85ba23e4b48420e (diff)
Day 4, go this time because fast.
-rw-r--r--day4.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/day4.go b/day4.go
new file mode 100644
index 0000000..ebf8810
--- /dev/null
+++ b/day4.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "bytes"
+ "crypto/md5"
+ "fmt"
+ "strconv"
+)
+
+func hash(secret []byte, x int64) [16]byte {
+ str := strconv.AppendInt(secret, x, 10)
+ return md5.Sum(str)
+}
+
+func check(hash [16]byte, target []byte, length int) bool {
+ str := []byte(fmt.Sprintf("%x", hash))
+ return bytes.Equal(str[:length], target[:length])
+}
+
+func main() {
+ secret := []byte("iwrupvqb")
+ target := []byte("000000")
+ x := int64(1)
+ got5, got6 := false, false
+
+ for !(got5 && got6) {
+ h := hash(secret, x)
+ if !got5 && check(h, target, 5) {
+ fmt.Println("Lowest number for 5-zeroes hash:", x)
+ got5 = true
+ }
+ if !got6 && check(h, target, 6) {
+ fmt.Println("Lowest number for 6-zeroes hash:", x)
+ got6 = true
+ }
+ x += 1
+ }
+}