aoc

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 7 Imported by: 0

README

aoc go module

Helper utility for adventofcode challenges

Installation

go get github.com/fercsi/aoc

Usage

Example of how to import and use it:

import "github.com/fercsi/aoc"

func main() {
    aoc.ReadInputIntList()
    ...
}

Dijkstra module:

import "github.com/fercsi/aoc/dijkstra"

func main() {
    graph := dijkstra.Graph{}
    ...
    distances := dijkstra.Dijkstra(graph, 0, len(Graph))
}

Features

Handling input data
  • func GetRunType() string
  • func ReadInput() []string
  • func ReadInputLine() string
  • func ReadInputBytes() [][]byte
  • func ReadInputSplit(delimiter string) [][]string
  • func ReadInputLineSplit(delimiter string) []string
  • func ReadInputInt() []int
  • func ReadInputIntList() [][]int
  • func ReadInputLineIntList() []int
  • func ParseSplit(input []string, delimiter string) [][]string
  • func ParseLineSplit(input string, delimiter string) []string
  • func ParseInt(input []string) []int
  • func ParseIntList(input []string) [][]int
  • func ParseLineIntList(line string) []int
Show intermediate and final results
  • func ShowResult(values ...any)
  • func ShowBytesCondensed(area [][]byte)
  • func ShowIntsCondensed(area [][]int)
  • func ShowIntsCsv(area [][]int)
Manage coordinates
  • type Coord struct
  • func (coord Coord) Up() Coord
  • func (coord Coord) Down() Coord
  • func (coord Coord) Left() Coord
  • func (coord Coord) Right() Coord
  • func (coord Coord) Neighbours(width, height int) []Coord
  • func (coord Coord) AllNeighbours() []Coord
  • func (coord Coord) Neighbours8(width, height int) []Coord
  • func (coord Coord) AllNeighbours8() []Coord
  • func Distance(coord1, coord2 Coord) int
  • func AtCoord[E any](area [][]E, coord Coord) E
  • func AtCoordUnlimited[E any](area [][]E, coord Coord, defaultValue E) E
  • func SetAtCoord[E any](area [][]E, coord Coord, val E)
  • func Boundaries(coords []Coord) (Coord, Coord)
  • func GridCoords(tl, br Coord) iter.Seq[Coord]
Save results as an image
  • func SaveBytesImage(filename string, image [][]byte, colors map[byte]int)
Further tools
  • func Sum[E addable](s []E) E
  • func SumFunc[S any, E addable](s []S, f func(e S) E) E
  • func SumSeq[E addable](seq iter.Seq[E]) E
  • func Prod[E mulable](s []E) E
  • func ProdFunc[S any, E mulable](s []S, f func(e S) E) E
  • func ProdSeq[E mulable](seq iter.Seq[E]) E
  • func CountIf[S any](s []S, f func(S) bool) int
  • func Grid2D(startx, endx, starty, endy int) iter.Seq2[int, int]
  • func Transpose(lines []string) []string
Dijkstra algprithm
  • type Edge struct
  • type Graph map[int][]Edge
  • func Dijkstra(graph Graph, start int, n int) []int

Documentation

Testing

How to run tests:

go test

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Package aoc provides utility functions and types to assist with solving Advent of Code puzzles and other similar algorithmic challenges.

It includes:

  • Input reading and parsing utilities
  • Simple output formatting (for 2D arrays)
  • Coordinate manipulation (type Coord)
  • Saving 2D byte arrays as images

Example usage:

input := aoc.ReadInputInt()
sum := 0
for _, value := range input {
    sum += value
}

coord := aoc.Coord{X: 0, Y: 0}
upper := coord.Up()

caveMap := aoc.ReadInputBytes()
aoc.SaveBytesImage("output.ppm", caveMap, map[byte]int{
    '.': 0xFFFFFF,
    '#': 0x000000,
})

This package aims to provide fast and simple helpers for common Advent of Code tasks, minimizing boilerplate code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtCoord added in v1.1.0

func AtCoord[E any](area [][]E, coord Coord) E

AtCoord returns the value found at coord of the given area

func AtCoordUnlimited added in v1.1.0

func AtCoordUnlimited[E any](area [][]E, coord Coord, defaultValue E) E

AtCoordUnlimited returns the value found at coordinate of the given area and default value if the area does not contain the coordinate

func Boundaries added in v1.3.0

func Boundaries(coords []Coord) (Coord, Coord)

Boundaries returns the top left corner and the bottom right corner of the smallestboundary box containing each coordinates

func CountIf added in v1.6.0

func CountIf[S any](s []S, f func(S) bool) int

CountIf returns the number of elements in s which pass the f test funvtion

func Distance added in v1.1.0

func Distance(coord1, coord2 Coord) int

Distance returns the Manhattan-distance between two coordinates

