lib

package
v0.0.0-...-2841176 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2022 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AStar

func AStar[T AStarNode[T]](start T, target func(T) bool, h func(T) int) ([]T, bool)

func Abs

func Abs[T constraints.Integer | constraints.Float](v T) T

Abs returns the absolute value of v.

func Adjacent

func Adjacent[T any](diag bool, row, col int, grid [][]T) []T

Adjacent returns the adjacent items in a 2d grid of items. Diag controls whether diagonals are considered as adjacent.

func AsIs

func AsIs(line string) string

AsIs is a parse function helper that leaves the value as is. Useful with ParseLine.

func Atoi

func Atoi(s string) int

Atoi is a convenience wrapper on strconv.Atoi that panics if it fails.

func BFS

func BFS[T BFSNode[T]](start T, target func(T) bool) ([]T, bool)

BFS runs breadth-first-search from start until target returns true. T must be a type that 1. has a method to return adjacent nodes and 2. is comparable.

func Clone

func Clone[T any](items []T) []T

Clone returns a copy of items.

func Filter

func Filter[T any](items []T, filter func(T) bool) []T

Filter returns entries in items that the filter function returns true for.

func Int

func Int(line string) int

Int is like Ints except it returns the first int found. See Atoi to convert a string representation of a number into an int.

func Ints

func Ints(line string) []int

Ints returns all the integers in line.

func Keys

func Keys[T comparable, V any](items map[T]V) []T

Keys returns the keys of a map. It does not return the items in a consistent order.

func Map

func Map[T, V any](items []T, mapper func(T) V) []V

Map converts a slice of items of type T to type V using a mapping function.

func Max

func Max(values ...int) int

Max returns the largest value in values.

func MaxSlice

func MaxSlice[T constraints.Ordered](items []T) T

MaxSlice returns the max of items.

func Min

func Min[T constraints.Ordered](values ...T) T

Min returns the smallest value in values.

func MinSlice

func MinSlice[T constraints.Ordered](items []T) T

MinSlice returns the min of items.

func MulSlice

func MulSlice[T constraints.Integer | constraints.Float](items []T) T

MulSlice returns the multiplication of items.

func Must

func Must[T any](value T, err error) T

Must return value if err is non-nil and panics otherwise.

func NoPanic

func NoPanic(err error)

NoPanic panics if err is non-nil.

func ParseBytes

func ParseBytes(input string) []byte

ParseBytes is a parse function that returns the raw bytes read.

func ParseBytesFunc

func ParseBytesFunc[T any](parse func(input []byte) T) func(input string) T

ParseBytesFunc is a parse function that returns a parsed value of the raw bytes read.

func ParseChunks

func ParseChunks[T any](parse func(chunk string) T) func(input string) []T

ParseChunks is like ParseLine but parses lines delimited by two new lines, not one

func ParseChunksUnique

func ParseChunksUnique[T any](parsers ...func(chunk string, val *T)) func(input string) T

ParseChunksUnique is like ParseChunks but uses a unique parser for every chunk. It's different from all the other functions in that it passes in a T to the parse func to modify as needed. The intended use is for each parser to be able to return a different type safe type - this couldn't work with generics - so T is intended a container for the result of each parser.

func ParseGrid

func ParseGrid[T any](parse func(s byte) T) func(input string) [][]T

ParseGrid parses a grid of characters.

func ParseLine

func ParseLine[T any](parse func(line string) T) func(input string) []T

ParseLine is a parse function that splits parsing into one line at a time, returning a slice of items. It accepts a parse function to parse each line seen.

func ParseRegexp

func ParseRegexp[T any](reg *regexp.Regexp, parse func(parts []string) T) func(line string) T

ParseRegexp is a parse function helper that returns substring matches from a string. Useful with ParseLine. There should only be one match of reg in the string; others will be ignored.

func Permutations

func Permutations[T any](items []T) [][]T

