puzzle

package
v0.0.0-...-f96c37d Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package puzzle provides the interface for a puzzle solver and a function to run a solver. Also contains some utils. See the utils package for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FetchAndSaveInput

func FetchAndSaveInput(session string, filePath string, year int, day int) (string, error)

FetchAndSaveInput fetches the input for a particular year and day of an Advent of Code puzzle and saves it to a file before returning it.

func FetchInput

func FetchInput(session string, year int, day int) (string, error)

FetchInput gets an Advent of Code input directly from the site for a particular year and day. The session parameter must contain the value of the session cookie that will be used to perform the request.

func SaveInput

func SaveInput(filePath string, input string) error

SaveInput saves an input to a given filepath. The file path specified as a parameter must be the complete path to the file and not a directory.

func SubmitSolution

func SubmitSolution(session string, year int, day int, part int, solution string) (string, error)

SubmitSolution submits and Advent of Code solution

Types

type PerformanceMetrics

type PerformanceMetrics struct {
	InputReadingTime    time.Duration
	InputProcessingTime time.Duration
	Part1Time           time.Duration
	Part2Time           time.Duration
}

PerformanceMetrics has performance metrics related to the execution of a puzzle

type Solution

type Solution struct {
	// Part1Output is the output of part 1 as a string
	Part1Output string
	// Part2Output is the output of part 2 as a string
	Part2Output string
}

Solution represents a solution for an Advent of Code puzzle. A solution includes the outputs for both parts of the puzzle

type Solver

type Solver interface {

	// ProcessInput should process the input read from the file.
	// The argument is the whole contents of the file as a string.
	// It is up to the implementation how the result of the processing should be saved.
	// For Advent of Code, an instance variable that can be shared between part 1 and part 2 is often
	// a good way of saving the input processing.
	ProcessInput(fileContent string) error

	// Part1 should return the solution for part 1 of the puzzle
	Part1() (string, error)

	// Part2 should return the solution for part 2 of the puzzle
	Part2() (string, error)
}

Solver is an Advent of Code puzzle solver.

type SolverRunner

type SolverRunner struct {
	// Input contains a reader to the puzzle input
	Input io.Reader
	// Solver is the solver that can solve the puzzle
	Solver Solver
	// PerformanceMetrics contains stats about input reading/processing and the
	// execution of the solver
	PerformanceMetrics
	// Solution contains the output for both parts of the puzzle
	Solution
}

SolverRunner runs a solver with the input coming from a reader. It runs the solver by executing solver.ProcessInput(), solver.Part1() and solver.Part2() sequentially.

func NewSolverRunnerFromFile

func NewSolverRunnerFromFile(filepath string, solver Solver) (*SolverRunner, error)

NewSolverRunnerFromFile returns a new SolverRunner with the reader set to the contents of the file.

func (*SolverRunner) PrintSolutionAndStats

func (sr *SolverRunner) PrintSolutionAndStats(w io.Writer) error

func (*SolverRunner) Run

func (sr *SolverRunner) Run() (*Solution, error)

RunSolver takes a solver for a Puzzle and runs it, taking care of reading the input, processing it and running both parts. Also benchmarks the time for input processing and solving each part of the puzzle.

Directories

Path Synopsis
Package utils contains some util functions and types for Advent Of Code.
Package utils contains some util functions and types for Advent Of Code.

Jump to

Keyboard shortcuts

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