aoc

package module
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package aoc provides some Advent of Code utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(a int) int

Abs returns the absolute value.

func GreatestCommonDivisor

func GreatestCommonDivisor(a, b int) int

GreatestCommonDivisor returns the greatest common divisor of two numbers.

func IndexAll

func IndexAll(s string, search string) []int

IndexAll returns all the indices from a given string into a string.

func IntToRune

func IntToRune(i int) rune

IntToRune converts an int into a rune.

func IsRuneDecimal

func IsRuneDecimal(r rune) bool

IsRuneDecimal checks whether a rune is a decimal number.

func LeastCommonMultiple

func LeastCommonMultiple(numbers []int) int

LeastCommonMultiple returns the least common multiple from a list of numbers.

func ManhattanDistance

func ManhattanDistance(row, col int) int

ManhattanDistance returns the manhattan distance.

func MapKeysToSlice added in v1.9.0

func MapKeysToSlice[K comparable, V any](m map[K]V) []K

MapKeysToSlice converts the maps keys to a slice.

func MapValuesToSlice added in v1.9.0

func MapValuesToSlice[K comparable, V any](m map[K]V) []V

MapValuesToSlice converts the map values to a slice.

func Mod

func Mod(d, m int) int

Mod returns the modulo.

func ReaderToInts

func ReaderToInts(input io.Reader) []int

ReaderToInts converts optimistically an io.Reader into a slice of strings.

func ReaderToString

func ReaderToString(input io.Reader) string

ReaderToString converts an io.Reader into a string.

func ReaderToStrings

func ReaderToStrings(input io.Reader) []string

ReaderToStrings converts an io.Reader into a slice of strings.

func RuneToInt

func RuneToInt(r rune) int

RuneToInt converts a rune into an int.

func SliceToMap added in v1.9.0

func SliceToMap[K comparable, V any](s []K, f func(K) V) map[K]V

SliceToMap converts a slice into a map.

func StringGroups

func StringGroups(lines []string) [][]string

StringGroups returns groups of lines inputs that are not separated by empty lines.

For example:

0, 1, 2

foo bar

Returns {"0, 1, 2"} and {"foo", "bar"}

func StringToInt

func StringToInt(s string) int

StringToInt is an optimistic conversion from a string to an int.

func StringsToInts

func StringsToInts(s []string) []int

StringsToInts is an optimistic conversion from a slice of strings to a slice of ints.

func Substring

func Substring(s string, del string) string

Substring returns the substring from a given delimiter.

func TopologicalSort added in v1.9.0

func TopologicalSort[K comparable](vertices []K, edgesFunc func(K) []K) []K

TopologicalSort applies the topological sort algorithm based on vertices and a way to compute the edges.

func TryStringToInt

func TryStringToInt(s string) (int, bool)

TryStringToInt tries to convert a string into an int.

Types

type Board added in v1.6.1

type Board[T any] struct {
	Positions map[Position]T
	MinRows   int
	MinCols   int
	MaxRows   int
	MaxCols   int
}

func NewBoard added in v1.8.0

func NewBoard[T any](positions map[Position]T) Board[T]

NewBoard creates a board from a list of positions.

func ParseBoard added in v1.4.0

func ParseBoard[T any](lines []string, fn func(r rune, pos Position) T) Board[T]

ParseBoard parses a board and maps it to a map of Position.

func (Board[T]) Contains added in v1.8.0

func (b Board[T]) Contains(position Position) bool

Contains checks whether a position is inside a board.

func (Board[T]) Get added in v1.6.2

func (b Board[T]) Get(pos Position) T

Get returns the value at a give position.

func (Board[T]) String added in v1.8.3

func (b Board[T]) String(f func(T) rune, missing rune) string

String returns the board representation.

type DAG added in v1.9.0

type DAG[K comparable, V any] struct {
	// contains filtered or unexported fields
}

DAG is the representation of a directed acyclic graph.

func NewDAG added in v1.9.0

func NewDAG[K comparable, V any]() DAG[K, V]

NewDAG returns a new DAG.

func (DAG[K, V]) AddEdge added in v1.9.0

func (g DAG[K, V]) AddEdge(from, to *DAGNode[K, V])

AddEdge adds a new edge.

func (DAG[K, V]) AddSimpleEdge added in v1.9.0

func (g DAG[K, V]) AddSimpleEdge(from, to K)

AddSimpleEdge adds a simple edge, without the DAGNode.

func (DAG[K, V]) Edges added in v1.9.0

func (g DAG[K, V]) Edges(id K) []*DAGNode[K, V]

Edges returns the children from an id.

func (DAG[K, V]) Node added in v1.9.0

func (g DAG[K, V]) Node(id K) (*DAGNode[K, V], bool)

Node returns a node from an id.

func (DAG[K, V]) TopologicalSort added in v1.9.0

func (g DAG[K, V]) TopologicalSort() []K

TopologicalSort applies the topological sort algorithm on a DAG.

type DAGNode added in v1.9.0

type DAGNode[K comparable, V any] struct {
	Id   K
	Data V
}

DAGNode is a DAG node.

type Delimiter

type Delimiter struct {
	// contains filtered or unexported fields
}

Delimiter implementation.

func NewDelimiter

func NewDelimiter(s, del string, opts ...DelimiterOption) Delimiter

NewDelimiter creates a new input delimiter logic.