func GetRunType

func GetRunType() string

func Grid2D added in v1.5.0

func Grid2D(startx, endx, starty, endy int) iter.Seq2[int, int]

Grid2D returns an iterator of x, y values for each point in a 2D rectangle defined by startx, endx, starty, endy values (ends not inclusive).

func GridCoords added in v1.4.0

func GridCoords(tl, br Coord) iter.Seq[Coord]

GridCoords returns an iterator of Coord values for each point in a 2D rectangle defined by its top-left and bottom-right coordinates (inclusive).

func ParseInt added in v1.9.0

func ParseInt(input []string) []int

func ParseIntList added in v1.9.0

func ParseIntList(input []string) [][]int

func ParseLineIntList

func ParseLineIntList(line string) []int

func ParseLineSplit added in v1.9.0

func ParseLineSplit(input string, delimiter string) []string

func ParseSplit added in v1.9.0

func ParseSplit(input []string, delimiter string) [][]string

func Prod added in v1.8.0

func Prod[E mulable](s []E) E

Prod returns the product of all elements in the slice.

func ProdFunc added in v1.8.0

func ProdFunc[S any, E mulable](s []S, f func(e S) E) E

ProdFunc maps each element using f and returns the product of the results.

func ProdSeq added in v1.8.0

func ProdSeq[E mulable](seq iter.Seq[E]) E

Prod returns the product of all elements defined by seq.

func ReadInput

func ReadInput() []string

func ReadInputBytes

func ReadInputBytes() [][]byte

func ReadInputInt

func ReadInputInt() []int

func ReadInputIntList

func ReadInputIntList() [][]int

func ReadInputLine

func ReadInputLine() string

func ReadInputLineIntList

func ReadInputLineIntList() []int

func ReadInputLineSplit

func ReadInputLineSplit(delimiter string) []string

func ReadInputSplit

func ReadInputSplit(delimiter string) [][]string

func SaveBytesImage

func SaveBytesImage(filename string, image [][]byte, colors map[byte]int)

func SetAtCoord added in v1.4.0

func SetAtCoord[E any](area [][]E, coord Coord, val E)

SetAtCoord sets the value at coord of a 2D slice of elements

func ShowBytesCondensed

func ShowBytesCondensed(area [][]byte)

func ShowIntsCondensed

func ShowIntsCondensed(area [][]int)

func ShowIntsCsv

func ShowIntsCsv(area [][]int)

func ShowResult

func ShowResult(values ...any)

ShowResult prints each value in a numbered list format. The numbering continues across multiple calls.

func Sum added in v1.2.0

func Sum[E addable](s []E) E

Sum returns the sum of all elements in the slice.

func SumFunc added in v1.2.0

func SumFunc[S any, E addable](s []S, f func(e S) E) E

SumFunc maps each element using f and returns the sum of the results.

func SumSeq added in v1.2.0

func SumSeq[E addable](seq iter.Seq[E]) E

Sum returns the sum of all elements defined by seq.

func Transpose added in v1.9.0

func Transpose(lines []string) []string

Transpose returns the column-wise transpose of the given lines. Missing characters are filled with spaces, and trailing spaces are trimmed while leading spaces are preserved.

Types

type Coord

type Coord struct {
	X, Y int
}

Coord represents a 2D coordinate point

func (Coord) AllNeighbours added in v1.1.0

func (coord Coord) AllNeighbours() []Coord

AllNeighbours returns all the adjacent coordinates of the given coordinate

In contrast to simple Neighbours, it returns all adjacent coordinates

func (Coord) AllNeighbours8 added in v1.1.0

func (coord Coord) AllNeighbours8() []Coord

AllNeighbours8 returns all the adjacent and diagonal coordinates of the given coordinate

In contrast to simple Neighbours8, it returns all coordinates

func (Coord) Down

func (coord Coord) Down() Coord

Down returns the coordinate below the current one

func (Coord) Left

func (coord Coord) Left() Coord

Left returns the coordinate left to the current one

func (Coord) Neighbours added in v1.1.0

func (coord Coord) Neighbours(width, height int) []Coord

Neighbours returns the adjacent coordinates of the given coordinate

If an adjacent coordinate is outside the block defined by width and height, those coordinates are not returned

func (Coord) Neighbours8 added in v1.1.0

func (coord Coord) Neighbours8(width, height int) []Coord

Neighbours8 returns the adjacent and diagonal coordinates of the given coordinate

If a coordinate is outside the block defined by width and height, those coordinates are not returned

func (Coord) Right

func (coord Coord) Right() Coord

Right returns the coordinate righz to the current one

func (Coord) Up

func (coord Coord) Up() Coord

Up returns the coordinate above the current one

Directories

Path Synopsis
dijkstra module

Jump to

Keyboard shortcuts

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