intcode

package
v0.0.0-...-e707fe5 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package intcode implements an intcode computer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Computer

type Computer struct {
	Done   chan struct{}
	Input  chan int
	Output chan int
	Err    chan error
	// contains filtered or unexported fields
}

Computer is an opcode computer. Input is read through the Input channel, output from the Output channel; the "halt" instruction closes the Done channel, and any errors can be read from the Err channel.

func NewComputer

func NewComputer(opcodes []int) *Computer

NewComputer returns an opcode computer with its memory initalized to opcodes.

func (*Computer) StartProgram

func (c *Computer) StartProgram()

StartProgram starts a goroutine that executes the program in the memory of the computer. It is typically used like this:

Loop:
    for {
        select {
        case v := <-c.Output:
            // Do something with output.
        case <-c.Done:
            break Loop
        case err := <-c.Err:
            // Handle error.
        }
    }

func (*Computer) Value

func (c *Computer) Value(idx int) int

Value returns the value at position idx; if the index is out of range, extra memory is appended.

Jump to

Keyboard shortcuts

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