mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2016-day17: md5 for bfs directions
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"flag"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/cast"
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var part int
|
||||
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
ans := md5Bfs(util.ReadFile("./input.txt"), part)
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
|
||||
var openPattern = regexp.MustCompile("[b-f]")
|
||||
|
||||
type node struct {
|
||||
coords [2]int
|
||||
path string
|
||||
distance int
|
||||
}
|
||||
|
||||
func md5Bfs(input string, part int) string {
|
||||
queue := []node{{path: input}}
|
||||
var longestPath string
|
||||
for len(queue) > 0 {
|
||||
front := queue[0]
|
||||
queue = queue[1:]
|
||||
|
||||
if front.coords == [2]int{3, 3} {
|
||||
validPath := front.path[len(input):]
|
||||
if part == 1 {
|
||||
return validPath
|
||||
}
|
||||
|
||||
if len(longestPath) < len(validPath) {
|
||||
longestPath = validPath
|
||||
}
|
||||
// cannot pass through the end point
|
||||
continue
|
||||
}
|
||||
|
||||
hash := fmt.Sprintf("%x", md5.Sum([]byte(front.path)))
|
||||
|
||||
for i, direction := range []struct {
|
||||
char string
|
||||
rowDiff int
|
||||
colDiff int
|
||||
}{
|
||||
{"U", -1, 0},
|
||||
{"D", 1, 0},
|
||||
{"L", 0, -1},
|
||||
{"R", 0, 1},
|
||||
} {
|
||||
nextRow := front.coords[0] + direction.rowDiff
|
||||
nextCol := front.coords[1] + direction.colDiff
|
||||
if nextRow >= 0 && nextRow < 4 && nextCol >= 0 && nextCol < 4 {
|
||||
if openPattern.MatchString(hash[i : i+1]) {
|
||||
queue = append(queue, node{
|
||||
coords: [2]int{nextRow, nextCol},
|
||||
path: front.path + direction.char,
|
||||
distance: front.distance + 1,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// part 2, stringified number...
|
||||
return cast.ToString(len(longestPath))
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func Test_md5Bfs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
part int
|
||||
want string
|
||||
}{
|
||||
{"part1_example1", "ihgpwlah", 1, "DDRRRD"},
|
||||
{"part1_actual", util.ReadFile("input.txt"), 1, "DDRUDLRRRD"},
|
||||
{"part2_example1", "ihgpwlah", 2, "370"},
|
||||
{"part2_actual", util.ReadFile("input.txt"), 2, "398"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := md5Bfs(tt.input, tt.part); got != tt.want {
|
||||
t.Errorf("md5Bfs() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user