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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user