intcode-0.4.0.0: Advent of Code 2019 intcode interpreter
Copyright(c) Eric Mertens 2019
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellTrustworthy
LanguageHaskell2010

Intcode.Machine

Description

The module implements the representation of the intcode machine state.

The Machine type stores the initial memory image in an array and only stores changes to that initial image. This allows for more efficient comparisons of machine states for equality when there are few changes to memory.

This implementation of the machine supports negative memory addresses. These are defined not to be used in the Advent of Code problems.

This implementation stores machine-sized Int values in memory.

Synopsis

Machine state

data Machine Source #

Machine state is comprised of the program counter, relative base pointer, and memory.

  • Interact with registers using: jmp, addRelBase
  • Interact with memory using: (!), set
  • Build new machines with: new

Updates to memory are stored separately from the initial values which can enable equality comparisons to be relatively efficient. This efficiency comes from being able to compare the inital memory using only pointer equality when two machines are created by the same call to new.

Constructors

Machine 

Fields

Instances

Instances details
Show Machine Source # 
Instance details

Defined in Intcode.Machine

Eq Machine Source # 
Instance details

Defined in Intcode.Machine

Methods

(==) :: Machine -> Machine -> Bool #

(/=) :: Machine -> Machine -> Bool #

Ord Machine Source # 
Instance details

Defined in Intcode.Machine

new Source #

Arguments

:: [Int]

initial memory

-> Machine 

Construct machine from a list of initial values starting at address 0. Program counter and relative base start at 0.

Register operations

jmp Source #

Arguments

:: Int

program counter

-> Machine 
-> Machine 

Set program counter to a new address.

addRelBase Source #

Arguments

:: Int

offset

-> Machine 
-> Machine 

Add offset to relative base pointer.

Memory operations

(!) Source #

Arguments

:: Machine

machine

-> Int

position

-> Int

value

Memory lookup.

set Source #

Arguments

:: Int

position

-> Int

value

-> Machine 
-> Machine 

Store value at given memory position.

memoryList Source #

Arguments

:: Machine 
-> [Int]

memory values

Generate a list representation of memory starting from zero. This can get big for sparsely filled memory using large addresses. Returned values start at position 0.

>>> memoryList (set 8 10 (new [1,2,3]))
[1,2,3,0,0,0,0,0,10]