-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathday22.go
167 lines (150 loc) · 3.44 KB
/
day22.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"flag"
"fmt"
)
var depth = flag.Int("depth", 8112, "The depth of the cave system.")
var targetX = flag.Int("targetX", 13, "The target's X coordinate.")
var targetY = flag.Int("targetY", 743, "The target's Y coordinate.")
var margin = flag.Int("margin", 150, "The margin by which we'll check 'longer' paths.")
type Position struct {
X, Y int
Climb bool
Torch bool
}
func (p Position) Passable(erosion [][]int) bool {
if p.X < 0 || p.X >= len(erosion) {
return false
}
if p.Y < 0 || p.Y >= len(erosion[0]) {
return false
}
terrain := erosion[p.X][p.Y] % 3
switch terrain {
case 0:
// Rocky
return p.Climb || p.Torch
case 1:
// Wet
return !p.Torch
case 2:
// Narrow
return !p.Climb
}
return false
}
func (p Position) Moves(erosion [][]int) map[Position]int {
ret := make(map[Position]int)
terrain := erosion[p.X][p.Y] % 3
swapGear := p
switch terrain {
case 0:
// Rocky
if !p.Climb && !p.Torch {
// Illegal position.
return ret
}
swapGear.Climb = !swapGear.Climb
swapGear.Torch = !swapGear.Torch
case 1:
// Wet
if swapGear.Torch {
// Illegal position.
return ret
}
swapGear.Climb = !swapGear.Climb
case 2:
// Narrow
if swapGear.Climb {
// Illegal position.
return ret
}
swapGear.Torch = !swapGear.Torch
}
ret[swapGear] = 7
up := p
up.Y--
if up.Passable(erosion) {
ret[up] = 1
}
down := p
down.Y++
if down.Passable(erosion) {
ret[down] = 1
}
left := p
left.X--
if left.Passable(erosion) {
ret[left] = 1
}
right := p
right.X++
if right.Passable(erosion) {
ret[right] = 1
}
return ret
}
func main() {
flag.Parse()
risk := 0
// [x][y] in ordering.
geoIdx := make([][]int, *targetX+1+*margin)
erosion := make([][]int, *targetX+1+*margin)
for x := 0; x <= *targetX+*margin; x++ {
geoIdx[x] = make([]int, *targetY+1+*margin)
erosion[x] = make([]int, *targetY+1+*margin)
for y := 0; y <= *targetY+*margin; y++ {
if (x == 0 && y == 0) || (x == *targetX && y == *targetY) {
geoIdx[x][y] = 0
} else if x == 0 {
geoIdx[x][y] = y * 48271
} else if y == 0 {
geoIdx[x][y] = x * 16807
} else {
// This should already be memoized.
geoIdx[x][y] = erosion[x-1][y] * erosion[x][y-1]
}
erosion[x][y] = (geoIdx[x][y] + *depth) % 20183
terrain := erosion[x][y] % 3
if x <= *targetX && y <= *targetY {
risk += terrain
}
}
}
fmt.Printf("Total risk of area is %d\n", risk)
start := Position{0, 0, false, true}
winner := Position{*targetX, *targetY, false, true}
shortestPath := ComputeShortest(start, winner, erosion)
fmt.Printf("Took %d moves to reach our friend.\n", shortestPath)
}
func ComputeShortest(start, winner Position, erosion [][]int) int {
seen := make(map[Position]int)
seen[start] = 0
queue := []Position{start}
// Perform a BFS with backtracking.
for len(queue) != 0 {
pos := queue[0]
queue = queue[1:]
if shortest, ok := seen[pos]; !ok {
// We don't know how to get here yet.
continue
} else {
moves := pos.Moves(erosion)
if len(moves) == 0 {
// Illegal position.
continue
}
for neighbor, incremental := range moves {
shortestPathToNeighbor, ok := seen[neighbor]
newPathLength := shortest + incremental
if !ok || newPathLength < shortestPathToNeighbor {
// Record new paths, as well as shortened paths.
// Enqueue them to be re-run.
seen[neighbor] = shortest + incremental
queue = append(queue, neighbor)
}
}
}
}
return seen[winner]
}