Documentation ¶
Overview ¶
Package util contains shared functions for several AoC days.
Index ¶
- Variables
- func CheckPrefix(s, prefix string) (tail string, ok bool)
- func Chunks(s string) (chunks []string)
- func Diag(a ...interface{})
- func Diagf(format string, a ...interface{})
- func Diagln(a ...interface{})
- func DistC(a, b P) int
- func DistM(a, b P) int
- func Ints(s string) (ints []int)
- func IsDiag() bool
- func Lines(s string) (lines []string)
- func NextInt(s string) (n int, ok bool, tail string)
- func NextWord(s string) (word, tail string)
- func QuickSelect(input []int, k int) int
- func ReadChunks(path string) (chunks []string, err error)
- func ReadInts(path string) ([]int, error)
- func ReadLines(path string) ([]string, error)
- func ReadRegexp(path, pattern string) ([][]string, error)
- func ScanAll(r io.Reader, split bufio.SplitFunc) (tokens []string, err error)
- func ScanAllInts(r io.Reader) (ints []int, err error)
- func ScanAllRegexp(r io.Reader, pattern string) ([][]string, error)
- func ScanChunks(data []byte, atEOF bool) (advance int, token []byte, err error)
- func ScanInts(data []byte, atEOF bool) (advance int, token []byte, err error)
- func Sort3(x, y, z int) (a, b, c int)
- func SortBy[S ~[]I, F ~func(I) O, I any, O cmp.Ordered](x S, f F)
- func Words(s string) (words []string)
- type Bitmap2D
- func (bmp *Bitmap2D) Clear(x, y int)
- func (bmp *Bitmap2D) Count() (sum int)
- func (bmp *Bitmap2D) Get(x, y int) bool
- func (bmp *Bitmap2D) GetClear(x, y int) (old bool)
- func (bmp *Bitmap2D) GetR(x0, y0, x1, y1 int) (result uint64)
- func (bmp *Bitmap2D) GetSet(x, y int) (old bool)
- func (bmp *Bitmap2D) Set(x, y int)
- type BucketQ
- type FixedBitmap1D
- type FixedBitmap2D
- func (bmp FixedBitmap2D) Clear(x, y int)
- func (bmp FixedBitmap2D) Clone() (clone FixedBitmap2D)
- func (bmp FixedBitmap2D) Get(x, y int) bool
- func (bmp FixedBitmap2D) GetN(x, y, n int) uint64
- func (bmp FixedBitmap2D) RotateL(w int)
- func (bmp FixedBitmap2D) RotateR(w int)
- func (bmp FixedBitmap2D) Set(x, y int)
- func (bmp FixedBitmap2D) Size() (w, h int)
- type FixedLevel
- type LabelMap
- type Level
- func EmptyLevel(min, max P, empty byte) *Level
- func ParseLevel(data []byte, empty byte) *Level
- func ParseLevelAt(data []byte, empty byte, min P) *Level
- func ParseLevelString(data string, empty byte) *Level
- func ParseLevelStringAt(data string, empty byte, min P) *Level
- func ReadLevel(path string, empty byte) (*Level, error)
- func SparseLevel(origin P, empty byte) *Level
- func (l *Level) At(x, y int) byte
- func (l *Level) Bounds() (min, max P)
- func (l *Level) Copy() *Level
- func (l *Level) CoreAt(x, y int) byte
- func (l *Level) CoreBounds() (min, max P)
- func (l *Level) CoreSet(x, y int, b byte)
- func (l *Level) Find(key byte) (x, y int, found bool)
- func (l *Level) InBounds(x, y int) bool
- func (l *Level) Lines(min, max P) []string
- func (l *Level) Range(cb func(x, y int, b byte))
- func (l *Level) Row(x1, x2, y int) []byte
- func (l *Level) Set(x, y int, b byte)
- func (l *Level) Size() (w, h int)
- func (l *Level) Write(w io.Writer) error
- func (l *Level) WriteRect(w io.Writer, min, max P) error
- type P
- type Queue
- type Splitter
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func CheckPrefix ¶
CheckPrefix tests if a string contains a prefix, and if so, removes it. If the string does not contain the prefix, it's returned unmodified. The `ok` result is true if the prefix was found.
func Diagf ¶
func Diagf(format string, a ...interface{})
Diagf formats data to the diagnostic output stream.
func Diagln ¶
func Diagln(a ...interface{})
Diagln prints its arguments, followed by a newline, to the diagnostic output stream.
func Ints ¶
Ints returns the list of all contiguous sequences of decimal digits parsed as integers, as defined by the ScanInts split function.
func Lines ¶
Lines splits the input string by newlines as if by bufio.ScanLines. In other words, it will not return an empty string even if the input has a trailing newline.
func NextInt ¶
NextInt parses the leading decimal digits of s as a (nonnegative) decimal number, and returns both the parsed number and the remainder of the input. If there are no decimal digits, `ok` will be false.
func NextWord ¶
NextWord returns the prefix of s up to the first space character, if any. If there is no space, the entire string is returned. The second return value gives the remainder of the input string.
func QuickSelect ¶
QuickSelect returns the k'th smallest element of the input array.
func ReadChunks ¶
ReadChunks returns the contents of a text file as a slice of strings representing all paragraphs, as defined by text separated by a blank line (two consecutive newlines).
func ReadInts ¶
ReadInts parses a text file containing integers separated by any non-digits (see ScanInts).
func ReadLines ¶
ReadLines returns the contents of a text file as a slice of strings representing the lines. The newline separators are not kept. The last line need not have a newline character at the end.
func ReadRegexp ¶
ReadRegexp parses a text file using a regular expression; see ScanAllRegexp for details.
func ScanAll ¶
ScanAll runs a scanner on a reader with the given split function, and returns all tokens.
func ScanAllInts ¶
ScanAllInts extracts all decimal integers from the reader.
func ScanAllRegexp ¶
ScanAllRegexp parses a reader's contents using a regular expression. The return value is a list of lists, containing each line's submatches. Note that unlike the usual convention, the match of the entire regular expression is not included.
func ScanChunks ¶
ScanChunks implements a bufio.SplitFunc for scanning paragraphs delimited by a blank line (i.e., two consecutive '\n' bytes).
func ScanInts ¶
ScanInts implements a bufio.SplitFunc for scanning decimal integers separated by any non-digits. An optional - can also be included as the first character of the token. "123-456" will be split to the two tokens "123", "-456".
Types ¶
type BucketQ ¶
type BucketQ[T any] struct { // contains filtered or unexported fields }
BucketQ is a priority queue using the "bucket queue" data structure.
func NewBucketQ ¶
NewBucketQ makes a new queue with the given maximum span, which must be a power of 2. The distance between the lowest and highest priority item in the queue must not exceed the span.
type FixedBitmap1D ¶
type FixedBitmap1D []uint64
func MakeFixedBitmap1D ¶
func MakeFixedBitmap1D(size int) FixedBitmap1D
func (FixedBitmap1D) Clear ¶
func (bmp FixedBitmap1D) Clear()
func (FixedBitmap1D) Get ¶
func (bmp FixedBitmap1D) Get(i int) bool
func (FixedBitmap1D) Set ¶
func (bmp FixedBitmap1D) Set(i int)
func (FixedBitmap1D) Unset ¶
func (bmp FixedBitmap1D) Unset(i int)
type FixedBitmap2D ¶
type FixedBitmap2D [][]uint64
func MakeFixedBitmap2D ¶
func MakeFixedBitmap2D(w, h int) FixedBitmap2D
func (FixedBitmap2D) Clear ¶
func (bmp FixedBitmap2D) Clear(x, y int)
func (FixedBitmap2D) Clone ¶
func (bmp FixedBitmap2D) Clone() (clone FixedBitmap2D)
func (FixedBitmap2D) Get ¶
func (bmp FixedBitmap2D) Get(x, y int) bool
func (FixedBitmap2D) GetN ¶
func (bmp FixedBitmap2D) GetN(x, y, n int) uint64
func (FixedBitmap2D) RotateL ¶
func (bmp FixedBitmap2D) RotateL(w int)
func (FixedBitmap2D) RotateR ¶
func (bmp FixedBitmap2D) RotateR(w int)
func (FixedBitmap2D) Set ¶
func (bmp FixedBitmap2D) Set(x, y int)
func (FixedBitmap2D) Size ¶
func (bmp FixedBitmap2D) Size() (w, h int)
type FixedLevel ¶
type FixedLevel struct {
// W and H are the level size.
W, H int
// Data is a row-major representation of the level data in a W*H length array.
Data []byte
}
FixedLevel is a restricted subset of Level, with less overhead for some operations.
Limitations:
- The bounding box of the level has a predefined size based on the initial contents.
- The northwest corner is fixed at (0, 0).
func EmptyFixedLevel ¶
func EmptyFixedLevel(w, h int, empty byte) *FixedLevel
EmptyFixedLevel returns an empty fixed level filled with the given byte.
func ParseFixedLevel ¶
func ParseFixedLevel(allData []byte) *FixedLevel
ParseFixedLevel converts the input to a level structure. All lines are expected to be equally long.
func (*FixedLevel) At ¶
func (l *FixedLevel) At(x, y int) byte
At returns the value at the given coordinates.
func (*FixedLevel) Find ¶
func (l *FixedLevel) Find(key byte) (x, y int, found bool)
Find locates the coordinates of a byte, which must be unique on the level.
func (*FixedLevel) InBounds ¶
func (l *FixedLevel) InBounds(x, y int) bool
InBounds returns true if the given coordinates are within the area of the level.
func (*FixedLevel) Row ¶
func (l *FixedLevel) Row(y int) []byte
Row returns the contents of an entire single row of the level.
func (*FixedLevel) Set ¶
func (l *FixedLevel) Set(x, y int, b byte)
Set assigns a new value at the given coordinates.
func (*FixedLevel) Write ¶
func (l *FixedLevel) Write(w io.Writer)
Write outputs the contents of the level to the given writer.
type LabelMap ¶
LabelMap is a mapping from string labels to integer indices, with automatic allocation.
type Level ¶
type Level struct {
// contains filtered or unexported fields
}
A Level models a two-dimensional map of ASCII character cells, similar to a roguelike level. The origin of the coordinate system is the top-left cell; X grows right, Y grows down.
func EmptyLevel ¶
EmptyLevel returns a level filled with empty space matching the provided symbol. The given bounds determine the densely allocated space and the initial bounds of the level.
func ParseLevel ¶
ParseLevel parses a byte array into a level. See ReadLevel.
func ParseLevelAt ¶
ParseLevelAt parses a byte array into a level using a specified offset.
func ParseLevelString ¶
ParseLevelString parses a string into a level. See ReadLevel.
func ParseLevelStringAt ¶
ParseLevelStringAt parses a string into a level using a specified offset.
func ReadLevel ¶
ReadLevel reads the contents of a text file into a level. Character cells outside the contents of the file are considered to be the specified empty byte.
func SparseLevel ¶
SparseLevel returns a level with no densely allocated space at all. The level bounds are set to contain only the origin point.
func (*Level) Bounds ¶
Bounds returns the top-left and bottom-right corners of the level's bounding box. See InBounds for the definition.
func (*Level) CoreAt ¶
CoreAt returns the byte at the given coordinates, which must be within the densely stored region (see CoreBounds).
func (*Level) CoreBounds ¶
CoreBounds returns the top-left and bottom-right corners of the core (densely stored) area.
func (*Level) CoreSet ¶
CoreSet sets the byte at the given coordinates, which must be within the densely stored region (see CoreBounds).
func (*Level) InBounds ¶
InBounds returns true if the given coordinates are within the bounding box of the level. The bounds will grow to accommodate new non-empty characters, but will never shrink even if those characters are later overwritten to be empty.
func (*Level) Row ¶
Row returns a []byte with the contents of a single contiguous row from the level. If this is within the core area, the returned slice will share storage with the level.
func (*Level) Size ¶
Size returns the width and height of the bounding box of the level (see InBounds).
type P ¶
type P struct {
X, Y int
}
P represents a two-dimensional integer-valued coordinate.
func (P) Add ¶
Add returns the point with coordinates corresponding to the sum of the receiver and the other point.
func (P) Neigh ¶
Neigh returns a point's von Neumann neighbourhood (the 4 orthogonally adjacent elements).
The directions will be returned in this order: north, south, west, east.
func (P) Neigh8 ¶
Neigh8 returns a point's Moore neighbourhood (the 8 orthogonally or diagonally adjacent elements).
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a simple array-backed ring buffer implementation of an unbounded queue.
Using it in place of a simple slice might help avoid some copying, at least in a scenario where the "active" size stays roughly the same but a lot of elements go through the queue.
func MakeQueue ¶
MakeQueue makes a new, empty queue of the given initial size, which must be a power of 2.
func QueueOf ¶
QueueOf returns a new queue of the given size initialized with the specified contents.
func (*Queue[T]) Clear ¶
func (q *Queue[T]) Clear()
Clear makes the queue empty but without releasing any storage associated with it.
func (Queue[T]) Index ¶
Index returns the i'th item in the queue. The head of the queue (least recently pushed item) is at index 0.
func (*Queue[T]) Pop ¶
func (q *Queue[T]) Pop() T
Pop removes (and returns) the first (least recently added) item in the queue. The queue must not be empty.
func (*Queue[T]) Slice ¶
Slice returns a segment of the contents of the queue as a slice. The semantics of the parameters are the same as the `s[low:high]` expression on a slice. If possible, the returned slice will share backing array with the queue. However, if the selected region wraps around, a copy will be made.
type Splitter ¶
type Splitter string
Splitter is a convenience type for iterating over the results of splitting a string without allocating a slice.
func (Splitter) Count ¶
Count returns how many parts there would be in the string if it were to be split with a delimiter.
Directories ¶
Path | Synopsis |
---|---|
Package fn contains the sort of non-Go-like, occasionally higher-order, utility functions you might find in a functional language.
|
Package fn contains the sort of non-Go-like, occasionally higher-order, utility functions you might find in a functional language. |
Package graph contains utilities for (sparse|dense) (un|)directed (un|)weighted graphs.
|
Package graph contains utilities for (sparse|dense) (un|)directed (un|)weighted graphs. |
Package ix contains small integer arithmetic functions in the style of the standard `math` package.
|
Package ix contains small integer arithmetic functions in the style of the standard `math` package. |