81 lines
1.3 KiB
Go
81 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
|
|
)
|
|
|
|
var auto bool
|
|
|
|
func main() {
|
|
progFileName := "input"
|
|
|
|
if len(os.Args) > 1 && os.Args[1] == "-auto" {
|
|
auto = true
|
|
}
|
|
prog := intcode.ReadIntCodeFile(progFileName)
|
|
play(prog)
|
|
}
|
|
|
|
func play(prog []int) {
|
|
p := intcode.NewProgram(prog)
|
|
go func() {
|
|
for {
|
|
for !p.NeedsInput() {
|
|
time.Sleep(1)
|
|
}
|
|
if auto {
|
|
} else {
|
|
var gotInput bool
|
|
for !gotInput {
|
|
fmt.Print("Input (vimlike): ")
|
|
reader := bufio.NewReader(os.Stdin)
|
|
inp, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
inp = strings.TrimSpace(inp)
|
|
switch inp {
|
|
case "h":
|
|
p.Input(3)
|
|
gotInput = true
|
|
case "j":
|
|
p.Input(2)
|
|
gotInput = true
|
|
case "k":
|
|
p.Input(1)
|
|
gotInput = true
|
|
case "l":
|
|
p.Input(4)
|
|
gotInput = true
|
|
}
|
|
}
|
|
}
|
|
for !p.NeedsOutput() {
|
|
time.Sleep(1)
|
|
}
|
|
moveRes := p.Output()
|
|
switch moveRes {
|
|
case 0: // Hit a wall
|
|
fmt.Println("WALL")
|
|
case 1: // Moved
|
|
fmt.Println("OK")
|
|
case 2: // Moved and done
|
|
fmt.Println("DONE")
|
|
}
|
|
}
|
|
}()
|
|
ret := p.Run()
|
|
if ret == intcode.RET_DONE {
|
|
|
|
} else if ret == intcode.RET_ERR {
|
|
fmt.Println("ERROR")
|
|
fmt.Println(p.Error())
|
|
}
|
|
}
|