aoc

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2023 License: MIT Imports: 15 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

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 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

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 Input

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

func NewInput

func NewInput(content string) Input

func (Input) Content

func (i Input) Content() string

func (Input) Delimiter added in v0.1.3

func (i Input) Delimiter(d string) Input

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

func (Input) ToIntSlice

func (i Input) ToIntSlice() []int

func (Input) ToStringSlice

func (i Input) ToStringSlice() []String

type Point added in v0.1.3

type Point struct {
	X int
	Y int
	Z int
}

func (Point) Add added in v0.1.3

func (p Point) Add(o Point) Point

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 west to east, and Y from north to south.

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 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) 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 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

func (String) Atoi added in v0.2.0

func (s String) Atoi() int

func (String) From added in v0.2.0

func (s String) From(i int) String

func (String) Split added in v0.2.0

func (s String) Split() []String

func (String) SplitAtoi added in v0.2.0

func (s String) SplitAtoi() []int

func (String) SplitOn added in v0.3.0

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

func (String) SplitOnS added in v1.2.0

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

func (String) SplitS added in v1.1.0

func (s String) SplitS() []string

Directories

Path Synopsis
cmd
aoc

Jump to

Keyboard shortcuts

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