-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
254 lines (220 loc) · 4.28 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
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
package main
import (
"fmt"
lib "github.com/teivah/advent-of-code"
)
func fs1(depth, col, row int) int {
cave := toCave(depth, col, row)
fmt.Println(cave)
return cave.riskLevel()
}
type Cave struct {
board [][]Unit
col int
row int
targetCol int
targetRow int
}
func (c *Cave) riskLevel() int {
risk := 0
for row := 0; row < c.targetRow+1; row++ {
for col := 0; col < c.targetCol+1; col++ {
switch c.board[row][col].unitType {
case wet:
risk++
case narrow:
risk += 2
}
}
}
return risk
}
func (c *Cave) String() string {
s := ""
for row := 0; row < c.row; row++ {
for col := 0; col < c.col; col++ {
if row == 0 && col == 0 {
s += "M"
continue
}
if row == c.targetRow && col == c.targetCol {
s += "T"
continue
}
switch c.board[row][col].unitType {
case rocky:
s += "."
case wet:
s += "="
case narrow:
s += "|"
}
}
s += "\n"
}
return s
}
func toCave(depth, targetCol, targetRow int) *Cave {
const (
bufferRow = 100
bufferCol = 100
)
origCol := targetCol
origRow := targetRow
targetCol += bufferCol
targetRow += bufferRow
board := make([][]Unit, targetRow)
for y := 0; y < targetRow; y++ {
board[y] = make([]Unit, 0, targetCol+1)
for x := 0; x < targetCol+1; x++ {
gi := 0
if y == 0 && x == 0 {
gi = 0
} else if y == origRow && x == origCol {
gi = 0
} else if y == 0 {
gi = x * 16807
} else if x == 0 {
gi = y * 48271
} else {
gi = board[y][x-1].erosionLevel * board[y-1][x].erosionLevel
}
el := (gi + depth) % 20183
var ut UnitType
switch el % 3 {
case 0:
ut = rocky
case 1:
ut = wet
case 2:
ut = narrow
}
unit := Unit{
unitType: ut,
erosionLevel: el,
geologicIndex: gi,
}
board[y] = append(board[y], unit)
}
}
return &Cave{
board: board,
col: targetCol,
row: targetRow,
targetCol: origCol,
targetRow: origRow,
}
}
type Unit struct {
unitType UnitType
erosionLevel int
geologicIndex int
}
type UnitType int
const (
rocky UnitType = iota
wet
narrow
)
type ToolType int
const (
torch ToolType = iota
gear
none
)
func (c *Cave) isAllowed(pos Position, tool ToolType) bool {
ut := c.board[pos.row][pos.col].unitType
switch ut {
case rocky:
return tool == gear || tool == torch
case wet:
return tool == gear || tool == none
case narrow:
return tool == torch || tool == none
}
panic(ut)
}
func (c *Cave) getTools(pos Position) []ToolType {
ut := c.board[pos.row][pos.col].unitType
switch ut {
case rocky:
return []ToolType{gear, torch}
case wet:
return []ToolType{gear, none}
case narrow:
return []ToolType{torch, none}
}
panic(ut)
}
func (c *Cave) bfs() int {
type State struct {
minute int
pos Position
tool ToolType
}
q := []State{{}}
target := Position{c.targetRow, c.targetCol}
miner := lib.NewMiner()
visited := make(map[ToolType]map[Position]int)
for len(q) != 0 {
state := q[0]
q = q[1:]
if state.pos == target {
if state.tool == torch {
miner.Add(state.minute)
} else {
// Switching tool
miner.Add(state.minute + 7)
}
continue
}
if m, exists := visited[state.tool]; exists {
if minute, exists := m[state.pos]; exists {
if minute <= state.minute {
continue
}
}
} else {
visited[state.tool] = make(map[Position]int)
}
visited[state.tool][state.pos] = state.minute
add := func(next Position) {
if next.isValid(c) {
tools := c.getTools(next)
for _, tool := range tools {
if c.isAllowed(state.pos, tool) {
if tool == state.tool {
q = append(q, State{minute: state.minute + 1, pos: next, tool: tool})
} else {
q = append(q, State{minute: state.minute + 7, pos: state.pos, tool: tool})
}
}
}
}
}
add(state.pos.delta(-1, 0))
add(state.pos.delta(1, 0))
add(state.pos.delta(0, -1))
add(state.pos.delta(0, 1))
}
return miner.Get()
}
type Position struct {
row int
col int
}
func (p Position) isValid(c *Cave) bool {
if p.row < 0 || p.col < 0 || p.row >= c.row || p.col >= c.col {
return false
}
return true
}
func (p Position) delta(row, col int) Position {
p.row += row
p.col += col
return p
}
func fs2(depth, col, row int) int {
cave := toCave(depth, col, row)
return cave.bfs()
}