Permutations returns all possible permutations of items. It uses https://en.wikipedia.org/wiki/Heap%27s_algorithm.

Note: Almost every time, there is a more efficient solution than generating every possible permutation of a list. I.e. there's a way to filter out potential options that are known not right.

func Pow

func Pow(num, pow int) int

Pow is like math.Pow but for integers.

func Remove

func Remove[T any](items []T, index int) []T

Remove returns a new slice that has the item at index removed from items.

func Reverse

func Reverse[T any](items []T) []T

Reverse returns a new slice which is items in reverse order.

func Subsets

func Subsets[T any](items []T) [][]T

Subsets returns all subsets of items by enumerating the 2**n-1 possible combinations, using the bits in the counter as whether to include an item or not.

func SumSlice

func SumSlice[T constraints.Integer | constraints.Float](items []T) T

SumSlice returns the sum of items.

func ToByteSlice

func ToByteSlice(line string) []byte

ToByteSlice is a parse function helper that turns the value to a byte slice. Useful with ParseLine.

func ToString

func ToString[T any](solve func(T) []byte) func(T) string

ToString is a solve function helper that converts a byte slice to a string.

func TrimSpace

func TrimSpace[T any](parse func(s string) T) func(input string) T

TrimSpace trims the input of leading/starting spaces before calling parse.

Types

type AStarNode

type AStarNode[T any] interface {
	comparable
	Adjacent() []T
}

type BFSNode

type BFSNode[T any] interface {
	comparable
	Adjacent() []T
}

type Heap

type Heap[T comparable] struct {
	Max bool
	// contains filtered or unexported fields
}

Heap implements a binary heap: https://en.wikipedia.org/wiki/Binary_heap. Items are inserted with a priority and can later be efficiently retrieved. The zero value is an empty min heap. By default, items are retrieved in increasing priority order. Set Max to true to act as a max heap.

func (*Heap[T]) Add

func (m *Heap[T]) Add(item T, priority int)

Add adds a new item to the heap with the associated priority.

func (*Heap[T]) Contains

func (m *Heap[T]) Contains(item T) bool

func (*Heap[T]) Empty

func (m *Heap[T]) Empty() bool

Empty returns whether the heap has no items in it.

func (*Heap[T]) Pop

func (m *Heap[T]) Pop() T

Pop returns the item with the lowest priority (if !max, highest otherwise), removing it from the heap.

type LinkedList

type LinkedList[T any] struct {
	Item T
	// contains filtered or unexported fields
}

LinkedList represents a doubly-linked list of items. It provides O(1) removal/addition of nodes at any position in the list, but does not provide random access to list items. The zero value is a list of 1 item that has the zero value of T.

func (*LinkedList[T]) Append

func (l *LinkedList[T]) Append(m *LinkedList[T])

Append sets m to be the node after l. If l is the end of the list, the result is l <-> m. If the current layout is l <-> n, the result is l <-> m <-> n.

func (*LinkedList[T]) Next

func (l *LinkedList[T]) Next() *LinkedList[T]

Next returns the node after l.

func (*LinkedList[T]) Prepend

func (l *LinkedList[T]) Prepend(m *LinkedList[T])

Prepend sets m to be the node before l. If l is the start of the list, the result is m <-> l. If the current list is n <-> l, the result is n <-> m <-> l.

func (*LinkedList[T]) Prev

func (l *LinkedList[T]) Prev() *LinkedList[T]

Prev returns the node before l.

func (*LinkedList[T]) Remove

func (l *LinkedList[T]) Remove()

Remove removes l from the linked list, making its previous/next nodes point to each other and l point to nothing.

type OrderedSet

type OrderedSet[T constraints.Ordered] struct {
	Set[T]
}

func (*OrderedSet[T]) Items

func (s *OrderedSet[T]) Items() []T

Items returns the items in the Set in sorted order.

type Pos

