-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (100 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
_ "embed"
"crypto/md5"
"fmt"
. "github.com/beanz/advent/lib-go"
)
//go:embed input.txt
var input []byte
type Game struct {
h, w int
passcode string
debug bool
}
func (g *Game) Code(path string) map[rune]bool {
r := map[rune]bool{}
charToDir := map[int]rune{}
charToDir[0] = 'U'
charToDir[1] = 'D'
charToDir[2] = 'L'
charToDir[3] = 'R'
sum := fmt.Sprintf("%x", md5.Sum([]byte(g.passcode+path)))
for i, dir := range charToDir {
if sum[i] == 'b' || sum[i] == 'c' || sum[i] == 'd' ||
sum[i] == 'e' || sum[i] == 'f' {
r[dir] = true
}
}
return r
}
func (g *Game) Neighbours(path string, p Point) map[rune]Point {
code := g.Code(path)
n := map[rune]Point{}
if p.Y > 0 && code['U'] {
n['U'] = Point{p.X, p.Y - 1}
}
if p.X > 0 && code['L'] {
n['L'] = Point{p.X - 1, p.Y}
}
if p.X < g.w-1 && code['R'] {
n['R'] = Point{p.X + 1, p.Y}
}
if p.Y < g.h-1 && code['D'] {
n['D'] = Point{p.X, p.Y + 1}
}
return n
}
type Search struct {
p Point
path string
}
func (g Game) Play(part2 bool) string {
todo := []Search{Search{Point{0, 0}, ""}}
longest := ""
for len(todo) > 0 {
cur := todo[0]
todo = todo[1:]
if g.debug {
fmt.Printf("Checking [%d,%d] %s\n", cur.p.X, cur.p.Y, cur.path)
}
if cur.p.X == (g.w-1) && cur.p.Y == (g.h-1) {
if g.debug {
fmt.Printf("Found solution %s\n", cur.path)
}
if !part2 {
return cur.path
}
if len(cur.path) > len(longest) {
longest = cur.path
}
continue
}
for dir, np := range g.Neighbours(cur.path, cur.p) {
if g.debug {
fmt.Printf(" door %s to %d,%d\n", string(dir), np.X, np.Y)
}
todo = append(todo, Search{np, cur.path + string(dir)})
}
}
return longest
}
func (g Game) Part1() string {
return g.Play(false)
}
func (g Game) Part2() int {
return len(g.Play(true))
}
func main() {
in := InputLines(input)
game := Game{4, 4, in[0], false}
p1 := game.Part1()
if !benchmark {
fmt.Printf("Part 1: %s\n", p1)
}
p2 := game.Part2()
if !benchmark {
fmt.Printf("Part 2: %d\n", p2)
}
}
var benchmark = false