aoc

package module
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

README

Advent Of Code Library in Go

The library that I use extensively for my Advent Of Code solutions.

Documentation

Overview

Package aoc provides some utilities for completing and Advent of Code.

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 DeleteSliceIndex added in v1.0.1

func DeleteSliceIndex[T any](in []T, index int, deepCopy bool) []T

DeleteSliceIndex deletes a given index.

func DeleteSliceIndices added in v1.0.1

func DeleteSliceIndices[T any](in []T, indices []int, deepCopy bool) []T

DeleteSliceIndices deletes a list of indices.

func GreatestCommonDivisor

func GreatestCommonDivisor(a, b int) int

GreatestCommonDivisor returns the greatest common divisor of two numbers.

func InnerMapGet

func InnerMapGet[K1 comparable, K2 comparable, X any](m map[K1]map[K2]X, k K1, cap ...int) map[K2]X

InnerMapGet performs a get or init in an inner map.

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 IsSliceMonotonicallyDecreasing

func IsSliceMonotonicallyDecreasing[T constraints.Ordered](in []T) bool

func IsSliceMonotonicallyIncreasing

func IsSliceMonotonicallyIncreasing[T constraints.Ordered](in []T) bool

func IsSliceSorted

func IsSliceSorted[T constraints.Ordered](in []T, comp func(a T, b T) bool) bool

func IsSliceStrictlyDecreasing

func IsSliceStrictlyDecreasing[T constraints.Ordered](in []T) bool

func IsSliceStrictlyIncreasing

func IsSliceStrictlyIncreasing[T constraints.Ordered](in []T) bool

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 MapCopy added in v1.0.3

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

func MapKeysToSlice

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

MapKeysToSlice converts the maps keys to a slice.

func MapPositionsToString added in v1.0.4

func MapPositionsToString[V any](positions map[Position]V) string

func MapSwitch added in v1.0.12

func MapSwitch[K, V comparable](m map[K]V) map[V]K

func MapValuesToSlice

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 Over added in v1.0.11

func Over[T any](ts ...T) iter.Seq[T]

func ReaderToInts

func ReaderToInts(input io.Reader) []int

ReaderToInts converts an io.Reader into a slice of strings. It panics in case of a parsing error.

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 RegexpFindAll

func RegexpFindAll(s string, re *regexp.Regexp) []string

RegexpFindAll finds all occurrences of a regexp.

func RuneToInt

func RuneToInt(r rune) int

RuneToInt converts a rune into an int.

func SliceCopy

func SliceCopy[T any](in []T) []T

func SliceCount

func SliceCount[T comparable](in []T) map[T]int

func SlicePositionsToString added in v1.0.4

func SlicePositionsToString(positions []Position) string

func SliceToMap

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

SliceToMap converts a slice into a map.

func SliceToSet

func SliceToSet[T comparable](s []T) map[T]bool

SliceToSet converts a slice into a set (a map where all the keys are the elements from the slice and the values are true).

func StringFindIndices

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

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

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 converts a string into an int. It panics in case of a parsing error.

func StringsToInts

func StringsToInts(s []string) []int

StringsToInts converts a slice of strings to a slice of ints. It panics in case of a parsing error.

func Substring

func Substring(s string, del string) string

Substring returns the substring from a given delimiter.

func TopologicalSort

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

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

func NewBoard

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

NewBoard creates a board from a list of positions.

func NewBoardFromElements added in v1.0.7

func NewBoardFromElements[T any](elements []BoardElement[T], rows, cols int) Board[T]

func NewBoardFromLength added in v1.0.5

func NewBoardFromLength[T any](fromRow, toRow, fromCol, toCol int, zero T) Board[T]

func NewBoardFromReader

func NewBoardFromReader[T any](input io.Reader, f func(row, col int, r rune) T) Board[T]

NewBoardFromReader creates a board from an input reader.

func NewBoardWithLength added in v1.0.6

func NewBoardWithLength[T any](positions map[Position]T, rows, cols int) Board[T]

func ParseBoard

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

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

Contains checks whether a position is inside a board.

func (Board[T]) Get

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

Get returns the value at a give position.

func (Board[T]) String

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

String returns the board representation.

type BoardElement added in v1.0.7

type BoardElement[T any] struct {
	T   T
	Pos Position
}

type CapturingGroup

type CapturingGroup struct {
	Start int
	End   int
}

func RegexpFindIndices

func RegexpFindIndices(s string, re *regexp.Regexp) []CapturingGroup

RegexpFindIndices finds all the indices of a regexp.

type DAG

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

DAG is the representation of a directed acyclic graph.

func NewDAG

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

NewDAG returns a new DAG.

func (DAG[K, V]) AddEdge

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

AddEdge adds a new edge.

func (DAG[K, V]) AddSimpleEdge

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

AddSimpleEdge adds a simple edge, without the DAGNode.

func (DAG[K, V]) Edges

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

Edges returns the children from an id.

func (DAG[K, V]) Node

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

Node returns a node from an id.

func (DAG[K, V]) TopologicalSort

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

TopologicalSort applies the topological sort algorithm on a DAG.

type DAGNode

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 (
	UnknownDirection Direction = iota
	Up
	Down
	Left
	Right

	UpLeft
	UpRight
	DownLeft
	DownRight
)

func (Direction) Rev

func (d Direction) Rev() Direction

Rev reverses the current direction.

func (Direction) String

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 Heap added in v1.0.1

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

Heap is a priority queue implementation.

func NewHeap added in v1.0.1

func NewHeap[T any](comparator func(a, b T) bool) Heap[T]

NewHeap creates a new Heap using a comparator.

func (*Heap[T]) IsEmpty added in v1.0.1

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

IsEmpty checks if the priority queue is empty.

func (*Heap[T]) Len added in v1.0.1

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

Len returns the number of elements in the priority queue.

func (*Heap[T]) Peek added in v1.0.1

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

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

func (*Heap[T]) Pop added in v1.0.1

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

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

func (*Heap[T]) Push added in v1.0.1

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

Push pushes a new item.

type Location

type Location struct {
	Pos Position
	Dir Direction
}

Location represents a given position and direction.

func NewLocation

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

NewLocation creates a new location.

func (Location) Move

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

Move moves in a given direction.

func (Location) Rev

func (l Location) Rev(moves int) Location

Rev moves in the reverse direction.

func (Location) Straight

func (l Location) Straight(moves int) Location

Straight moves in the current direction.

func (Location) String

func (l Location) String() string

String implements strings.Stringer.

func (Location) Turn

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

Turn turns left or right.

type Node

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

Node is a linked list node.

func NewNode

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

NewNode returns a new node.

func (*Node[T]) Head

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

Head returns the head.

func (*Node[T]) InsertAfter

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

InsertAfter insers a node after the current one.

func (*Node[T]) InsertHead

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

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

func (*Node[T]) InsertTail

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

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

func (*Node[T]) String

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

String implements strings.Stringer.

func (*Node[T]) Tail

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

Tail returns the tail.

type Pair added in v1.0.2

type Pair[A any, B any] struct {
	First  A
	Second B
}

type Position

type Position struct {
	Row int
	Col int
}

Position represents a given position (row/col)

func NewPosition

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 Submatch

type Submatch struct {
	Start int
	End   int
	// CapturingGroups is a list of optional capturing groups.
	// For example, `mul\((\d{1,3}),(\d{1,3})\)` contains 2 capturing groups.
	CapturingGroups []CapturingGroup
}

func RegexpFindSubmatches

func RegexpFindSubmatches(s string, re *regexp.Regexp) []Submatch

RegexpFindSubmatches find all submatches and related capturing groups.

Jump to

Keyboard shortcuts

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