Documentation
¶
Index ¶
- Constants
- Variables
- func Abs(a int) int
- func Atoi(s string) int
- func CRT(cycles []Cycle) int
- func Check(err error)
- func Cmp(a, b int) int
- func ContainsInt(x int, entries []int) bool
- func ContainsString(x string, entries []string) bool
- func CountInts(entries []int) map[int]int
- func CountStrings(entries []string) map[string]int
- func Cut(s string, separator string) (left, right string)
- func FirstAsciiNr(s string) int
- func GCD(a, b int) int
- func LCM(values []int) int
- func Max(numbers []int) int
- func Min(numbers []int) int
- func MinMax(a, b int) (int, int)
- func MinMaxInts(numbers []int) (int, int)
- func ReadLinesFromFile(path string) []string
- func ReadNumbersFromFile(path string) []int
- func ReadRawLinesFromFile(path string) []string
- func ReverseSlice[T any](s []T)
- func Sign(a int) int
- func Sort(a []int)
- func SplitToChars(line string) []string
- func SplitToInts(line string) []int
- func SplitToRunes(line string) []rune
- func SplitWithSpace(line string) []string
- func SplitWithTrim(line, splitPattern string) []string
- func Triangle(nr int) int
- func Trim(line string) string
- func TrimTrailingNewline(lines []string) []string
- type CharGrid
- type Command
- type Cycle
- type DigitGrid
- type Grid2D
- func (g *Grid2D[T]) At(row, col int) T
- func (g Grid2D[T]) AtBorder(row, col int) bool
- func (g *Grid2D[T]) Find(value T) (row, col int, ok bool)
- func (g Grid2D[T]) Get(row, col int) T
- func (g Grid2D[T]) InBounds(row, col int) bool
- func (g *Grid2D[T]) Set(val T, row, col int)
- func (g *Grid2D[T]) SetAll(value T)
- type Grid3D
- type Heap
- type Pos2D
- type RuneGrid
- type Set
- func (s Set[K]) Add(elem K)
- func (s Set[K]) Clone() Set[K]
- func (s Set[K]) Contains(elem K) bool
- func (s Set[K]) Extend(other Set[K])
- func (s Set[K]) GetOne() K
- func (s Set[K]) Intersect(other Set[K])
- func (s Set[K]) Remove(elem K)
- func (s Set[K]) Size() int
- func (s Set[K]) Subtract(other Set[K])
- func (s Set[K]) Values() []K
- type Stack
Examples ¶
Constants ¶
const (
MaxInt = 9223372036854775807
)
Variables ¶
var Dirs2D = []Pos2D{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
Dirs2D provides all right-angle directions down, right, up, left
var Dirs2DAll = []Pos2D{{1, 0}, {0, 1}, {-1, 0}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
Dirs2DAll provides all 8 directions
var NeighborsAll [][3]int
var NeighborsStraight = [][3]int{{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}}
Functions ¶
func CRT ¶
CRT is a function that takes a slice of cycles and returns the smallest number that satisfies all the cycles according to the Chinese Remainder Theorem.
func ContainsInt ¶
func ContainsString ¶
func CountStrings ¶
func FirstAsciiNr ¶
FirstAsciiNr return ascii number of first character in string. Panics if value > 127.
func MinMaxInts ¶
func ReadLinesFromFile ¶
ReadLinesFromFile reads lines from a file.
func ReadNumbersFromFile ¶
ReadNumbersFromFile reads one integer from each line in file.
func ReadRawLinesFromFile ¶
ReadRawLinesFromFile reads lines from a file.
func ReverseSlice ¶
func ReverseSlice[T any](s []T)
func SplitToInts ¶
SplitToInts finds all ints in line (including sign).
func SplitWithSpace ¶
SplitWithSpace splits a line with space as separator.
func SplitWithTrim ¶
func TrimTrailingNewline ¶
TrimTrailingNewline removes trailing newline
Types ¶
type Command ¶
func ParseCommand ¶
ParseCommand parses a "verb value" from a line.
type Cycle ¶
type Cycle struct {
Offset, Period int
}
Cycle is a struct that represents a cycle with an offset and a period.
type Grid2D ¶
type Grid2D[T comparable] struct { Grid [][]T Width int Height int }
func CreateGrid2D ¶
func CreateGrid2D[T comparable](width, height int) Grid2D[T]
type Heap ¶
type Heap[E any] struct { // contains filtered or unexported fields }
A Heap is a min-heap backed by a slice. This code comes from https://github.com/golang/go/issues/47632
Example ¶
pq := NewHeap[*Item]((*Item).Compare) pq.SetIndex((*Item).setIndex) pq.Push(&Item{value: "banana", priority: 3}) pq.Push(&Item{value: "apple", priority: 2}) pq.Push(&Item{value: "pear", priority: 4}) for pq.Len() > 0 { item := pq.Pop() fmt.Printf("%d:%s ", item.priority, item.value) } fmt.Println()
Output: 4:pear 3:banana 2:apple
func (*Heap[E]) Fix ¶
Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling h.Remove(i) followed by a Push of the new value. The complexity is O(log n) where n = h.Len(). The index for use with Fix is recorded using the function passed to SetIndex.
func (*Heap[E]) Peek ¶
func (h *Heap[E]) Peek() E
Peek returns the minimum element (according to the cmp function) in the heap. Peek panics if the heap is empty. The complexity is O(1).
func (*Heap[E]) Pop ¶
func (h *Heap[E]) Pop() E
Pop removes and returns the minimum element (according to the cmp function) from the heap. Pop panics if the heap is empty. The complexity is O(log n) where n = h.Len().
func (*Heap[E]) Push ¶
func (h *Heap[E]) Push(elem E)
Push pushes an element onto the heap. The complexity is O(log n) where n = h.Len().
func (*Heap[E]) Remove ¶
Remove removes and returns the element at index i from the heap. The complexity is O(log n) where n = h.Len(). The index for use with Remove is recorded using the function passed to SetIndex.
func (*Heap[E]) SetIndex ¶
SetIndex specifies an optional function to be called when updating the position of any heap element within the slice, including during the element's initial Push.
SetIndex must be called at most once, before any calls to Push.
When an element is removed from the heap by Pop or Remove, the index function is called with the invalid index -1 to signify that the element is no longer within the slice.
type RuneGrid ¶
func CreateRuneGridFromLines ¶
type Set ¶
type Set[K comparable] map[K]struct{}
Set - mathematical set with operations Empty struct requires zero bytes so is more efficient than bool
func (Set[K]) Remove ¶
func (s Set[K]) Remove(elem K)
Remove - remove elem from set (does not need to be in set)