day04
This commit is contained in:
parent
aff0fbe573
commit
21cd974566
38
day04/day04.go
Normal file
38
day04/day04.go
Normal file
@ -0,0 +1,38 @@
|
||||
package day04
|
||||
|
||||
import (
|
||||
"aoc2015/aoclib"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func findMd5SumStartsWith(secretStart string, start string) int {
|
||||
var secret string
|
||||
var md5sum [16]byte
|
||||
counter := 0
|
||||
|
||||
for true {
|
||||
secret = secretStart + strconv.Itoa(counter)
|
||||
md5sum = md5.Sum([]byte(secret))
|
||||
if strings.HasPrefix(hex.EncodeToString(md5sum[:]), start) {
|
||||
return counter
|
||||
}
|
||||
counter++
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
func Part1(puzzle aoclib.Puzzle) interface{} {
|
||||
secretStart := puzzle.GetInputArray()[0]
|
||||
|
||||
return findMd5SumStartsWith(secretStart, "00000")
|
||||
}
|
||||
|
||||
func Part2(puzzle aoclib.Puzzle) interface{} {
|
||||
secretStart := puzzle.GetInputArray()[0]
|
||||
|
||||
return findMd5SumStartsWith(secretStart, "000000")
|
||||
}
|
||||
1
inputs/4_test
Normal file
1
inputs/4_test
Normal file
@ -0,0 +1 @@
|
||||
abcdef
|
||||
4
main.go
4
main.go
@ -5,6 +5,7 @@ import (
|
||||
"aoc2015/day01"
|
||||
"aoc2015/day02"
|
||||
"aoc2015/day03"
|
||||
"aoc2015/day04"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -30,6 +31,9 @@ func initDayFunctions() {
|
||||
dayFunctions[3] = make(map[int]func(puzzle aoclib.Puzzle) interface{})
|
||||
dayFunctions[3][1] = day03.Part1
|
||||
dayFunctions[3][2] = day03.Part2
|
||||
dayFunctions[4] = make(map[int]func(puzzle aoclib.Puzzle) interface{})
|
||||
dayFunctions[4][1] = day04.Part1
|
||||
dayFunctions[4][2] = day04.Part2
|
||||
}
|
||||
|
||||
func execute(thisDay int) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user