aoc

package module
v0.0.0-...-675bdce Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 24 Imported by: 0

README

This contains quick and dirty utilities to help @maisem solve Advent of Code problems.

Documentation

Overview

Package aoc are quick & dirty utilities for helping Maisem solve Advent of Code problems. (forked from bradfitz/aoc)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsDiff

func AbsDiff[T Number](x, y T) T

AbsDiff returns the absolute difference between x and y.

func AnyKey

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

AnyKey returns any key from the map. It panics if the map is empty.

func Digit

func Digit(r rune) int

Digit returns the digit value of the rune.

func Digits

func Digits(line string) []int

Digits returns the individual digits of the string.

func Extrapolate

func Extrapolate[T Number](x []T, forward bool) (y T)

Extrapolate returns the next value in the sequence x. If forward is true, it extrapolates the next value, otherwise it extrapolates the previous value in the sequence.

func FloodFill

func FloodFill[T comparable](grid Grid[T], start Pt, empty, fill T) int

FloodFill fills all empty cells reachable from start with fill.

func Fold

func Fold[T any, R any](in []T, f func(R, T) R, defVal R) R

Fold folds the input into a single value.

func GCD

func GCD(a, b int) int

GCD returns the greatest common divisor of the integers.

func InitMap

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

InitMap initializes a map if it is nil.

func Int

func Int(s string) int

Int returns the int value of the string.

func Ints

func Ints(s ...string) []int

Ints returns the int values of the strings.

func LCM

func LCM(integers ...int) int

LCM returns the least common multiple of the integers.

func MustDo

func MustDo(err error)

MustDo panics if err is non-nil.

func MustGet

func MustGet[T any](v T, err error) T

MustGet returns v as is. It panics if err is non-nil.

func Or

func Or[T any](list ...T) T

Or returns the first non-zero value in list.

func Parallel

func Parallel[I, O any](in []I, f func(I) O) []O

Parallel runs f in parallel for each value in in.

func ParallelMapFold

func ParallelMapFold[A, B, C any](in []A, f func(A) B, f2 func(C, B) C, defVal C) C

ParallelMapFold maps the input to a new slice, then folds the result. It is equivalent to Fold(Parallel(in, f), f2, defVal).

func ParseBinary

func ParseBinary(in string) int64

ParseBinary parses a binary string.

func PolygonArea

func PolygonArea(pts []Pt) int

PolygonArea returns the area of the polygon defined by the points. It assumes the points are in clockwise order, and uses the shoelace formula.

func PolygonBoundedPoints

func PolygonBoundedPoints(pts []Pt) int

PolygonBoundedPoints returns the number of points with integer coordinates inside the polygon defined by the points.

func PolygonPermimeter

func PolygonPermimeter(pts []Pt) int

PolygonPerimeter returns the perimeter of the polygon defined by the points.

func Run

func Run(year int, src []byte, slvr any)

func SolveQuad

func SolveQuad[T Number](a, b, c T) (float64, float64)

SolveQuad returns the roots of the quadratic equation ax^2 + bx + c = 0.

func Sum

func Sum[T Number](nums ...T) T

Sum returns the sum of the numbers.

func TrimPrefix

func TrimPrefix(s, prefix string) string

TrimPrefix trims the prefix from s. It panics if the prefix is not found.

Types

type Direction

type Direction int
const (
	Up Direction = iota
	Right
	Down
	Left
)

func (Direction) String

func (d Direction) String() string

func (Direction) Turn

func (d Direction) Turn(right bool) Direction

type Edge

type Edge[T comparable] struct {
	A, B T
}

type Graph

type Graph[K comparable] struct {
	Nodes map[K]bool
	Edges map[K]map[K]int
}

func (*Graph[K]) AddEdge

func (g *Graph[K]) AddEdge(a, b K, dist int)

func (*Graph[K]) AddNode

func (g *Graph[K]) AddNode(a K)

func (*Graph[K]) AllShortestPaths

func (g *Graph[K]) AllShortestPaths() map[Edge[K]]int

func (*Graph[K]) Clone

func (g *Graph[K]) Clone() *Graph[K]

func (*Graph[K]) Collapse

func (g *Graph[K]) Collapse()

Collapse collapses the graph by removing any nodes with only two edges and merging the two edges into one.

func (Graph[K]) LongestPath

func (g Graph[K]) LongestPath(start, end K) (rp int, ok bool)

LongestPath returns the size of the longest path from start to end.

func (*Graph[T]) MinCut

func (g *Graph[T]) MinCut() []Edge[T]

MinCut calculates the minimum cut of a graph using the Stoer–Wagner algorithm. It returns a list of edges that make up the cut.

func (*Graph[K]) NumPaths

func (g *Graph[K]) NumPaths(start, end K) int

func (*Graph[K]) NumPathsWithRestriction

func (g *Graph[K]) NumPathsWithRestriction(start, end K, canVisit func(x K, alreadyVisited map[K]int) bool) int

func (*Graph[K]) ReachableNodes

func (g *Graph[K]) ReachableNodes(a K) map[K]bool

func (*Graph[K]) RemoveEdge

func (g *Graph[K]) RemoveEdge(a, b K)

func (*Graph[K]) RemoveNode

func (g *Graph[K]) RemoveNode(a K)

type Grid

type Grid[T any] [][]T

func MakeGrid

func MakeGrid[T any](x, y int) Grid[T]

func (Grid[T]) At

func (g Grid[T]) At(p Pt) T

func (Grid[T]) AtOk

func (g Grid[T]) AtOk(p Pt) (T, bool)

func (Grid[T]) EdgePaths

func (g Grid[T]) EdgePaths() []Path

func (Grid[T]) Hash

func (g Grid[T]) Hash() deephash.Sum

func (Grid[T]) Move

func (g Grid[T]) Move(p Path) (Path, bool)

func (Grid[T]) RotateCounterClockwise

func (g Grid[T]) RotateCounterClockwise() Grid[T]

func (Grid[T]) RotateCounterClockwiseInto

func (g Grid[T]) RotateCounterClockwiseInto(out [][]T)

func (Grid[T]) Set

func (g Grid[T]) Set(p Pt, v T)

func (Grid[T]) Size

func (g Grid[T]) Size() Pt

func (Grid[T]) ToGraph

func (grid Grid[T]) ToGraph(start Pt, allowDiagonals bool, disallowed func(T) bool) Graph[Pt]

ToGraph converts the grid into a graph. If allowDiagonals is true, then diagonal neighbors are included. If disallowed is not nil, it is additionally called on each cell, and if it returns true, that cell is not included in the graph.

func (Grid[T]) Transpose

func (g Grid[T]) Transpose() Grid[T]

func (Grid[T]) TransposeInto

func (g Grid[T]) TransposeInto(out Grid[T])

type Number

type Number interface {
	constraints.Float | constraints.Integer
}

Number is a type that can be used in math functions.

type PQ

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

func MaxQueue

func MaxQueue[T any]() *PQ[T]

func MinQueue

func MinQueue[T any]() *PQ[T]

func (*PQ[T]) Len

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

func (*PQ[T]) Peek

func (pq *PQ[T]) Peek() *PQI[T]

func (*PQ[T]) Pop

func (pq *PQ[T]) Pop() *PQI[T]

func (*PQ[T]) Push

func (pq *PQ[T]) Push(v *PQI[T])

func (*PQ[T]) Update

func (pq *PQ[T]) Update(v *PQI[T])

type PQI

type PQI[T any] struct {
	V T
	P int
	// contains filtered or unexported fields
}

func (*PQI[T]) Index

func (i *PQI[T]) Index() int

func (*PQI[T]) String

func (i *PQI[T]) String() string

type Path

type Path struct {
	Pt  Pt
	Dir Direction
}

Path is a point and a direction.

type Pt

type Pt = Pt2[int]

func StandardizePt

func StandardizePt(p, size Pt) Pt

type Pt2

type Pt2[T constraints.Signed] struct {
	X, Y T
}

func (Pt2[T]) ForImmediateNeighbors

func (p Pt2[T]) ForImmediateNeighbors(f func(Pt2[T]) (keepGoing bool))

func (Pt2[T]) ForNeighbors

func (p Pt2[T]) ForNeighbors(f func(Pt2[T]) (keepGoing bool))

func (Pt2[T]) MDist

func (a Pt2[T]) MDist(b Pt2[T]) T

MDist returns the manhattan distance between a and b.

func (Pt2[T]) Toward

func (p Pt2[T]) Toward(b Pt2[T]) Pt2[T]

Toward returns a point moving from p to b in max 1 step in the X and/or Y direction.

type Puzzle

type Puzzle struct {
	SampleMode bool
	// contains filtered or unexported fields
}

func (*Puzzle) Debug

func (p *Puzzle) Debug(v ...any)

func (*Puzzle) Debugf

func (p *Puzzle) Debugf(format string, args ...any)

func (*Puzzle) Description

func (p *Puzzle) Description() []byte

func (*Puzzle) ForLines

func (p *Puzzle) ForLines(onLine func(line string))

ForLines calls onLine for each line of input. The y value is the row number, starting with 0.

func (*Puzzle) ForLinesY

func (p *Puzzle) ForLinesY(onLine func(int, string))

func (*Puzzle) Input

func (p *Puzzle) Input() []byte

func (*Puzzle) Sample

func (p *Puzzle) Sample() sample

func (*Puzzle) Scanner

func (p *Puzzle) Scanner() *bufio.Scanner

type Queue

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

func NewQueue

func NewQueue[T any](in ...T) Queue[T]

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (T, bool)

func (*Queue[T]) Push

func (q *Queue[T]) Push(v T)

func (*Queue[T]) While

func (q *Queue[T]) While(f func(T) bool)

type Segment

type Segment struct {
	A, B Pt
}

Segment is a line segment between two points.

type Stack

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

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

func (*Stack[T]) Push

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

func (*Stack[T]) While

func (s *Stack[T]) While(f func(T) bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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