Allow intcode programs to be halted externally
This is simplest to do during an input or output callback, but could potentially also be done to a program running on a goroutine.
This commit is contained in:
@ -24,9 +24,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type IntcodeProgram struct {
|
type IntcodeProgram struct {
|
||||||
memory []int64
|
memory []int64
|
||||||
program []int64
|
program []int64
|
||||||
relativeBase int
|
relativeBase int
|
||||||
|
haltRequested bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type IntcodeProgramState struct {
|
type IntcodeProgramState struct {
|
||||||
@ -141,7 +142,7 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
|
|||||||
p.init()
|
p.init()
|
||||||
|
|
||||||
inputsRequested := 0
|
inputsRequested := 0
|
||||||
for instructionPointer := 0; instructionPointer < len(p.program); {
|
for instructionPointer := 0; instructionPointer < len(p.program) && !p.haltRequested; {
|
||||||
instruction := p.GetMemory(instructionPointer)
|
instruction := p.GetMemory(instructionPointer)
|
||||||
instructionPointer++
|
instructionPointer++
|
||||||
|
|
||||||
@ -247,4 +248,10 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
|
|||||||
panic(fmt.Sprintf("exception executing program - unhandled opcode %d", opcode))
|
panic(fmt.Sprintf("exception executing program - unhandled opcode %d", opcode))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.haltRequested = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *IntcodeProgram) Stop() {
|
||||||
|
p.haltRequested = true
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user