package aoc type stack[T any] []T func NewStack[T any](items ...T) *stack[T] { s := make(stack[T], len(items)) copy(s, items) return &s } // Push adds an element to the top of the stack. func (s *stack[T]) Push(v T) { *s = append(*s, v) } // Pop removes the top element from the stack and returns it. // If the stack is empty, it panics. func (s *stack[T]) Pop() T { if s.IsEmpty() { var zero T return zero } v := (*s)[len(*s)-1] *s = (*s)[:len(*s)-1] return v } // Peek returns the top element from the stack without removing it. // If the stack is empty, it panics. func (s *stack[T]) Peek() T { if s.IsEmpty() { var zero T return zero } return (*s)[len(*s)-1] } // Len returns the number of elements in the stack. func (s *stack[T]) Len() int { return len(*s) } // IsEmpty returns true if the stack is empty and false otherwise. func (s *stack[T]) IsEmpty() bool { return s == nil || len(*s) == 0 } // Clear removes all elements from the stack, returning it to its initial state. func (s *stack[T]) Clear() { *s = (*s)[:0] } func (s *stack[T]) Pull() T { if s.IsEmpty() { var zero T return zero } v := (*s)[0] *s = (*s)[1:] return v }