IntCode Processor Input/Output changes

For day 5, ask the user for input
This commit is contained in:
Brian Buller 2019-12-05 08:50:28 -06:00
parent 3d955a924d
commit 5edde5ba62
4 changed files with 77 additions and 23 deletions

View File

@ -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)
}

View File

@ -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
}

1
go.mod
View File

@ -9,5 +9,6 @@ require (
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-runewidth v0.0.7 // indirect
github.com/nlopes/slack v0.6.0 // indirect
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317
)

12
go.sum
View File

@ -1,9 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/go-gl/glfw v0.0.0-20191125211704-12ad95a8df72 h1:LgLYrxDRSVv3kStk6louYTP1ekZ6t7HZY/X05KUyaeM=
github.com/go-gl/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
@ -11,8 +15,16 @@ github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW1
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA=
github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk=
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317 h1:hhGN4SFXgXo61Q4Sjj/X9sBjyeSa2kdpaOzCO+8EVQw=
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=