utils

package
v0.0.0-...-d752c0d Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxInt = 9223372036854775807
)

Variables

View Source
var NeighborsAll [][3]int
View Source
var NeighborsStraight = [][3]int{{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}}

Functions

func Abs

func Abs(a int) int

func Atoi

func Atoi(s string) int

Atoi is panicing version of strconv.Atoi. Panics instead of error.

func CRT

func CRT(cycles []Cycle) int

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 Check

func Check(err error)

func Cmp

func Cmp(a, b int) int

func ContainsInt

func ContainsInt(x int, entries []int) bool

func ContainsString

func ContainsString(x string, entries []string) bool

func CountInts

func CountInts(entries []int) map[int]int

func CountStrings

func CountStrings(entries []string) map[string]int

func Cut

func Cut(s string, separator string) (left, right string)

Cut is panicing version of strings.Cut. Panics instead of error.

func FirstAsciiNr

func FirstAsciiNr(s string) int

FirstAsciiNr return ascii number of first character in string. Panics if value > 127.

func GCD

func GCD(a, b int) int

GCD - greatest common divisor (GCD) via Euclidean algorithm

func LCM

func LCM(values []int) int

LCM - least common multiple (LCM) via GCD

func Max

func Max(numbers []int) int

func Min

func Min(numbers []int) int

func MinMax

func MinMax(a, b int) (int, int)

func MinMaxInts

func MinMaxInts(numbers []int) (int, int)

func ReadLinesFromFile

func ReadLinesFromFile(path string) []string

ReadLinesFromFile reads lines from a file.

func ReadNumbersFromFile

func ReadNumbersFromFile(path string) []int

ReadNumbersFromFile reads one integer from each line in file.

func ReadRawLinesFromFile

func ReadRawLinesFromFile(path string) []string

ReadRawLinesFromFile reads lines from a file.

func ReverseSlice

func ReverseSlice[T any](s []T)

func Sign

func Sign(a int) int

func Sort

func Sort(a []int)

func SplitToChars

func SplitToChars(line string) []string

SplitToChars splits a line into chars.

func SplitToInts

func SplitToInts(line string) []int

SplitToInts finds all ints in line (including sign).

func SplitToRunes

func SplitToRunes(line string) []rune

SplitToRunes splits a line into runes.

func SplitWithSpace

func SplitWithSpace(line string) []string

SplitWithSpace splits a line with space as separator.

func SplitWithTrim

func SplitWithTrim(line, splitPattern string) []string

func Triangle

func Triangle(nr int) int

func Trim

func Trim(line string) string

Trim just trims space.

func TrimTrailingNewline

func TrimTrailingNewline(lines []string) []string

TrimTrailingNewline removes trailing newline

Types

type CharGrid

type CharGrid struct {
	Grid   [][]string
	Width  int
	Height int
}

func CreateCharGridFromLines

func CreateCharGridFromLines(lines []string) CharGrid

func CreateEmptyCharGrid

func CreateEmptyCharGrid(width, height int) CharGrid

func (*CharGrid) Find

func (g *CharGrid) Find(x string) (row, col int)

func (CharGrid) InBounds

func (g CharGrid) InBounds(y, x int) bool

InBounds - is (y, x) in grid

func (*CharGrid) SetAll

func (g *CharGrid) SetAll(value string)

func (CharGrid) String

func (g CharGrid) String() string

type Command

type Command struct {
	Verb  string `regroup:"verb"`
	Value int    `regroup:"value"`
}

func ParseCommand

func ParseCommand(line string) Command

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 DigitGrid

type DigitGrid struct {
	Grid   [][]int
	Width  int
	Height int
}

func CreateDigitGridFromLines

func CreateDigitGridFromLines(lines []string) DigitGrid

func CreateZeroDigitGrid

func CreateZeroDigitGrid(width, height int) DigitGrid

func (DigitGrid) InBounds

func (g DigitGrid) InBounds(y, x int) bool

InBounds - is (y, x) in grid

func (*DigitGrid) SetAll

func (g *DigitGrid) SetAll(value int)

func (*DigitGrid) String

func (g *DigitGrid) String() string

type Grid2D

type Grid2D[T comparable] struct {
	Grid   [][]T
	Width  int
	Height int
}

func CreateGrid2D

func CreateGrid2D[T comparable](width, height int) Grid2D[T]

func (*Grid2D[T]) At

func (g *Grid2D[T]) At(row, col int) T

func (Grid2D[T]) AtBorder

