@@ -91,9 +91,9 @@ type cheat struct {
9191 end util.Point
9292}
9393
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 {
9595 var s = moves [startIdx ]
96- var cheats [] cheat
96+ var cheats = make ( map [ cheat ] int )
9797 var visited = make (map [util.Point ]int )
9898 var recents = make (map [util.Point ]int )
9999 recents [s ] = 0
@@ -111,9 +111,9 @@ func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int
111111 if _ , ok := visited [p2 ]; ok {
112112 continue
113113 }
114+ visited [p2 ] = cost + 1
115+ found [p2 ] = cost + 1
114116 if course [p2.Y ][p2.X ] == WALL {
115- visited [p2 ] = cost + 1
116- found [p2 ] = cost + 1
117117 continue
118118 }
119119 var endIdx = findMoveIdx (moves , p2 )
@@ -124,7 +124,7 @@ func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int
124124 if debug {
125125 fmt .Printf ("%d picoseconds saved by cheat: %d,%d -> %d,%d\n " , saving , s .Y , s .X , p2 .Y , p2 .X )
126126 }
127- cheats = append ( cheats , cheat {start : s , end : p2 })
127+ cheats [ cheat {start : s , end : p2 }] = saving
128128 }
129129 }
130130 recents = found
@@ -155,15 +155,29 @@ func main() {
155155 var time = len (moves ) - 1
156156 fmt .Printf ("course takes %d picoseconds\n " , time )
157157
158- var cheats = make (map [cheat ]util. Void )
158+ var cheats = make (map [cheat ]int )
159159 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+ }
162164 }
163165 if startIdx % 100 == 0 {
164166 fmt .Printf ("searched cheats for %d/%d moves\n " , startIdx + 1 , len (moves ))
165167 }
166168 }
167169
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+
168182 fmt .Printf ("found %d unique cheats saving >= %d picoseconds" , len (cheats ), * minSaving )
169183}
0 commit comments