aoc

package module
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 19 Imported by: 2

README

AoC golang utilities

This package provides both a CLI tool to generate a default AoC project, and a library to ease the solution writing.

Generating an AoC project

Installation
$ go install github.com/erdnaxeli/adventofcode/aoc/cmd/aoc@latest
Creating a new project
$ mkdir adventofcode2023
$ aoc github.com/<username>/adventofcode2023

This command will generate the boilerplate for a project in the current directory. It generates the following files:

  • main.go: a main() function with all the wiring to run the project
  • day01.go: the solutions for the day 1:
    • func day1p1(input Input) string: the solution for the part 1 of the day 1
    • func day1p2(input Input) string: the solution for the part 2 of the day 1
  • and similar files for all the other days
Usage

To use your project, you have to implement the solution in the corresponding file, then you run it like this:

$ # firt export your adventofcode.com session cookie
$ export AOC_SESSION=...
$ # then run your solution
$ go build
$ ./adventofcode2023 1 1  # run the solution for the part 1 of the day 1

The aoc library

This library provides many types, the two most important are:

  • Runner: the code that will get your puzzle input, run your solution, and submit the result. You should not have to deal with it as the CLI utility create a main.go file with everything set up.
  • Input: represent your puzzle input.

The Runner object wants a type implementing the Solver interface. This interface defines methods dayXpY() for each day and each part.

Every method dayXpY() takes a single parameter input Input which is the puzzle input. The type Input provides many method to parse the input. See the type documentation for more details.

Every method must return a single string. If the string is empty, the Runner assumes the solution is not implemented.

Have fun!

Roadmap

Here is a list of thing I found missing when doing AoC exercices:

  • Input.ToMatrix(): it should return a slice of slice of strings.
  • a whole module to manage interval: intersect them, substract them, and so on.

Documentation

Index

Constants

View Source
const URL = "https://adventofcode.com"

Variables

View Source
var SPACE_REGEX = regexp.MustCompile(`\s+`)

Functions

func AbsI added in v0.1.3

func AbsI(x int) int

AbsI is exactly like math.Abs but for integers.

func AllCombinations added in v1.14.0

func AllCombinations[S ~[]E, E any](s S) iter.Seq[[]E]

Allcombinations returns an iterator over all combinations of elements in s.

func Atoi added in v0.1.3

func Atoi(s string) int

Atoi is exactly like strconv.Atoi but does not return an error.

Instead it panics if anything goes wrong.

func Combinations added in v1.14.0

func Combinations[S ~[]E, E any](s S) iter.Seq[[]E]

Combinations returns an iterator over all two sized combinations of elements in s.

func FirstMaxIndex added in v1.6.0

func FirstMaxIndex(s []int) (int, int)

FirstMaxIndex returns the index of the first max value found, and the value.

func FirstMinIndex added in v1.6.0

func FirstMinIndex(s []int) (int, int)

FirstMaxIndex returns the index of the first min value found, and the value.

func MultSlice added in v1.11.0

func MultSlice(s []int) int

func MultiInput added in v1.10.0

func MultiInput[R1, R2 any](i Input, parseInput1 func(i Input) R1, parseInput2 func(i Input) R2) (R1, R2)

MultiInput splits and parse the input as two differents input separated by an empty line.

func ResultF64 added in v1.2.0

func ResultF64(f float64) string

func ResultI added in v0.1.3

func ResultI(i int) string

func ResultS added in v0.1.3

func ResultS(s []string) string

func ResultSI added in v0.1.3

func ResultSI(s []int) string

func SumSlice added in v1.11.0

func SumSlice(s []int) int

Types

type Cache

type Cache interface {
	GetInput(year int, day int, part int) Input
	StoreInput(year int, day int, part int, input Input) error
}

type Client

type Client interface {
	GetInput(year int, day int, part int) (Input, error)
	SendSolution(year int, day int, part int, solution string) error
}

type Counter added in v0.4.0

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

func NewCounter added in v0.4.0

func NewCounter(s String) Counter

func (Counter) Get added in v0.4.0

func (c Counter) Get(k rune) int

