-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrucible.go
276 lines (235 loc) · 4.82 KB
/
crucible.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package day17
import (
"errors"
"fmt"
"github.com/user3690/advent-of-code-in-go/util"
"log"
"strconv"
)
type heatLoss = int
type direction uint8
const (
up direction = iota
right
down
left
)
func (dir direction) TurnLeftOrRight(turn direction) direction {
if turn != left && turn != right {
panic("should be left or right")
}
switch dir {
case up:
return turn
case right:
switch turn {
case left:
return up
case right:
return down
}
case down:
switch turn {
case left:
return right
case right:
return left
}
case left:
switch turn {
case left:
return down
case right:
return up
}
}
panic("not handled")
}
type cityBlock struct {
state state
heatLoss heatLoss
}
func (cb cityBlock) move(dir direction) point {
switch dir {
case up:
return point{
x: cb.state.pos.poi.x - 1,
y: cb.state.pos.poi.y,
}
case right:
return point{
x: cb.state.pos.poi.x,
y: cb.state.pos.poi.y + 1,
}
case down:
return point{
x: cb.state.pos.poi.x + 1,
y: cb.state.pos.poi.y,
}
case left:
return point{
x: cb.state.pos.poi.x,
y: cb.state.pos.poi.y - 1,
}
}
panic("panic cb turn")
}
func (cb cityBlock) turn(dir direction) direction {
return cb.state.pos.dir.TurnLeftOrRight(dir)
}
func (cb cityBlock) goStraightOn() direction {
return cb.state.pos.dir
}
type state struct {
pos position
straight uint8
}
type position struct {
poi point
dir direction
}
type point struct {
x int16 // row
y int16 // col
}
func BothParts() {
var (
lines []string
cityBlocks map[point]cityBlock
rows, cols, leastHeatLoss int
err error
)
lines, err = util.ReadFileInLines("./2023/day17/input_test2.txt")
if err != nil {
log.Fatal(err)
}
cityBlocks, rows, cols, err = createCityBlocks(lines)
if err != nil {
log.Fatal(err)
}
leastHeatLoss, err = findRouteWithLeastHeatLoss(cityBlocks, rows, cols, 0, 3)
if err != nil {
log.Fatal(err)
}
fmt.Println(leastHeatLoss)
leastHeatLoss, err = findRouteWithLeastHeatLoss(cityBlocks, rows, cols, 4, 10)
if err != nil {
log.Fatal(err)
}
fmt.Println(leastHeatLoss)
}
func createCityBlocks(
lines []string,
) (
cityBlocks map[point]cityBlock,
rows int,
cols int,
err error,
) {
var number int
rows = len(lines)
cols = len(lines[0])
cityBlocks = make(map[point]cityBlock, rows*cols)
for i, line := range lines {
for j, char := range line {
newPoint := point{
x: int16(i),
y: int16(j),
}
number, err = strconv.Atoi(string(char))
if err != nil {
return nil, 0, 0, err
}
cityBlocks[newPoint] = cityBlock{
state: state{
pos: position{
poi: newPoint,
dir: 0,
},
straight: 0,
},
heatLoss: number,
}
}
}
return cityBlocks, rows, cols, err
}
func findRouteWithLeastHeatLoss(
cityBlocks map[point]cityBlock,
rows int,
cols int,
minStraight, maxStraight uint8,
) (int, error) {
targetBlock := cityBlocks[point{x: int16(rows - 1), y: int16(cols - 1)}]
priorityQueue := util.NewPriorityQueue(
func(itemA, itemB cityBlock) int {
return itemA.heatLoss - itemB.heatLoss
},
)
// first block below start block
priorityQueue.Push(cityBlock{
state: state{
pos: position{
poi: point{
x: 1,
y: 0,
},
dir: down,
},
straight: 1,
},
})
// first block to the right of start block
priorityQueue.Push(cityBlock{
state: state{
pos: position{
poi: point{
x: 0,
y: 1,
},
dir: right,
},
straight: 1,
},
})
visited := make(map[state]heatLoss)
for !priorityQueue.IsEmpty() {
item := priorityQueue.Pop()
curBlockPoint := item.state.pos.poi
if _, exists := cityBlocks[curBlockPoint]; !exists {
continue
}
totalHeatLoss := cityBlocks[curBlockPoint].heatLoss + item.heatLoss
if curBlockPoint == targetBlock.state.pos.poi {
return totalHeatLoss, nil
}
if heatLossVal, exists := visited[item.state]; exists {
if heatLossVal <= totalHeatLoss {
continue
}
}
visited[item.state] = totalHeatLoss
if item.state.straight >= minStraight {
newDirection := item.turn(right)
rightBlock := cityBlocks[item.move(newDirection)]
rightBlock.state.pos.dir = newDirection
rightBlock.state.straight = 1
rightBlock.heatLoss = totalHeatLoss
priorityQueue.Push(rightBlock)
newDirection = item.turn(left)
leftBlock := cityBlocks[item.move(newDirection)]
leftBlock.state.pos.dir = newDirection
leftBlock.state.straight = 1
leftBlock.heatLoss = totalHeatLoss
priorityQueue.Push(leftBlock)
}
if item.state.straight < maxStraight {
block := cityBlocks[item.move(item.goStraightOn())]
block.state.pos.dir = item.goStraightOn()
block.state.straight = item.state.straight + 1
block.heatLoss = totalHeatLoss
priorityQueue.Push(block)
}
}
return 0, errors.New("no path found")
}