adventofcode/2018/day22/priorityqueue.go

49 lines
726 B
Go
Raw Normal View History

2019-11-07 17:53:26 +00:00
package main
type Item struct {
pos coord
equip int
time int
index int
}
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int {
return len(pq)
}
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].time < pq[j].time
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
item := x.(*Item)
item.index = len(*pq)
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[0 : n-1]
return item
}
func (i Item) Compare(o Item) int {
switch {
case i.time > o.time:
return 1
case i.time < o.time:
return -1
}
return 0
}