adventofcode/2019/intcode-processor/processor.go

422 lines
8.5 KiB
Go
Raw Permalink Normal View History

package intcodeprocessor
import (
2020-11-06 21:04:08 +00:00
"bytes"
"encoding/json"
"errors"
"fmt"
2019-12-15 14:57:44 +00:00
"io/ioutil"
"math"
2019-12-15 14:57:44 +00:00
"os"
"strings"
2020-11-06 21:04:08 +00:00
"time"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
const (
OP_ADD = 1
OP_MLT = 2
OP_INP = 3
OP_OUT = 4
OP_JIT = 5
OP_JIF = 6
OP_ILT = 7
OP_IEQ = 8
2019-12-09 13:32:24 +00:00
OP_RBS = 9
OP_EXT = 99
)
const (
MODE_POS = iota
MODE_IMM
2019-12-09 13:32:24 +00:00
MODE_REL
)
const (
RET_ERR = iota - 1
RET_OK
RET_DONE
)
type Program struct {
2020-11-06 21:04:08 +00:00
OriginalCode []int
Code []int
Ptr int
RelBase int
2020-11-06 21:04:08 +00:00
State int
Error error
2020-11-06 21:04:08 +00:00
Pause bool
Bail bool
WaitingForInput bool
InputChan chan int
WaitingForOutput bool
OutputChan chan int
2019-12-09 13:32:24 +00:00
2020-11-06 21:04:08 +00:00
Debug bool
DebugToFile bool
}
2019-12-15 14:57:44 +00:00
func ReadIntCodeFile(fn string) []int {
dat, err := ioutil.ReadFile(fn)
if err != nil {
fmt.Println("Error reading program file:", err.Error())
os.Exit(1)
}
var prog []int
stringDat := strings.TrimSpace(string(dat))
for _, v := range strings.Split(stringDat, ",") {
prog = append(prog, helpers.Atoi(v))
}
return prog
}
func NewProgram(prog []int) *Program {
p := new(Program)
2020-11-06 21:04:08 +00:00
p.OriginalCode = make([]int, len(prog))
p.Code = make([]int, len(prog))
copy(p.OriginalCode, prog)
2019-12-09 03:51:43 +00:00
p.Reset()
return p
}
2020-11-06 21:04:08 +00:00
func (p *Program) SaveState(slot string) error {
f, err := os.OpenFile("saveslot-"+slot+".sav", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.Encode(p)
return nil
2019-12-09 13:32:24 +00:00
}
2020-11-06 21:04:08 +00:00
func (p *Program) LoadState(slot string) error {
hold, err := ioutil.ReadFile("saveslot-" + slot + ".sav")
if err != nil {
return err
2019-12-09 13:32:24 +00:00
}
2020-11-06 21:04:08 +00:00
json.NewDecoder(bytes.NewBuffer(hold)).Decode(&p)
return nil
2019-12-09 13:32:24 +00:00
}
2020-11-06 21:04:08 +00:00
func (p *Program) Pointer() int {
return p.Ptr
}
2020-11-06 21:04:08 +00:00
func (p *Program) DebugLog(l string) {
if p.Debug {
fmt.Print(l)
}
2019-12-04 20:16:41 +00:00
}
2020-11-06 21:04:08 +00:00
func (p *Program) OutputToFile(l string) {
f, err := os.OpenFile("debug-log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
_, err = fmt.Fprintln(f, l)
}
2020-11-06 21:04:08 +00:00
func (p *Program) Reset() {
copy(p.Code, p.OriginalCode)
p.Ptr = 0
p.State = RET_OK
p.Error = nil
p.Pause = false
p.WaitingForInput = false
p.WaitingForOutput = false
p.InputChan = make(chan int)
p.OutputChan = make(chan int)
p.RelBase = 0
}
2019-12-16 23:31:11 +00:00
func (p *Program) ForceQuit() {
2020-11-06 21:04:08 +00:00
p.Bail = true
close(p.InputChan)
close(p.OutputChan)
2019-12-16 23:31:11 +00:00
}
func (p *Program) Run() int {
for {
2020-11-06 21:04:08 +00:00
p.State = p.Step()
if p.State != RET_OK {
return p.State
}
}
}
func (p *Program) Step() int {
2020-11-06 21:04:08 +00:00
for p.Pause {
time.Sleep(1)
}
if p.Bail {
p.Error = errors.New("Force Quit")
2019-12-16 23:31:11 +00:00
return RET_ERR
}
2020-11-06 21:04:08 +00:00
if len(p.Code) < p.Ptr {
p.Error = errors.New("Pointer Exception")
return RET_ERR
}
2019-12-09 13:32:24 +00:00
p.DebugLog(p.String() + "\n")
intcode := p.readNext()
2020-11-06 21:04:08 +00:00
p.Ptr++
switch p.opCode(intcode) {
case OP_ADD:
v1, v2, v3 := p.readNextThree()
2020-11-06 21:04:08 +00:00
debug := fmt.Sprintf("ADD %d (%d, %d, %d)\n", intcode, v1, v2, v3)
p.DebugLog(debug)
if p.DebugToFile {
p.OutputToFile(fmt.Sprintf("%d: %s", p.Ptr, debug))
}
p.Ptr = p.Ptr + 3
p.opAdd(intcode, v1, v2, v3)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_MLT:
v1, v2, v3 := p.readNextThree()
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("MLT %d (%d, %d, %d)\n", intcode, v1, v2, v3))
2020-11-06 21:04:08 +00:00
p.Ptr = p.Ptr + 3
p.opMult(intcode, v1, v2, v3)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_INP:
v1 := p.readNext()
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("INP %d (%d)\n", intcode, v1))
2020-11-06 21:04:08 +00:00
p.Ptr++
p.opInp(intcode, v1)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_OUT:
v1 := p.readNext()
2020-11-06 21:04:08 +00:00
p.Ptr++
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("OUT %d (%d)\n", intcode, v1))
p.opOut(intcode, v1)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_JIT:
v1, v2 := p.readNextTwo()
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("JIT %d (%d, %d)\n", intcode, v1, v2))
2020-11-06 21:04:08 +00:00
p.Ptr = p.Ptr + 2
p.opJumpIfTrue(intcode, v1, v2)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_JIF:
v1, v2 := p.readNextTwo()
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("JIF %d (%d, %d)\n", intcode, v1, v2))
2020-11-06 21:04:08 +00:00
p.Ptr = p.Ptr + 2
p.opJumpIfFalse(intcode, v1, v2)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_ILT:
v1, v2, dest := p.readNextThree()
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("ILT %d (%d, %d, %d)\n", intcode, v1, v2, dest))
2020-11-06 21:04:08 +00:00
p.Ptr = p.Ptr + 3
p.opIfLessThan(intcode, v1, v2, dest)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
case OP_IEQ:
v1, v2, dest := p.readNextThree()
2020-11-06 21:04:08 +00:00
debug := fmt.Sprintf("IEQ %d (%d, %d, %d)\n", intcode, v1, v2, dest)
p.DebugLog(debug)
p.Ptr = p.Ptr + 3
p.opIfEqual(intcode, v1, v2, dest)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
return RET_ERR
}
return RET_OK
2019-12-09 13:32:24 +00:00
case OP_RBS:
v1 := p.readNext()
p.DebugLog(fmt.Sprintf("RBS %d (%d)\n", intcode, v1))
2020-11-06 21:04:08 +00:00
p.Ptr = p.Ptr + 1
2019-12-09 13:32:24 +00:00
p.opModRelBase(intcode, v1)
2020-11-06 21:04:08 +00:00
if p.Error != nil {
2019-12-09 13:32:24 +00:00
return RET_ERR
}
return RET_OK
case OP_EXT:
2019-12-09 13:32:24 +00:00
p.DebugLog(fmt.Sprintf("EXT %d\n", intcode))
return RET_DONE
}
2020-11-06 21:04:08 +00:00
p.Error = errors.New(fmt.Sprintf("Invalid OpCode (%d)", intcode))
2019-12-09 13:32:24 +00:00
p.DebugLog(p.String())
return RET_ERR
}
2019-12-09 15:17:27 +00:00
func (p *Program) GetCurrentOpCode() int {
2020-11-06 21:04:08 +00:00
return p.Code[p.Ptr]
2019-12-09 15:17:27 +00:00
}
func (p *Program) GetProgramValueAt(idx int) int {
2019-12-09 13:32:24 +00:00
p.ensureLength(idx)
2020-11-06 21:04:08 +00:00
return p.Code[idx]
}
func (p *Program) SetProgramValueAt(idx, val int) {
2019-12-09 13:32:24 +00:00
p.ensureLength(idx)
2020-11-06 21:04:08 +00:00
p.Code[idx] = val
}
func (p *Program) NeedsInput() bool {
2020-11-06 21:04:08 +00:00
return p.WaitingForInput
}
func (p *Program) NeedsOutput() bool {
2020-11-06 21:04:08 +00:00
return p.WaitingForOutput
}
func (p *Program) opCode(intcode int) int {
return intcode % 100
}
func (p *Program) paramMode(intcode, pNum int) int {
plc := math.Pow10(pNum + 2)
return ((intcode - p.opCode(intcode)) / int(plc)) % 10
}
2019-12-09 13:32:24 +00:00
func (p *Program) ensureLength(idx int) {
2020-11-06 21:04:08 +00:00
for len(p.Code) < idx+1 {
p.Code = append(p.Code, 0)
}
2019-12-09 13:32:24 +00:00
}
func (p *Program) readNext() int {
2020-11-06 21:04:08 +00:00
p.ensureLength(p.Ptr)
return p.Code[p.Ptr]
}
func (p *Program) readNextTwo() (int, int) {
2020-11-06 21:04:08 +00:00
p.ensureLength(p.Ptr + 1)
return p.Code[p.Ptr], p.Code[p.Ptr+1]
}
func (p *Program) readNextThree() (int, int, int) {
2020-11-06 21:04:08 +00:00
p.ensureLength(p.Ptr + 2)
return p.Code[p.Ptr], p.Code[p.Ptr+1], p.Code[p.Ptr+2]
}
func (p *Program) get(mode, v int) int {
if mode == MODE_POS {
2019-12-09 13:32:24 +00:00
p.ensureLength(v)
2020-11-06 21:04:08 +00:00
return p.Code[v]
2019-12-09 13:32:24 +00:00
} else if mode == MODE_REL {
2020-11-06 21:04:08 +00:00
p.ensureLength(p.RelBase + v)
return p.Code[p.RelBase+v]
}
return v
}
2019-12-09 13:32:24 +00:00
func (p *Program) set(mode, idx, v int) {
if mode == MODE_POS {
p.ensureLength(idx)
2020-11-06 21:04:08 +00:00
p.Code[idx] = v
2019-12-09 13:32:24 +00:00
} else if mode == MODE_REL {
2020-11-06 21:04:08 +00:00
p.ensureLength(p.RelBase + idx)
p.Code[p.RelBase+idx] = v
2019-12-09 13:32:24 +00:00
}
}
func (p *Program) Input(v int) {
2020-11-06 21:04:08 +00:00
p.InputChan <- v
//p.WaitingForInput = false
}
func (p *Program) Output() int {
2020-11-06 21:04:08 +00:00
v := <-p.OutputChan
p.WaitingForOutput = false
return v
}
func (p *Program) opAdd(intcode, a1, a2, dest int) {
a1md, a2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
2019-12-09 13:32:24 +00:00
p.set(destmd, dest, p.get(a1md, a1)+p.get(a2md, a2))
}
func (p *Program) opMult(intcode, a1, a2, dest int) {
a1md, a2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
2019-12-09 13:32:24 +00:00
p.set(destmd, dest, p.get(a1md, a1)*p.get(a2md, a2))
}
func (p *Program) opInp(intcode, dest int) {
destmd := p.paramMode(intcode, 0)
2020-11-06 21:04:08 +00:00
p.WaitingForInput = true
p.set(destmd, dest, <-p.InputChan)
p.WaitingForInput = false
}
func (p *Program) opOut(intcode, val int) {
valmd := p.paramMode(intcode, 0)
ret := p.get(valmd, val)
2020-11-06 21:04:08 +00:00
p.WaitingForOutput = true
p.OutputChan <- ret
}
func (p *Program) opJumpIfTrue(intcode, v1, v2 int) {
v1md, v2md := p.paramMode(intcode, 0), p.paramMode(intcode, 1)
if p.get(v1md, v1) != 0 {
2020-11-06 21:04:08 +00:00
p.Ptr = p.get(v2md, v2)
}
}
func (p *Program) opJumpIfFalse(intcode, v1, v2 int) {
v1md, v2md := p.paramMode(intcode, 0), p.paramMode(intcode, 1)
if p.get(v1md, v1) == 0 {
2020-11-06 21:04:08 +00:00
p.Ptr = p.get(v2md, v2)
}
}
func (p *Program) opIfLessThan(intcode, v1, v2, dest int) {
v1md, v2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
if p.get(v1md, v1) < p.get(v2md, v2) {
2019-12-09 13:32:24 +00:00
p.set(destmd, dest, 1)
} else {
2019-12-09 13:32:24 +00:00
p.set(destmd, dest, 0)
}
}
func (p *Program) opIfEqual(intcode, v1, v2, dest int) {
v1md, v2md, destmd := p.paramMode(intcode, 0), p.paramMode(intcode, 1), p.paramMode(intcode, 2)
if p.get(v1md, v1) == p.get(v2md, v2) {
2019-12-09 13:32:24 +00:00
p.set(destmd, dest, 1)
} else {
2019-12-09 13:32:24 +00:00
p.set(destmd, dest, 0)
}
}
2019-12-09 13:32:24 +00:00
func (p *Program) opModRelBase(intcode, v1 int) {
v1md := p.paramMode(intcode, 0)
2020-11-06 21:04:08 +00:00
p.RelBase = p.RelBase + p.get(v1md, v1)
2019-12-09 13:32:24 +00:00
}
func (p Program) String() string {
var ret string
2020-11-06 21:04:08 +00:00
ret = ret + fmt.Sprintf("(PTR: %d, RBS: %d)\n", p.Ptr, p.RelBase)
for k := range p.Code {
if k == p.Ptr {
ret = fmt.Sprintf("%s [%d]", ret, p.Code[k])
} else {
2020-11-06 21:04:08 +00:00
ret = fmt.Sprintf("%s %d", ret, p.Code[k])
}
}
return ret
}