type Pos struct {
	Row, Col int
}

Pos represents a two-dimensional position.

func AdjacentPos

func AdjacentPos(diag bool, row, col int) []Pos

AdjacentPos returns the adjacent positions in a 2d grid of items (excluding bounds checks). Diag controls whether diagonals are considered as adjacent.

func AdjacentPosBounds

func AdjacentPosBounds[T any](diag bool, row, col int, grid [][]T) []Pos

AdjacentPosBounds returns the adjacent positions in a 2d grid of items within the given bounds. Diag controls whether diagonals are considered as adjacent.

func (Pos) Add

func (p Pos) Add(o Pos) Pos

Add returns a new Pos that's the sum of the two rows and cols.

func (Pos) Max

func (p Pos) Max(o Pos) Pos

Max returns a new Pos that's the maximum of the two rows and cols.

func (Pos) Min

func (p Pos) Min(o Pos) Pos

Min returns a new Pos that's the minimum of the two rows and cols.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is a simple first in, first out data structure implementation. It is not safe to use concurrently. The zero value is an empty queue.

func (*Queue[T]) Empty

func (q *Queue[T]) Empty() bool

Empty returns whether the queue has any items in it.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() T

Pop removes an item from the queue.

func (*Queue[T]) Push

func (q *Queue[T]) Push(item T)

Push adds an item to the queue.

func (*Queue[T]) Size

func (q *Queue[T]) Size() int

Size returns the number of items in the queue.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a simple set implementation that only holds unique values. It is not safe to use concurrently. The zero value is an empty set.

func (*Set[T]) Add

func (s *Set[T]) Add(item T)

Add adds an item to the Set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(item T) bool

Contains returns whether the Set contains the item.

func (*Set[T]) Items

func (s *Set[T]) Items() []T

Items returns the items in the Set.

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T) bool

Remove removes an item from the Set. It returns whether the item was removed.

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the size of the set.

type Solver

type Solver[T, V any] struct {
	// ParseF is a parse function that accepts a string and returns the parsed value.
	// See below for common parse functions.
	ParseF func(input string) T

	// SolveF is the function which solves the puzzle using the parsed input.
	SolveF func(parsed T) V
	// contains filtered or unexported fields
}

Solver is a wrapper around running a solution, providing helper methods to simplify boilerplate.

func (*Solver[T, V]) Expect

func (s *Solver[T, V]) Expect(input string, expected V)

Expect runs the solution against input, compares it to expected, and prints the result.

func (*Solver[T, V]) Incorrect

func (s *Solver[T, V]) Incorrect(solutions ...V)

Incorrect marks a solution as being not right; this makes it not be automatically submitted through the API.

func (*Solver[T, V]) Parse

func (s *Solver[T, V]) Parse(input string)

Parse parses input and prints the result.

func (*Solver[T, V]) ParseExpect

func (s *Solver[T, V]) ParseExpect(input string, expected T)

ParseExpect parses input, compares it to expected, and prints the result.

func (*Solver[T, V]) Solve

func (s *Solver[T, V]) Solve()

Solve runs the solution against the real input and prints the result.

func (*Solver[T, V]) Test

func (s *Solver[T, V]) Test(input string)

Test runs the solution against input and prints the result.

func (*Solver[T, V]) Verify

func (s *Solver[T, V]) Verify(expected V)

Verify runs the solution against the real input, compares it to expected, and prints the result.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is a simple first in, last out data structure implementation. It is not safe to use concurrently. The zero value is an empty stack.

func (*Stack[T]) Empty

func (s *Stack[T]) Empty() bool

Empty returns whether the stack has any items in it.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() T

Peek is like Pop but the item isn't removed.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() T

Pop removes an item from the stack.

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Push adds an item to the stack.

func (*Stack[T]) Reverse

func (s *Stack[T]) Reverse()

Reverse reverses the order of the stack.

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size returns the number of items in the stack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL