Skip to content

Commit

Permalink
day20: ignore walls in pathsearch
Browse files Browse the repository at this point in the history
this was the missing piece, solution is now right
  • Loading branch information
fxnn committed Dec 22, 2024
1 parent c87e403 commit 03464e5
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions day20/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ type cheat struct {
end util.Point
}

func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int, debug bool) []cheat {
func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int, debug bool) map[cheat]int {
var s = moves[startIdx]
var cheats []cheat
var cheats = make(map[cheat]int)
var visited = make(map[util.Point]int)
var recents = make(map[util.Point]int)
recents[s] = 0
Expand All @@ -111,9 +111,9 @@ func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int
if _, ok := visited[p2]; ok {
continue
}
visited[p2] = cost + 1
found[p2] = cost + 1
if course[p2.Y][p2.X] == WALL {
visited[p2] = cost + 1
found[p2] = cost + 1
continue
}
var endIdx = findMoveIdx(moves, p2)
Expand All @@ -124,7 +124,7 @@ func findCheats(course [][]byte, moves []util.Point, startIdx int, minSaving int
if debug {
fmt.Printf("%d picoseconds saved by cheat: %d,%d -> %d,%d\n", saving, s.Y, s.X, p2.Y, p2.X)
}
cheats = append(cheats, cheat{start: s, end: p2})
cheats[cheat{start: s, end: p2}] = saving
}
}
recents = found
Expand Down Expand Up @@ -155,15 +155,29 @@ func main() {
var time = len(moves) - 1
fmt.Printf("course takes %d picoseconds\n", time)

var cheats = make(map[cheat]util.Void)
var cheats = make(map[cheat]int)
for startIdx := range moves {
for _, c := range findCheats(course, moves, startIdx, *minSaving, *debug) {
cheats[c] = util.Void{}
for c, s := range findCheats(course, moves, startIdx, *minSaving, *debug) {
if so, ok := cheats[c]; !ok || so < s {
cheats[c] = s
}
}
if startIdx%100 == 0 {
fmt.Printf("searched cheats for %d/%d moves\n", startIdx+1, len(moves))
}
}

var savingCheats = make(map[int]int)
for _, s := range cheats {
if n, ok := savingCheats[s]; ok {
savingCheats[s] = n + 1
} else {
savingCheats[s] = 1
}
}
for s, n := range savingCheats {
fmt.Printf("found %d cheats that save %d picoseconds\n", n, s)
}

fmt.Printf("found %d unique cheats saving >= %d picoseconds", len(cheats), *minSaving)
}

0 comments on commit 03464e5

Please sign in to comment.