This commit is contained in:
Stefan Harmuth 2021-10-24 18:09:53 +02:00
parent 1c8af2a8aa
commit 3cea8acd1f
4 changed files with 80 additions and 1 deletions

74
day17/day.go Normal file
View 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)
}

1
inputs/17 Normal file
View File

@ -0,0 +1 @@
pvhmgsws

3
inputs/17_test Normal file
View File

@ -0,0 +1,3 @@
ihgpwlah
kglvqrro
ulqzkmiv

View File

@ -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},