2018 Day 22 Complete
This commit is contained in:
48
2018/day22/priorityqueue.go
Normal file
48
2018/day22/priorityqueue.go
Normal file
@@ -0,0 +1,48 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user