-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
272 lines (242 loc) · 5.39 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/teivah/go-aoc"
)
type cell uint8
const (
track cell = iota
wall
)
func fs1(input io.Reader) int {
var start, end aoc.Position
board := aoc.NewBoardFromReader(input, func(row, col int, r rune) cell {
switch r {
default:
panic(r)
case '.':
return track
case '#':
return wall
case 'S':
start = aoc.NewPosition(row, col)
return track
case 'E':
end = aoc.NewPosition(row, col)
return track
}
})
shortest := shortestPath(board, start, end)
//all := make(map[aoc.Position]int)
//for pos, c := range board.Positions {
// if c != track {
// continue
// }
// all[pos] = shortestPathWithAll(board, pos, end)
//}
//offload(all)
all := load()
return countCheats(all, board, start, end, shortest)
}
func load() map[aoc.Position]int {
f, err := os.Open("all.txt")
if err != nil {
panic(err)
}
lines := aoc.ReaderToStrings(f)
m := make(map[aoc.Position]int)
for _, line := range lines {
var a, b, value int
_, err := fmt.Sscanf(line, "%d,%d=%d", &a, &b, &value)
if err != nil {
panic(err)
}
m[aoc.NewPosition(a, b)] = value
}
return m
}
func offload(all map[aoc.Position]int) {
var lines []string
for pos, v := range all {
lines = append(lines, fmt.Sprintf("%d,%d=%d", pos.Row, pos.Col, v))
}
file, err := os.Create("all.txt")
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.WriteString(strings.Join(lines, "\n"))
if err != nil {
panic(err)
}
}
func shortestPathWithAll(board aoc.Board[cell], start, end aoc.Position) int {
q := []state1{
{pos: start},
}
visited := make(map[aoc.Position]bool)
for len(q) != 0 {
s := q[0]
q = q[1:]
if s.pos == end {
return s.moves
}
if visited[s.pos] {
continue
}
visited[s.pos] = true
if !board.Contains(s.pos) {
continue
}
if board.Get(s.pos) == wall {
continue
}
moves := s.moves + 1
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Up, 1)})
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Down, 1)})
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Left, 1)})
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Right, 1)})
}
return -1
}
type state1 struct {
moves int
pos aoc.Position
}
func shortestPath(board aoc.Board[cell], start, end aoc.Position) int {
q := []state1{
{pos: start},
}
visited := make(map[aoc.Position]bool)
for len(q) != 0 {
s := q[0]
q = q[1:]
if s.pos == end {
return s.moves
}
if visited[s.pos] {
continue
}
visited[s.pos] = true
if !board.Contains(s.pos) {
continue
}
if board.Get(s.pos) == wall {
continue
}
moves := s.moves + 1
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Up, 1)})
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Down, 1)})
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Left, 1)})
q = append(q, state1{moves: moves, pos: s.pos.Move(aoc.Right, 1)})
}
return -1
}
type state2 struct {
moves int
pos aoc.Position
cheated bool
cheatedPos aoc.Position
}
type key struct {
pos aoc.Position
cheated bool
cheat aoc.Position
}
func countCheats(all map[aoc.Position]int, board aoc.Board[cell], start, end aoc.Position, shortest int) int {
q := []state2{
{pos: start},
}
visited := make(map[key]bool)
res := 0
for len(q) != 0 {
s := q[0]
q = q[1:]
if s.pos == end {
if s.cheated {
saved := shortest - s.moves
if saved >= 100 {
res++
}
}
continue
}
if s.moves >= shortest {
continue
}
k := key{pos: s.pos, cheated: s.cheated, cheat: s.cheatedPos}
if visited[k] {
continue
}
visited[k] = true
if !board.Contains(s.pos) {
continue
}
if board.Get(s.pos) == wall {
if s.cheated {
continue
}
s.cheated = true
s.cheatedPos = s.pos
} else {
if s.cheated {
solution := s.moves + all[s.pos]
saved := shortest - solution
if saved >= 100 {
res++
}
continue
}
}
moves := s.moves + 1
q = append(q, state2{cheated: s.cheated, cheatedPos: s.cheatedPos, moves: moves, pos: s.pos.Move(aoc.Up, 1)})
q = append(q, state2{cheated: s.cheated, cheatedPos: s.cheatedPos, moves: moves, pos: s.pos.Move(aoc.Down, 1)})
q = append(q, state2{cheated: s.cheated, cheatedPos: s.cheatedPos, moves: moves, pos: s.pos.Move(aoc.Left, 1)})
q = append(q, state2{cheated: s.cheated, cheatedPos: s.cheatedPos, moves: moves, pos: s.pos.Move(aoc.Right, 1)})
}
return res
}
func fs2(input io.Reader, minSaves int) int {
var start, end aoc.Position
board := aoc.NewBoardFromReader(input, func(row, col int, r rune) cell {
switch r {
default:
panic(r)
case '.':
return track
case '#':
return wall
case 'S':
start = aoc.NewPosition(row, col)
return track
case 'E':
end = aoc.NewPosition(row, col)
return track
}
})
shortest := shortestPath(board, start, end)
//all := make(map[aoc.Position]int)
//for pos, c := range board.Positions {
// if c != track {
// continue
// }
// all[pos] = shortestPathWithAll(board, pos, end)
//}
_ = shortest
//offload(all)
all := load()
cheats := make(map[aoc.Pair[aoc.Position, aoc.Position]]int)
savings := 20
for pos1, distance1 := range all {
for pos2, distance2 := range all {
if pos1.Manhattan(pos2) <= savings && distance2-distance1-pos1.Manhattan(pos2) >= minSaves {
k := aoc.Pair[aoc.Position, aoc.Position]{pos1, pos2}
cheats[k] = distance2 - distance1
}
}
}
return len(cheats)
}