intcode

package
v0.0.0-...-a30599c Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instruction

type Instruction struct {
	// Op is the opcode for the action the instruction should perform.
	Op Op
	// Params is the list of parameters provided to the instruction.
	Params []Param
}

Instruction is a single instruction read from the memory of a VM.

func (Instruction) Get

func (inst Instruction) Get(vm *VM, i int) int

Get reads the value for a parameter of the instruction. It uses the parameter's mode to read from the correct location.

func (Instruction) Set

func (inst Instruction) Set(vm *VM, i int, value int)

Set updates a value in memory for a parameter of the instruction. It uses the parameter's mode to determine the correct location to write to.

type Op

type Op int

Op is an opcode for an Intcode instruction.

const (
	OpAdd             Op = 1
	OpMultiply        Op = 2
	OpInput           Op = 3
	OpOutput          Op = 4
	OpJumpIfTrue      Op = 5
	OpJumpIfFalse     Op = 6
	OpLessThan        Op = 7
	OpEquals          Op = 8
	OpSetRelativeBase Op = 9
	OpHalt            Op = 99
)

The supported opcodes for this Intcode interpreter.

type Param

type Param struct {
	// Value is the integer value of the parameter.
	Value int
	// Mode is the mode that should be used to interpret the value.
	Mode ParamMode
}

Param is a single parameter from an Intcode instruction.

type ParamMode

type ParamMode int

ParamMode represents how to use the value in an Intcode instruction parameter.

const (
	// ModePosition indicates the parameter's value refers to an absolute address
	// memory.
	ModePosition ParamMode = 0
	// ModeImmediate indicates the parameter's value should be used directly, and
	// does not correspond to another address in memory.
	ModeImmediate ParamMode = 1
	// ModeRelative indicates the parameter's value refers to an offset from the
	// VM's current relative base.
	ModeRelative ParamMode = 2
)

type VM

type VM struct {
	Memory []int
	Input  func() int
	Output chan int
	// contains filtered or unexported fields
}

VM is an Intcode virtual machine.

Each VM can run a single Intcode program.

func LoadFromString

func LoadFromString(s string) (*VM, error)

LoadFromString parses an Intcode program from a string and loads it into a new VM.

func NewVM

func NewVM(memory []int) *VM

NewVM creates an Intcode VM with the given memory values already set.

func (*VM) At

func (vm *VM) At(i int) int

At gets the integer at the given memory address.

func (*VM) AtOffset

func (vm *VM) AtOffset(i int) int

AtOffset gets the integer that is at the given offset from the current instruction pointer.

func (*VM) Clone

func (vm *VM) Clone() *VM

Clone creates a blank copy of the VM. This can be used to run the same program multiple times. The cloned VM has a copy of the internal state of the original. The clone has its own input and output channels.

func (*VM) Execute

func (vm *VM) Execute() error

Execute runs the loaded Intcode program to completion.

It only returns an error if the program is invalid in some way: either it uses an unknown instruction opcode or an instruction is malformed.

func (*VM) MustExecute

func (vm *VM) MustExecute()

MustExecute runs the loaded program like Execute, but it panics if there is an error running the program.

func (*VM) PipeTo

func (vm *VM) PipeTo(other *VM)

PipeTo continuously waits for output from this VM and provides each value as an input to `other`.

PipeTo blocks waiting for output, so it should be run on its own goroutine.

func (*VM) Set

func (vm *VM) Set(i int, value int)

Set assigns a new value to the given memory address.

func (*VM) SetInputChan

func (vm *VM) SetInputChan(input chan int)

SetInputChan sets a channel to provide input to the program.

func (*VM) SetInputFunc

func (vm *VM) SetInputFunc(input func() int)

SetInputFunc sets a function to provide input to the program when requested.

func (*VM) SetInputSeq

func (vm *VM) SetInputSeq(input []int)

SetInputSeq sets a sequence of values to provide one-by-one as input to the program.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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