func (Counter) GetKeys added in v0.4.0

func (c Counter) GetKeys() []CounterKey

type CounterKey added in v0.4.0

type CounterKey struct {
	Key   rune
	Count int
}

type DefaultCache

type DefaultCache struct{}

func NewDefaultCache

func NewDefaultCache() DefaultCache

func (DefaultCache) GetInput

func (c DefaultCache) GetInput(year int, day int, part int) Input

func (DefaultCache) StoreInput added in v0.1.3

func (c DefaultCache) StoreInput(year int, day int, part int, input Input) error

type DefaultClient

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

func NewDefaultClient

func NewDefaultClient(session string) (DefaultClient, error)

func (DefaultClient) GetInput

func (c DefaultClient) GetInput(year int, day int, part int) (Input, error)

func (DefaultClient) SendSolution

func (c DefaultClient) SendSolution(year int, day int, part int, solution string) error

type Grid added in v1.10.0

type Grid[E any] struct {
	// contains filtered or unexported fields
}

Grid represent a grid.

It expects to be manipulated using Point objects to address positions in the grid.

func NewGridFromPoints added in v1.14.0

func NewGridFromPoints[E any](points []Point, empty E, notEmpty E) Grid[E]

func (Grid[E]) Around added in v1.10.0

func (g Grid[E]) Around(p Point) iter.Seq2[Point, E]

Around return an iterator over all the 8 points around a position.

If the position is on an edge of the grid, fewer than 8 points can be returned.

func (Grid[E]) At added in v1.10.0

func (g Grid[E]) At(p Point) E

func (Grid[E]) AtXY added in v1.11.0

func (g Grid[E]) AtXY(x int, y int) E

func (Grid[E]) IterAllColumn added in v1.11.0

func (g Grid[E]) IterAllColumn(y int) iter.Seq2[Point, E]

func (Grid[E]) IterAllLine added in v1.12.0

func (g Grid[E]) IterAllLine(x int) iter.Seq2[Point, E]

func (Grid[E]) IterColumn added in v1.11.0

func (g Grid[E]) IterColumn(y int, xMin int, xMax int) iter.Seq2[Point, E]

func (Grid[E]) IterLine added in v1.12.0

func (g Grid[E]) IterLine(x int, yMin int, yMax int) iter.Seq2[Point, E]

func (Grid[E]) LenX added in v1.10.0

func (g Grid[E]) LenX() int

func (Grid[E]) LenY added in v1.10.0

func (g Grid[E]) LenY() int

func (Grid[E]) MaxX added in v1.10.0

func (g Grid[E]) MaxX() int

func (Grid[E]) MaxY added in v1.10.0

func (g Grid[E]) MaxY() int

func (Grid[E]) Points added in v1.10.0

func (g Grid[E]) Points() iter.Seq2[Point, E]

Points return an iterator over all points on the grid.

func (Grid[E]) Set added in v1.10.0

func (g Grid[E]) Set(p Point, v E)

Set sets a value on the grid

It actually mutates the grid even if the receiver is not on a pointer because it involves slices on the inside, which use pointers.

func (Grid[E]) String added in v1.14.0

func (g Grid[E]) String() string

type Input

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

func NewInput

func NewInput(content string) Input

NewInput return a new Input object.

By default the input is configured in multiline mode, without any delimiter, which is equivalent to using a "\n" delimiter.

func (Input) Content

func (i Input) Content() string

func (Input) Delimiter added in v0.1.3

func (i Input) Delimiter(d string) Input

Delimiter sets the delimiter used by methods to split the input.

If the delimiter set is different than "" it overwrite the multiline configuration.

It returns the same input, so methods can be chained.

func (Input) Lines added in v1.10.0

func (i Input) Lines() iter.Seq[String]

func (Input) Rotate added in v0.3.0

func (i Input) Rotate() []String

func (Input) SingleLine added in v0.1.3

func (i Input) SingleLine() Input

SingleLine set the multiline mode to false.

In multiline mode, the input is split on "\n". In singleline, the input is split on each char.

If a delimiter is set, the multiline mode is ignored.

func (Input) ToGrid added in v1.9.0

