IntCode Processor Input/Output changes
For day 5, ask the user for input
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -11,23 +13,46 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := helpers.StdinToString()
|
||||
progFileName := "input"
|
||||
if len(os.Args) > 1 {
|
||||
progFileName = os.Args[1]
|
||||
}
|
||||
dat, err := ioutil.ReadFile(progFileName)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading program file:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
var prog []int
|
||||
for _, v := range strings.Split(inp, ",") {
|
||||
stringDat := strings.TrimSpace(string(dat))
|
||||
for _, v := range strings.Split(stringDat, ",") {
|
||||
prog = append(prog, helpers.Atoi(v))
|
||||
}
|
||||
fmt.Println("Day 5, part 1: Input '1'")
|
||||
fmt.Println("Day 5, part 2: Input '5'")
|
||||
p := intcode.NewProgram(prog)
|
||||
go p.Run()
|
||||
// Part 1:
|
||||
// p.Input() <- 1
|
||||
// Part 2:
|
||||
p.Input() <- 5
|
||||
go func() {
|
||||
for {
|
||||
out := <-p.Output()
|
||||
fmt.Println(out)
|
||||
if p.State() != intcode.RET_OK {
|
||||
os.Exit(0)
|
||||
if st := p.State(); st != intcode.RET_OK {
|
||||
if st == intcode.RET_ERR {
|
||||
os.Exit(1)
|
||||
} else {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
if p.NeedsInput() {
|
||||
fmt.Print("Requesting Input: ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
inp, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
fmt.Println("Input Error:", err.Error())
|
||||
} else {
|
||||
p.Input(helpers.Atoi(strings.TrimSpace(inp)))
|
||||
}
|
||||
}
|
||||
if p.NeedsOutput() {
|
||||
out := p.Output()
|
||||
fmt.Println(out)
|
||||
}
|
||||
time.Sleep(50)
|
||||
}
|
||||
|
@@ -6,11 +6,6 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
MODE_POS = iota
|
||||
MODE_IMM
|
||||
)
|
||||
|
||||
const (
|
||||
OP_ADD = 1
|
||||
OP_MLT = 2
|
||||
@@ -23,6 +18,11 @@ const (
|
||||
OP_EXT = 99
|
||||
)
|
||||
|
||||
const (
|
||||
MODE_POS = iota
|
||||
MODE_IMM
|
||||
)
|
||||
|
||||
const (
|
||||
RET_ERR = iota - 1
|
||||
RET_OK
|
||||
@@ -36,15 +36,17 @@ type Program struct {
|
||||
state int
|
||||
error error
|
||||
|
||||
input chan int
|
||||
output chan int
|
||||
waitingForInput bool
|
||||
input chan int
|
||||
waitingForOutput bool
|
||||
output chan int
|
||||
}
|
||||
|
||||
func NewProgram(prog []int) *Program {
|
||||
p := new(Program)
|
||||
p.code = make([]int, len(prog))
|
||||
p.input = make(chan int, 2)
|
||||
p.output = make(chan int, 2)
|
||||
p.input = make(chan int)
|
||||
p.output = make(chan int)
|
||||
copy(p.code, prog)
|
||||
return p
|
||||
}
|
||||
@@ -157,6 +159,14 @@ func (p *Program) SetProgramValueAt(idx, val int) {
|
||||
p.code[idx] = val
|
||||
}
|
||||
|
||||
func (p *Program) NeedsInput() bool {
|
||||
return p.waitingForInput
|
||||
}
|
||||
|
||||
func (p *Program) NeedsOutput() bool {
|
||||
return p.waitingForOutput
|
||||
}
|
||||
|
||||
func (p *Program) opCode(intcode int) int {
|
||||
return intcode % 100
|
||||
}
|
||||
@@ -188,12 +198,15 @@ func (p *Program) get(mode, v int) int {
|
||||
return v
|
||||
}
|
||||
|
||||
func (p *Program) Input() chan int {
|
||||
return p.input
|
||||
func (p *Program) Input(v int) {
|
||||
p.input <- v
|
||||
p.waitingForInput = false
|
||||
}
|
||||
|
||||
func (p *Program) Output() chan int {
|
||||
return p.output
|
||||
func (p *Program) Output() int {
|
||||
v := <-p.output
|
||||
p.waitingForOutput = false
|
||||
return v
|
||||
}
|
||||
|
||||
func (p *Program) opAdd(intcode, a1, a2, dest int) {
|
||||
@@ -220,12 +233,15 @@ func (p *Program) opInp(intcode, dest int) {
|
||||
p.error = errors.New("Invalid Destination Mode")
|
||||
p.state = RET_ERR
|
||||
}
|
||||
p.waitingForInput = true
|
||||
p.code[dest] = <-p.input
|
||||
p.waitingForInput = false
|
||||
}
|
||||
|
||||
func (p *Program) opOut(intcode, val int) {
|
||||
valmd := p.paramMode(intcode, 0)
|
||||
ret := p.get(valmd, val)
|
||||
p.waitingForOutput = true
|
||||
p.output <- ret
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user