mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
32 lines
440 B
Go
32 lines
440 B
Go
|
package one
|
||
|
|
||
|
type minHeap []move
|
||
|
|
||
|
func newHeap() *minHeap {
|
||
|
return &minHeap{}
|
||
|
}
|
||
|
|
||
|
func (h minHeap) Len() int {
|
||
|
return len(h)
|
||
|
}
|
||
|
|
||
|
func (h minHeap) Less(i, j int) bool {
|
||
|
return h[i].cost < h[j].cost
|
||
|
}
|
||
|
|
||
|
func (h minHeap) Swap(i, j int) {
|
||
|
h[i], h[j] = h[j], h[i]
|
||
|
}
|
||
|
|
||
|
func (h *minHeap) Push(x interface{}) {
|
||
|
*h = append(*h, x.(move))
|
||
|
}
|
||
|
|
||
|
func (h *minHeap) Pop() interface{} {
|
||
|
old := *h
|
||
|
n := len(old)
|
||
|
x := old[n-1]
|
||
|
*h = old[:n-1]
|
||
|
return x
|
||
|
}
|