Documentation
¶
Index ¶
- func AStar[T AStarNode[T]](start T, target func(T) bool, h func(T) int) ([]T, bool)
- func Abs[T constraints.Integer | constraints.Float](v T) T
- func Adjacent[T any](pos Pos, grid Grid[T], diag bool) []T
- func AsDigit(val byte) (int, bool)
- func AsIs(line string) string
- func Atoi(s string) int
- func BFS[T BFSNode[T]](start T, target func(T) bool) ([]T, bool)
- func Clone[T any](items []T) []T
- func Filter[T any](items []T, filter func(T) bool) []T
- func Int(line string) int
- func Ints(line string) []int
- func IsDigit(val byte) bool
- func Keys[T comparable, V any](items map[T]V) []T
- func Map[T, V any](items []T, mapper func(T) V) []V
- func Max(values ...int) int
- func MaxSlice[T constraints.Ordered](items []T) T
- func Min[T constraints.Ordered](values ...T) T
- func MinSlice[T constraints.Ordered](items []T) T
- func MulSlice[T constraints.Integer | constraints.Float](items []T) T
- func Must[T any](value T, err error) T
- func NoPanic(err error)
- func ParseBytes(input string) []byte
- func ParseBytesFunc[T any](parse func(input []byte) T) func(input string) T
- func ParseChunks[T any](parse func(chunk string) T) func(input string) []T
- func ParseChunksUnique[T any](parsers ...func(chunk string, val *T)) func(input string) T
- func ParseGrid[T any](parse func(s byte) T) func(input string) [][]T
- func ParseLine[T any](parse func(line string) T) func(input string) []T
- func ParseRegexp[T any](reg *regexp.Regexp, parse func(parts []string) T) func(line string) T
- func Permutations[T any](items []T) [][]T
- func Pow(num, pow int) int
- func Remove[T any](items []T, index int) []T
- func Reverse[T any](items []T) []T
- func SolveSum[T any, V constraints.Integer | constraints.Float](solve func(T) V) func(items []T) V
- func Subsets[T any](items []T) [][]T
- func SumSlice[T constraints.Integer | constraints.Float](items []T) T
- func ToByteSlice(line string) []byte
- func ToString[T any](solve func(T) []byte) func(T) string
- func TrimSpace[T any](parse func(s string) T) func(input string) T
- func Unique[T comparable](items []T) []T
- type AStarNode
- type BFSNode
- type Grid
- type Heap
- type LinkedList
- type MapGrid
- type OrderedSet
- type Pos
- type Queue
- type Set
- type SliceGrid
- type Solver
- func (s *Solver[T, V]) Expect(input string, expected V)
- func (s *Solver[T, V]) Incorrect(solutions ...V)
- func (s *Solver[T, V]) Parse(input string)
- func (s *Solver[T, V]) ParseExpect(input string, expected T)
- func (s *Solver[T, V]) Solve()
- func (s *Solver[T, V]) Test(input string)
- func (s *Solver[T, V]) Verify(expected V)
- type Stack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[T constraints.Integer | constraints.Float](v T) T
Abs returns the absolute value of v.
func Adjacent ¶
Adjacent returns the adjacent items in a grid of items. Diag controls whether diagonals are considered as adjacent.
func AsDigit ¶
AsDigit returns the integer representation of value if it's a character from '0' to '9', or false if its not.
func Atoi ¶
Atoi is a convenience wrapper on strconv.Atoi that panics if it fails.
func BFS ¶
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 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 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 MaxSlice ¶
func MaxSlice[T constraints.Ordered](items []T) T
MaxSlice returns the max of items.
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 ParseBytes ¶
ParseBytes is a parse function that returns the raw bytes read.
func ParseBytesFunc ¶
ParseBytesFunc is a parse function that returns a parsed value of the raw bytes read.
func ParseChunks ¶
ParseChunks is like ParseLine but parses lines delimited by two new lines, not one
func ParseChunksUnique ¶
ParseChunksUnique is like ParseChunks but uses a unique parser for every chunk. If there are fewer parsers than chunks, the last parser is re-used for the remaining chunks. 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 ParseLine ¶
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 ¶
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 Reverse ¶
func Reverse[T any](items []T) []T
Reverse returns a new slice which is items in reverse order.
func SolveSum ¶
func SolveSum[T any, V constraints.Integer | constraints.Float](solve func(T) V) func(items []T) V
SolveSum is a solve helper function that returns the sum of a slice of items. Useful with ParseLine, or any other parse helper that returns a list of items.
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 ¶
ToByteSlice is a parse function helper that turns the value to a byte slice. Useful with ParseLine.
func Unique ¶
func Unique[T comparable](items []T) []T
Unique returns de-duplicated items in items. The type must be comparable, and the comparison is by value equality.
Types ¶
type AStarNode ¶
type AStarNode[T any] interface { comparable Adjacent() []T }
type BFSNode ¶
type BFSNode[T any] interface { comparable Adjacent() []T }
type Grid ¶
type Grid[T any] interface { // contains filtered or unexported methods }
Grid is an interface that represents a two-dimensional addressable grid.
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.
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 ¶
AdjacentPos returns the adjacent positions in a Grid of items. Diag controls whether diagonals are considered as adjacent.
func AdjacentPosNoBoundsChecks ¶
AdjacentPosNoBoundsChecks returns the adjacent positions in a 2d grid of items (excluding bounds checks). Diag controls whether diagonals are considered as adjacent.
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.
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 NewSet ¶
func NewSet[T comparable](items []T) Set[T]
type SliceGrid ¶
type SliceGrid[T any] struct { Grid [][]T }
SliceGrid is an implementation of Grid using a two-dimensional slice.
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 ¶
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]) ParseExpect ¶
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.