diff --git a/day17/day.go b/day17/day.go new file mode 100644 index 0000000..69e1764 --- /dev/null +++ b/day17/day.go @@ -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) +} diff --git a/inputs/17 b/inputs/17 new file mode 100644 index 0000000..4fa48d1 --- /dev/null +++ b/inputs/17 @@ -0,0 +1 @@ +pvhmgsws \ No newline at end of file diff --git a/inputs/17_test b/inputs/17_test new file mode 100644 index 0000000..513c03c --- /dev/null +++ b/inputs/17_test @@ -0,0 +1,3 @@ +ihgpwlah +kglvqrro +ulqzkmiv \ No newline at end of file diff --git a/main.go b/main.go index 1158ff3..1101620 100644 --- a/main.go +++ b/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},