-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.go
216 lines (183 loc) · 4.99 KB
/
day24.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
_ "embed"
"math"
"strings"
"github.com/oleiade/lane/v2"
)
//go:embed input/day24
var d24 string
type Blizzard [4][][]bool
func newBlizzard(minX, maxX, minY, maxY int) *Blizzard {
var b Blizzard
lenX := maxX - minX - 1
lenY := maxY - minY - 1
b[0] = make([][]bool, lenY)
b[1] = make([][]bool, lenY)
b[2] = make([][]bool, lenX)
b[3] = make([][]bool, lenX)
for j := 0; j < lenY; j++ {
b[0][j] = make([]bool, lenX)
b[1][j] = make([]bool, lenX)
}
for i := 0; i < lenX; i++ {
b[2][i] = make([]bool, lenY)
b[3][i] = make([]bool, lenY)
}
return &b
}
func (b *Blizzard) add(x, y int, dir byte) {
switch dir {
case '^':
b[0][y][x] = true
case 'v':
b[1][y][x] = true
case '<':
b[2][x][y] = true
case '>':
b[3][x][y] = true
}
}
func clone(s [][]bool) [][]bool {
dup := make([][]bool, len(s))
for i := range s {
dup[i] = make([]bool, len(s[i]))
copy(dup[i], s[i])
}
return dup
}
func (b *Blizzard) step() *Blizzard {
newB := *b
for i := 0; i < 4; i++ {
newB[i] = clone(b[i])
}
newB[0] = append(newB[0][1:], newB[0][0])
newB[1] = append(newB[1][len(newB[1])-1:], newB[1][:len(newB[1])-1]...)
newB[2] = append(newB[2][1:], newB[2][0])
newB[3] = append(newB[3][len(newB[3])-1:], newB[3][:len(newB[3])-1]...)
return &newB
}
type State struct {
pos point
minutes int
}
func neighbors(s State, blizzards []Blizzard) []State {
i, j := s.pos.x, s.pos.y
explore := []point{{i, j}, {i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}}
var res []State
newTime := s.minutes + 1
b := blizzards[newTime%len(blizzards)]
exit1 := point{0, -1}
exit2 := point{x: len(b[2]) - 1, y: len(b[0]) - 1 + 1}
for _, p := range explore {
if p == exit1 || p == exit2 {
res = append(res, State{p, newTime})
continue
}
if p.x < 0 || p.y < 0 || p.x >= len(b[0][0]) || p.y >= len(b[0]) {
continue
}
if b[0][p.y][p.x] || b[1][p.y][p.x] || b[2][p.x][p.y] || b[3][p.x][p.y] {
continue
}
res = append(res, State{p, newTime})
}
return res
}
func gdc(a, b int) int {
for b != 0 {
a, b = b, a%b
}
return a
}
func lcm(a, b int, integers ...int) int {
res := a * b / gdc(a, b)
for i := 0; i < len(integers); i++ {
res = lcm(res, integers[i])
}
return res
}
func bounds(grid map[point]uint8) (minX, maxX, minY, maxY int) {
minX, minY, maxX, maxY = math.MaxInt, math.MaxInt, math.MinInt, math.MinInt
for p := range grid {
minX = Min(p.x, minX)
minY = Min(p.y, minY)
maxX = Max(p.x, maxX)
maxY = Max(p.y, maxY)
}
return minX, maxX, minY, maxY
}
type (
heuristicFunction[T comparable] func(from T) int
goalFunction[T comparable] func(from T) bool
costFunction[T comparable] func(from, to T) int
)
func aStart(start State, goal goalFunction[State], cost costFunction[State], heuristic heuristicFunction[State], blizzards []Blizzard) (path []State, distance int) {
queue := lane.NewMinPriorityQueue[State, int]()
queue.Push(start, 0)
cameFrom := map[State]State{start: start}
costSoFar := map[State]int{start: 0}
for {
if queue.Size() == 0 {
// There's no path, return found false.
return
}
current, _, _ := queue.Pop()
if goal(current) {
// Found a path to the goal.
var path []State
curr := current
for curr != start {
path = append(path, curr)
curr = cameFrom[curr]
}
return path, costSoFar[current]
}
for _, neighbor := range neighbors(current, blizzards) {
newCost := costSoFar[current] + cost(current, neighbor)
if _, ok := costSoFar[neighbor]; !ok || newCost < costSoFar[neighbor] {
costSoFar[neighbor] = newCost
priority := newCost + heuristic(neighbor)
queue.Push(neighbor, priority)
cameFrom[neighbor] = current
}
}
}
}
func day24(input string) (int, int) {
lines := strings.Split(strings.TrimSuffix(input, "\n"), "\n")
grid := make(map[point]uint8)
for j, l := range lines {
for i, c := range l {
grid[point{i, j}] = uint8(c)
}
}
minX, maxX, minY, maxY := bounds(grid)
b := newBlizzard(minX, maxX, minY, maxY)
for p, v := range grid {
if v == '^' || v == 'v' || v == '<' || v == '>' {
b.add(p.x-1, p.y-1, v)
}
}
blizzards := []Blizzard{*b}
lenX := maxX - minX - 1
lenY := maxY - minY - 1
lcm := lcm(lenX, lenY)
for i := 1; i < lcm; i++ {
b = b.step()
blizzards = append(blizzards, *b)
}
start := point{0, -1}
goal := point{maxX - minX - 2, maxY - minY - 1}
costF := func(from, to State) int { return 1 }
goalF := func(s State) bool { return s.pos == goal }
heuristicF := func(s State) int { return manhattanDistance(s.pos, goal) }
_, cost := aStart(State{start, 0}, goalF, costF, heuristicF, blizzards)
goal1 := func(s State) bool { return s.pos == goal }
goal2 := func(s State) bool { return s.pos == start }
heuristic2 := func(s State) int { return manhattanDistance(s.pos, start) }
_, cost1 := aStart(State{start, 0}, goal1, costF, heuristicF, blizzards)
_, cost2 := aStart(State{goal, cost1}, goal2, costF, heuristic2, blizzards)
_, cost3 := aStart(State{start, cost1 + cost2}, goal1, costF, heuristicF, blizzards)
return cost, cost1 + cost2 + cost3
}