func (Delimiter) GetInt

func (d Delimiter) GetInt(i int) int

GetInt returns the int at a given index with an optimistic conversion.

func (Delimiter) GetInts

func (d Delimiter) GetInts() []int

GetInts returns all the ints found with an optimistic conversion.

func (Delimiter) GetString

func (d Delimiter) GetString(i int) string

GetString returns the string at a given index.

func (Delimiter) GetStrings

func (d Delimiter) GetStrings() []string

GetStrings returns all the strings found.

func (Delimiter) IsInt

func (d Delimiter) IsInt(i int) bool

IsInt checks whether the value at a given index is an int.

func (Delimiter) TryGetInt

func (d Delimiter) TryGetInt(i int) (int, bool)

TryGetInt returns the int at a given index.

type DelimiterOption

type DelimiterOption func(options *delimiterOptions)

DelimiterOption holds the Delimiter options.

func WithTrimSpace

func WithTrimSpace() DelimiterOption

WithTrimSpace applies strings.trimSpace on each string.

type Direction

type Direction int

Direction enum

const (
	Up Direction = iota
	Down
	Left
	Right

	UpLeft
	UpRight
	DownLeft
	DownRight
)

func (Direction) Rev added in v1.2.0

func (d Direction) Rev() Direction

Rev reverses the current direction.

func (Direction) String added in v1.2.0

func (d Direction) String() string

String implements strings.Stringer.

func (Direction) Turn

func (d Direction) Turn(turn Direction) Direction

Turn turns left or right.

type Location added in v1.2.0

type Location struct {
	Pos Position
	Dir Direction
}

Location represents a given position and direction.

func NewLocation added in v1.3.0

func NewLocation(row, col int, dir Direction) Location

NewLocation creates a new location.

func (Location) Move added in v1.2.0

func (l Location) Move(d Direction, moves int) Location

Move moves in a given direction.

func (Location) Rev added in v1.2.0

func (l Location) Rev(moves int) Location

Rev moves in the reverse direction.

func (Location) Straight added in v1.2.0

func (l Location) Straight(moves int) Location

Straight moves in the current direction.

func (Location) String added in v1.2.0

func (l Location) String() string

String implements strings.Stringer.

func (Location) Turn added in v1.2.0

func (l Location) Turn(d Direction, moves int) Location

Turn turns left or right.

type Node added in v1.7.0

type Node[T any] struct {
	Data     T
	Previous *Node[T]
	Next     *Node[T]
}

Node is a linked list node.

func NewNode added in v1.7.0

func NewNode[T any](data T) *Node[T]

NewNode returns a new node.

func (*Node[T]) Head added in v1.7.0

func (n *Node[T]) Head() *Node[T]

Head returns the head.

func (*Node[T]) InsertAfter added in v1.7.0

func (n *Node[T]) InsertAfter(data T)

InsertAfter insers a node after the current one.

func (*Node[T]) InsertHead added in v1.7.0

func (n *Node[T]) InsertHead(data T) *Node[T]

InsertHead inserts a node to the head and return the head.

func (*Node[T]) InsertTail added in v1.7.0

func (n *Node[T]) InsertTail(data T) *Node[T]

InsertTail inserts a node to the tail and return the tail.

func (*Node[T]) String added in v1.7.0

func (n *Node[T]) String() string

String implements strings.Stringer.

func (*Node[T]) Tail added in v1.7.0

func (n *Node[T]) Tail() *Node[T]

Tail returns the tail.

type Position

type Position struct {
	Row int
	Col int
}

Position represents a given position (row/col)

func NewPosition added in v1.3.0

func NewPosition(row, col int) Position

NewPosition creates a new position.

func (Position) Delta

func (p Position) Delta(row, col int) Position

Delta returns a new position from a row delta and col delta.

func (Position) Manhattan

func (p Position) Manhattan(p2 Position) int

Manhattan returns the manhattan distance.

func (Position) ManhattanZero

func (p Position) ManhattanZero() int

ManhattanZero returns the manhattan distance from the zero position.

func (Position) Move

func (p Position) Move(direction Direction, moves int) Position

Move moves into a given direction and a certain number of times.

func (Position) String

func (p Position) String() string

String implements strings.Stringer.

type PriorityQueue added in v1.5.0

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

PriorityQueue is a priority queue implementation.

func NewPriorityQueue added in v1.5.0

func NewPriorityQueue[T comparable](comparator func(a, b T) int) PriorityQueue[T]

NewPriorityQueue creates a new PriorityQueue using a comparator.

func (*PriorityQueue[T]) IsEmpty added in v1.5.0

func (pq *PriorityQueue[T]) IsEmpty() bool

IsEmpty checks if the priority queue is empty.

func (*PriorityQueue[T]) Len added in v1.5.1

func (pq *PriorityQueue[T]) Len() int

Len returns the number of elements in the priority queue.

func (*PriorityQueue[T]) Peek added in v1.5.0

func (pq *PriorityQueue[T]) Peek() T

Peek returns the top element of the priority queue without removing it.

func (*PriorityQueue[T]) Pop added in v1.5.0

func (pq *PriorityQueue[T]) Pop() T

Pop removes and returns the top element from the priority queue.

func (*PriorityQueue[T]) Push added in v1.5.0

func (pq *PriorityQueue[T]) Push(item T)

Push pushes a new item.

Jump to

Keyboard shortcuts

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