func (g Grid2D[T]) AtBorder(row, col int) bool

AtBorder checks if (row, col) is at the border of the grid

func (*Grid2D[T]) Find

func (g *Grid2D[T]) Find(value T) (row, col int, ok bool)

func (Grid2D[T]) Get

func (g Grid2D[T]) Get(row, col int) T

func (Grid2D[T]) InBounds

func (g Grid2D[T]) InBounds(row, col int) bool

InBounds checks if (row, col) inside grid

func (*Grid2D[T]) Set

func (g *Grid2D[T]) Set(val T, row, col int)

Set sets an individual cell in the grid

func (*Grid2D[T]) SetAll

func (g *Grid2D[T]) SetAll(value T)

SetAll sets all cells in the grid to the given value

type Grid3D

type Grid3D[K any] struct {
	G   [][][]K
	Min [3]int
	Max [3]int
}

func NewGrid

func NewGrid[K any](min, max [3]int) *Grid3D[K]

func (*Grid3D[K]) Get

func (g *Grid3D[K]) Get(x, y, z int) K

func (*Grid3D[K]) IsEdge

func (g *Grid3D[K]) IsEdge(x, y, z int) bool

func (*Grid3D[K]) IsOut

func (g *Grid3D[K]) IsOut(x, y, z int) bool

func (*Grid3D[K]) Set

func (g *Grid3D[K]) Set(v K, x, y, z int)

func (*Grid3D[K]) Volume

func (g *Grid3D[K]) Volume() int

type Pos2D

type Pos2D struct {
	Row, Col int
}

func (Pos2D) Add

func (p Pos2D) Add(other Pos2D) Pos2D

func (Pos2D) Left

func (p Pos2D) Left() Pos2D

left is a 90 degree left rotation of the vector

func (Pos2D) Mul

func (p Pos2D) Mul(factor int) Pos2D

func (Pos2D) Neg

func (p Pos2D) Neg() Pos2D

func (Pos2D) Right

func (p Pos2D) Right() Pos2D

right is a 90 degree right rotation of the vector

func (Pos2D) Sub

func (p Pos2D) Sub(other Pos2D) Pos2D

type RuneGrid

type RuneGrid struct {
	Grid   [][]rune
	Width  int
	Height int
}

func CreateRuneGridFromLines

func CreateRuneGridFromLines(lines []string) RuneGrid

func (RuneGrid) At

func (g RuneGrid) At(r, c int) rune

func (RuneGrid) Copy

func (g RuneGrid) Copy() RuneGrid

func (*RuneGrid) Find

func (g *RuneGrid) Find(x rune) (row, col int)

func (RuneGrid) InBounds

func (g RuneGrid) InBounds(r, c int) bool

InBounds - is (row, col) in grid

func (*RuneGrid) SetAll

func (g *RuneGrid) SetAll(value rune)

func (RuneGrid) String

func (g RuneGrid) String() string

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 CreateSet

func CreateSet[K comparable]() Set[K]

CreateSet - create an empty set

func (Set[K]) Add

func (s Set[K]) Add(elem K)

Add - add elem to set

func (Set[K]) Clone

func (s Set[K]) Clone() Set[K]

func (Set[K]) Contains

func (s Set[K]) Contains(elem K) bool

Contains - check if elem in set

func (Set[K]) Extend

func (s Set[K]) Extend(other Set[K])

Extend - extend set s with all elements in other (the result is union)

func (Set[K]) GetOne

func (s Set[K]) GetOne() K

func (Set[K]) Intersect

func (s Set[K]) Intersect(other Set[K])

Intersect - only keep elements in s which are also in other

func (Set[K]) Remove

func (s Set[K]) Remove(elem K)

Remove - remove elem from set (does not need to be in set)

func (Set[K]) Size

func (s Set[K]) Size() int

Size returns size of set.

func (Set[K]) Subtract

func (s Set[K]) Subtract(other Set[K])

Subtract - remove all elements from s that are in other

func (Set[K]) Values

func (s Set[K]) Values() []K

Values returns values in Set

type Stack

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

Stack

func (*Stack[K]) Depth

func (s *Stack[K]) Depth() int

func (*Stack[K]) IsEmpty

func (s *Stack[K]) IsEmpty() bool

func (*Stack[K]) Pop

func (s *Stack[K]) Pop() (elem K, ok bool)

Pop - get element if available as signaled by ok

func (*Stack[T]) Push

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

func (*Stack[K]) Reverse

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

Jump to

Keyboard shortcuts

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