@@ -91,9 +91,9 @@ type cheat struct {
91
91
end util.Point
92
92
}
93
93
94
- func findCheats (course [][]byte , moves []util.Point , startIdx int , minSaving int , debug bool ) [] cheat {
94
+ func findCheats (course [][]byte , moves []util.Point , startIdx int , minSaving int , debug bool ) map [ cheat ] int {
95
95
var s = moves [startIdx ]
96
- var cheats [] cheat
96
+ var cheats = make ( map [ cheat ] int )
97
97
var visited = make (map [util.Point ]int )
98
98
var recents = make (map [util.Point ]int )
99
99
recents [s ] = 0
@@ -111,9 +111,9 @@ func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int
111
111
if _ , ok := visited [p2 ]; ok {
112
112
continue
113
113
}
114
+ visited [p2 ] = cost + 1
115
+ found [p2 ] = cost + 1
114
116
if course [p2.Y ][p2.X ] == WALL {
115
- visited [p2 ] = cost + 1
116
- found [p2 ] = cost + 1
117
117
continue
118
118
}
119
119
var endIdx = findMoveIdx (moves , p2 )
@@ -124,7 +124,7 @@ func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int
124
124
if debug {
125
125
fmt .Printf ("%d picoseconds saved by cheat: %d,%d -> %d,%d\n " , saving , s .Y , s .X , p2 .Y , p2 .X )
126
126
}
127
- cheats = append ( cheats , cheat {start : s , end : p2 })
127
+ cheats [ cheat {start : s , end : p2 }] = saving
128
128
}
129
129
}
130
130
recents = found
@@ -155,15 +155,29 @@ func main() {
155
155
var time = len (moves ) - 1
156
156
fmt .Printf ("course takes %d picoseconds\n " , time )
157
157
158
- var cheats = make (map [cheat ]util. Void )
158
+ var cheats = make (map [cheat ]int )
159
159
for startIdx := range moves {
160
- for _ , c := range findCheats (course , moves , startIdx , * minSaving , * debug ) {
161
- cheats [c ] = util.Void {}
160
+ for c , s := range findCheats (course , moves , startIdx , * minSaving , * debug ) {
161
+ if so , ok := cheats [c ]; ! ok || so < s {
162
+ cheats [c ] = s
163
+ }
162
164
}
163
165
if startIdx % 100 == 0 {
164
166
fmt .Printf ("searched cheats for %d/%d moves\n " , startIdx + 1 , len (moves ))
165
167
}
166
168
}
167
169
170
+ var savingCheats = make (map [int ]int )
171
+ for _ , s := range cheats {
172
+ if n , ok := savingCheats [s ]; ok {
173
+ savingCheats [s ] = n + 1
174
+ } else {
175
+ savingCheats [s ] = 1
176
+ }
177
+ }
178
+ for s , n := range savingCheats {
179
+ fmt .Printf ("found %d cheats that save %d picoseconds\n " , n , s )
180
+ }
181
+
168
182
fmt .Printf ("found %d unique cheats saving >= %d picoseconds" , len (cheats ), * minSaving )
169
183
}
0 commit comments