aoc2024/day-15/internal/two/queue.go

48 lines
699 B
Go

package two
import (
"errors"
"fmt"
)
type queue struct {
elements []point
}
func newQueue() *queue {
return &queue{}
}
func (q *queue) Enqueue(elem point) {
q.elements = append(q.elements, elem)
}
func (q *queue) Dequeue() interface{} {
if q.IsEmpty() {
fmt.Println("UnderFlow")
return point{}
}
element := q.elements[0]
if q.Len() == 1 {
q.elements = nil
return element
}
q.elements = q.elements[1:]
return element
}
func (q *queue) Len() int {
return len(q.elements)
}
func (q *queue) IsEmpty() bool {
return len(q.elements) == 0
}
func (q *queue) Peek() (point, error) {
if q.IsEmpty() {
return point{}, errors.New("empty queue")
}
return q.elements[0], nil
}