intcode

package
v0.0.0-...-be83087 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package intcode implements the AoC 2019 Intcode language.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(input io.Reader) ([]int64, error)

Load will read an Intcode program in the standard format (comma-separated integers) from a stream.

func Run

func Run(prog, input []int64) (output, mem []int64)

Run executes an Intcode program with the given input, and returns the output and the resulting state of the computer memory.

Types

type Reader

type Reader interface {
	Read() int64
}

A Reader provides input capabilities to an Intcode computer.

type Solver

type Solver func([]int64) ([]int64, error)

Solver wraps a solution that wants an Intcode program in the standard format as input.

func (Solver) Solve

func (s Solver) Solve(input io.Reader) (out []string, err error)

Solve implements the Solver interface for an Intcode-based problem.

type SolverS

type SolverS func([]int64) ([]string, error)

SolverS wraps a solution that wants an Intcode program in the standard format as input, outputting arbitrary strings.

func (SolverS) Solve

func (s SolverS) Solve(input io.Reader) ([]string, error)

Solve implements the Solver interface for an Intcode-based problem generating non-numeric output.

type VM

type VM struct {
	Stdin  Reader
	Stdout Writer
	// contains filtered or unexported fields
}

A VM represents the state of an Intcode computer.

func (*VM) Dump

func (vm *VM) Dump() []int64

Dump returns a copy of the computer's memory.

func (*VM) Load

func (vm *VM) Load(p []int64)

Load resets the computer and initializes its memory to be a copy of the program.

func (*VM) Mem

func (vm *VM) Mem(offset int) *int64

Mem returns a pointer to the specified memory cell of the computer.

func (*VM) Reset

func (vm *VM) Reset()

Reset resets the IP and base pointers, but not the memory.

func (*VM) Run

func (vm *VM) Run(in []int64) []int64

Run executes the current program until it halts, reading input from a slice. All output generated by the program will be returned in the output slice.

func (*VM) Use

func (vm *VM) Use(p []int64)

Use resets the computer and uses the program directly as its memory.

func (*VM) Walk

func (vm *VM) Walk(token *WalkToken) bool

Walk executes the current program up to the first halt, input or output instruction. For the initial call, pass in an empty walk token (the zero value). If the function returns false, the computer has halted (and the token will be empty). Otherwise, the computer is requesting either input or output: inspect the walk token to learn which, and to either provide the input or deal with the output, as required. Then call Walk again with the same token to continue operation. This is intended for coöperative multitasking or other complex interleaving of Intcode operation with surrounding logic.

type WalkToken

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

WalkToken is used for holding invocation state when running Intcode via the Walk() API.

func (*WalkToken) IsEmpty

func (t *WalkToken) IsEmpty() bool

IsEmpty returns true if the walk token is empty (requests no input or contains no output).

func (*WalkToken) IsInput

func (t *WalkToken) IsInput() bool

IsInput returns true if the walk token requests input from the host.

func (*WalkToken) IsOutput

func (t *WalkToken) IsOutput() bool

IsOutput returns true if the walk token provides output to the host.

func (*WalkToken) ProvideInput

func (t *WalkToken) ProvideInput(val int64)

ProvideInput is used to set the input on an input-requesting walk token.

func (*WalkToken) ReadOutput

func (t *WalkToken) ReadOutput() int64

ReadOutput returns the output contained in this output-providing walk token.

func (*WalkToken) String

func (t *WalkToken) String() string

type Writer

type Writer interface {
	Write(val int64)
}

A Writer provides output capabilities to an Intcode computer.

Jump to

Keyboard shortcuts

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