day17
This commit is contained in:
parent
1c8af2a8aa
commit
3cea8acd1f
74
day17/day.go
Normal file
74
day17/day.go
Normal file
@ -0,0 +1,74 @@
|
||||
package day17
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"math"
|
||||
"tools"
|
||||
)
|
||||
|
||||
func getMD5Sum(s string) string {
|
||||
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
|
||||
}
|
||||
|
||||
type DirDelta struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
var dirDeltas = []DirDelta{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
|
||||
var dirs = []string{"U", "D", "L", "R"}
|
||||
var pathLen = math.MaxInt32
|
||||
var foundPath = ""
|
||||
|
||||
func findPath(passcode, path string, x, y int, findLongest bool) {
|
||||
doors := getMD5Sum(passcode + path)[:4]
|
||||
if !findLongest && len(path) >= pathLen {
|
||||
return
|
||||
}
|
||||
if x == 3 && y == 3 {
|
||||
if findLongest {
|
||||
if len(path) > pathLen {
|
||||
foundPath = path
|
||||
pathLen = len(path)
|
||||
}
|
||||
} else {
|
||||
if len(path) < pathLen {
|
||||
foundPath = path
|
||||
pathLen = len(path)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for i, c := range doors {
|
||||
if x+dirDeltas[i].x < 0 || x+dirDeltas[i].x > 3 || y+dirDeltas[i].y < 0 || y+dirDeltas[i].y > 3 {
|
||||
// border case
|
||||
continue
|
||||
}
|
||||
|
||||
if c > 97 { // door open
|
||||
findPath(passcode, path+dirs[i], x+dirDeltas[i].x, y+dirDeltas[i].y, findLongest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
||||
for _, passcode := range puzzle.GetInputArray() {
|
||||
pathLen = math.MaxInt32
|
||||
findPath(passcode, "", 0, 0, false)
|
||||
// fmt.Println("Passcode:", passcode, "Path:", foundPath)
|
||||
}
|
||||
|
||||
return foundPath
|
||||
}
|
||||
|
||||
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||
for _, passcode := range puzzle.GetInputArray() {
|
||||
pathLen = 0
|
||||
findPath(passcode, "", 0, 0, true)
|
||||
// fmt.Println("Passcode:", passcode, "Len:", len(foundPath), "Path:", foundPath)
|
||||
}
|
||||
|
||||
return len(foundPath)
|
||||
}
|
||||
3
inputs/17_test
Normal file
3
inputs/17_test
Normal file
@ -0,0 +1,3 @@
|
||||
ihgpwlah
|
||||
kglvqrro
|
||||
ulqzkmiv
|
||||
3
main.go
3
main.go
@ -16,6 +16,7 @@ import (
|
||||
"aoc2016/day14"
|
||||
"aoc2016/day15"
|
||||
"aoc2016/day16"
|
||||
"aoc2016/day17"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -49,7 +50,7 @@ func initDayFunctions() {
|
||||
14: {1: day14.Part1, 2: day14.Part2},
|
||||
15: {1: day15.Part1, 2: day15.Part2},
|
||||
16: {1: day16.Part1, 2: day16.Part2},
|
||||
// 17: {1: day17.Part1, 2: day17.Part2},
|
||||
17: {1: day17.Part1, 2: day17.Part2},
|
||||
// 18: {1: day18.Part1, 2: day18.Part2},
|
||||
// 19: {1: day19.Part1, 2: day19.Part2},
|
||||
// 20: {1: day20.Part1, 2: day20.Part2},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user