func (i Input) ToGrid() Grid[byte]

ToGrid parses the input as a grid.

x is the axe from top to bottom, y is the axis from left to right. (0, 0) is the top left point.

func (Input) ToGridS added in v1.11.0

func (i Input) ToGridS() Grid[String]

func (Input) ToIntSlice

func (i Input) ToIntSlice() []int

ToIntSlice parse the input as a slice of int.

If a delimiter is set, it is used to split the input. Else in multiline mode the input is split on each line. Else the input is plit on each char.

If any part cannot be parsed as an int, it panics.

func (Input) ToPoints added in v1.14.0

func (i Input) ToPoints() []Point

ToPoints parses the input as a list of points, one by line.

The coordinates must be separated by commas.

func (Input) ToRanges added in v1.10.0

func (i Input) ToRanges(inclusive bool) []Range

func (Input) ToStringSlice

func (i Input) ToStringSlice() []String

ToStringSlice parse the input as a slice of String.

If a delimiter is set, it is used to split the input. Else in multiline mode the input is split on each line. Else the input is plit on each char.

type Point added in v0.1.3

type Point struct {
	X int
	Y int
	Z int
}

func NewPointXY added in v1.13.0

func NewPointXY(x int, y int) Point

func NewPointXYZ added in v1.13.0

func NewPointXYZ(x int, y int, z int) Point

func (Point) Add added in v0.1.3

func (p Point) Add(o Point) Point

func (Point) Distance added in v1.13.0

func (p1 Point) Distance(p2 Point) float64

func (Point) MoveEast added in v1.4.0

func (p Point) MoveEast() Point

func (Point) MoveNorth added in v1.4.0

func (p Point) MoveNorth() Point

MoveNorth return a new point one step to the north.

It supposes a grid where X goes from north to south, and Y from east to west.

func (Point) MoveSouth added in v1.4.0

func (p Point) MoveSouth() Point

func (Point) MoveWest added in v1.4.0

func (p Point) MoveWest() Point

func (Point) MultI added in v0.1.3

func (p Point) MultI(i int) Point

func (Point) RotateLeftZ added in v0.1.3

func (p Point) RotateLeftZ() Point

func (Point) RotateRightZ added in v0.1.3

func (p Point) RotateRightZ() Point

type Range added in v1.10.0

type Range struct {
	Start int
	End   int
	// contains filtered or unexported fields
}

func CombineRanges added in v1.10.0

func CombineRanges(ranges []Range) []Range

CombineRanges takes a list of ranges and return an equivalent list of non overlapping ranges.

func NewRange added in v1.10.0

func NewRange(start int, end int, inclusive bool) Range

NewRange creates a new Range object.

If the inclusive parameter is true, the start and the end are part of the range, else they are not.

func (Range) Contains added in v1.10.0

func (r Range) Contains(v int) bool

func (Range) Len added in v1.10.0

func (r Range) Len() int

type Runner

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

func NewDefaultRunner

func NewDefaultRunner(year int, solver Solver) Runner

Return a new Runner object with default cache and client.

This is the main constructor you should use.

func NewRunner

func NewRunner(year int, cache Cache, client Client, solver Solver) Runner

Return a new Runner.

Unless you want to inject specific implementation for the cache and the client you should use NewDefaultRunner.

func (Runner) Benchmark added in v1.7.0

func (r Runner) Benchmark()

Benchmark runs a benchmark over all implemented solutions, and print the result.

func (Runner) GetInput added in v1.14.0

func (r Runner) GetInput(day int, part int) Input

func (Runner) Run

func (r Runner) Run(day int, part int)

Run runs the solution for a given day and part

func (Runner) RunCli

func (r Runner) RunCli()

RunCli runs the solution for a given day and part.

It reads the day and the part for the executable arguments. The first argument is the day, the second is the part.

type Set added in v1.14.0

type Set[E comparable] map[E]struct{}

func NewSet added in v1.14.0

func NewSet[E comparable](elems ...E) Set[E]

func NewSetFromIter added in v1.14.0

func NewSetFromIter[E comparable](elems iter.Seq[E]) Set[E]

