2019-12-05 14:04:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-05 14:50:28 +00:00
|
|
|
"bufio"
|
2019-12-05 14:04:30 +00:00
|
|
|
"fmt"
|
2019-12-05 14:50:28 +00:00
|
|
|
"io/ioutil"
|
2019-12-05 14:04:30 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
|
|
|
|
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-12-05 14:50:28 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-12-05 14:04:30 +00:00
|
|
|
var prog []int
|
2019-12-05 14:50:28 +00:00
|
|
|
stringDat := strings.TrimSpace(string(dat))
|
|
|
|
for _, v := range strings.Split(stringDat, ",") {
|
2019-12-05 14:04:30 +00:00
|
|
|
prog = append(prog, helpers.Atoi(v))
|
|
|
|
}
|
2019-12-05 14:50:28 +00:00
|
|
|
fmt.Println("Day 5, part 1: Input '1'")
|
|
|
|
fmt.Println("Day 5, part 2: Input '5'")
|
2019-12-05 14:04:30 +00:00
|
|
|
p := intcode.NewProgram(prog)
|
|
|
|
go p.Run()
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-12-05 14:50:28 +00:00
|
|
|
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)
|
2019-12-05 14:04:30 +00:00
|
|
|
}
|
|
|
|
time.Sleep(50)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|