adventofcode/2019/day09/main.go

57 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-12-09 13:32:24 +00:00
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
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
stringDat := strings.TrimSpace(string(dat))
for _, v := range strings.Split(stringDat, ",") {
prog = append(prog, helpers.Atoi(v))
}
2019-12-09 15:20:32 +00:00
solve(prog)
2019-12-09 13:32:24 +00:00
}
2019-12-09 15:20:32 +00:00
func solve(inp []int) {
2019-12-09 13:32:24 +00:00
p := intcode.NewProgram(inp)
//p.EnableDebug()
go p.Run()
for p.State() != intcode.RET_DONE {
if p.State() == intcode.RET_ERR {
panic(p.Error())
}
if p.NeedsInput() {
2019-12-09 15:20:32 +00:00
fmt.Print("Input ('1' for part 1, '2' for part 2): ")
2019-12-09 13:32:24 +00:00
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() {
fmt.Println(p.Output())
}
time.Sleep(1)
}
}