func (Set[E]) Add added in v1.14.0

func (s Set[E]) Add(e E)

func (Set[E]) Contains added in v1.14.0

func (s Set[E]) Contains(e E) bool

func (Set[E]) First added in v1.14.0

func (s Set[E]) First() E

First actually returns an arbitrary element of the set.

func (Set[E]) Len added in v1.14.0

func (s Set[E]) Len() int

func (Set[E]) Values added in v1.14.0

func (s Set[E]) Values() iter.Seq[E]

type Shape added in v1.14.0

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

func NewShape added in v1.14.0

func NewShape(points Set[Point]) Shape

NewShape creates a shape with the given points.

The points are expected to be side by side, either horizontally or vertically, and the shape is expected to be closed.

func (Shape) Contains added in v1.14.0

func (s Shape) Contains(p Point) bool

Contains returns true if the Shape s contains the Point p.

If p is part of the points defining the shape, it is considered as contained.

type Solver

type Solver interface {
	Day1p1(Input) string
	Day1p2(Input) string
	Day2p1(Input) string
	Day2p2(Input) string
	Day3p1(Input) string
	Day3p2(Input) string
	Day4p1(Input) string
	Day4p2(Input) string
	Day5p1(Input) string
	Day5p2(Input) string
	Day6p1(Input) string
	Day6p2(Input) string
	Day7p1(Input) string
	Day7p2(Input) string
	Day8p1(Input) string
	Day8p2(Input) string
	Day9p1(Input) string
	Day9p2(Input) string
	Day10p1(Input) string
	Day10p2(Input) string
	Day11p1(Input) string
	Day11p2(Input) string
	Day12p1(Input) string
	Day12p2(Input) string
	Day13p1(Input) string
	Day13p2(Input) string
	Day14p1(Input) string
	Day14p2(Input) string
	Day15p1(Input) string
	Day15p2(Input) string
	Day16p1(Input) string
	Day16p2(Input) string
	Day17p1(Input) string
	Day17p2(Input) string
	Day18p1(Input) string
	Day18p2(Input) string
	Day19p1(Input) string
	Day19p2(Input) string
	Day20p1(Input) string
	Day20p2(Input) string
	Day21p1(Input) string
	Day21p2(Input) string
	Day22p1(Input) string
	Day22p2(Input) string
	Day23p1(Input) string
	Day23p2(Input) string
	Day24p1(Input) string
	Day24p2(Input) string
	Day25p1(Input) string
	Day25p2(Input) string
}

An object implementing the solutions for all days.

A day method receives an Input object, and must return a string. If a day method returns an empty string, it will consider the solution is not implemented yet and it will not send it.

type String added in v0.2.0

type String string

func (String) At added in v0.2.0

func (s String) At(i int) byte

At return the char at the given position in the String.

func (String) Atoi added in v0.2.0

func (s String) Atoi() int

Atoi convert the String to an it.

It panics on error.

func (String) From added in v0.2.0

func (s String) From(i int) String

From return the String from the given char position to the end.

func (String) Split added in v0.2.0

func (s String) Split() []String

Split splits the String on spaces.

func (String) SplitAtoi added in v0.2.0

func (s String) SplitAtoi() []int

SplitAtoi splits the String on spaces and convert the results to integers.

func (String) SplitOn added in v0.3.0

func (s String) SplitOn(d string) []String

SplitOn splits the String using a given delimiter.

func (String) SplitOnAtoi added in v1.5.0

func (s String) SplitOnAtoi(d string) []int

SplitOn splits the String using a given delimiter and convert the results to integers.

func (String) SplitOnS added in v1.2.0

func (s String) SplitOnS(d string) []string

SplitOn splits the String using a given delimiter and convert the results to string.

func (String) SplitS added in v1.1.0

func (s String) SplitS() []string

SplitS splits the String on spaces.

func (String) ToIntSlice added in v1.6.0

func (s String) ToIntSlice() []int

ToIntSlice convert the String to a slice of int.

It considers each char as an element of the result slice.

Directories

Path Synopsis
cmd
aoc command

Jump to

Keyboard shortcuts

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