A little cleanup

This commit is contained in:
Brian Buller 2019-12-13 08:51:59 -06:00
parent c06abb613c
commit 74510f9723

View File

@ -15,7 +15,7 @@ import (
var auto bool var auto bool
func main() { func main() {
progFileName := "input" progFileName := "finishinput"
if len(os.Args) > 1 && os.Args[1] == "-auto" { if len(os.Args) > 1 && os.Args[1] == "-auto" {
auto = true auto = true
@ -26,6 +26,7 @@ func main() {
func play(prog []int) { func play(prog []int) {
p := intcode.NewProgram(prog) p := intcode.NewProgram(prog)
// For part 1, comment out this line
p.SetProgramValueAt(0, 2) p.SetProgramValueAt(0, 2)
//p.EnableDebug() //p.EnableDebug()
maxX, maxY := 0, 0 maxX, maxY := 0, 0
@ -70,24 +71,26 @@ func play(prog []int) {
for !gotInput { for !gotInput {
fmt.Print("Input (vimlike): ") fmt.Print("Input (vimlike): ")
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
char, _, err := reader.ReadRune() inp, err := reader.ReadString('\n')
if err != nil { if err != nil {
panic(err) panic(err)
} }
switch char { inp = strings.TrimSpace(inp)
case 'h': switch inp {
case "h":
p.Input(-1) p.Input(-1)
gotInput = true gotInput = true
case 'l': case "l":
p.Input(1) p.Input(1)
gotInput = true gotInput = true
case 'j', 'k': case "j", "k":
p.Input(0) p.Input(0)
gotInput = true gotInput = true
} }
} }
} }
} }
//lookForScore(p, score)
switch p.State() { switch p.State() {
case intcode.RET_ERR: case intcode.RET_ERR:
panic(p.Error()) panic(p.Error())
@ -98,13 +101,27 @@ func play(prog []int) {
}() }()
ret := p.Run() ret := p.Run()
if ret == intcode.RET_DONE { if ret == intcode.RET_DONE {
fmt.Println("DONE") fmt.Println("DONE:", score)
} else if ret == intcode.RET_ERR { } else if ret == intcode.RET_ERR {
fmt.Println("ERROR") fmt.Println("ERROR")
} }
//part1(screen) //part1(screen)
} }
func lookForScore(p *intcode.Program, score int) {
var res []int
code := p.GetCode()
for k, v := range code {
if v == score {
res = append(res, k)
}
}
if len(res) == 1 {
fmt.Println("Score Position:", res[0])
os.Exit(0)
}
}
func part1(s map[string]int) { func part1(s map[string]int) {
var cnt int var cnt int
for _, v := range s { for _, v := range s {
@ -116,7 +133,7 @@ func part1(s map[string]int) {
} }
func printScreen(screen map[string]int, maxX, maxY, score int) { func printScreen(screen map[string]int, maxX, maxY, score int) {
fmt.Print(helpers.CLEAR_SCREEN) //fmt.Print(helpers.CLEAR_SCREEN)
fmt.Println("Score:", score) fmt.Println("Score:", score)
for y := 0; y <= maxY; y++ { for y := 0; y <= maxY; y++ {
for x := 0; x <= maxX; x++ { for x := 0; x <= maxX; x++ {