84 lines
1.6 KiB
Go
84 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
|
||
|
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
pt := helpers.GetArgNumber(1)
|
||
|
prog := intcode.ReadIntCodeFile("input")
|
||
|
if pt == "1" {
|
||
|
part1(prog)
|
||
|
} else {
|
||
|
part2(prog)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func part1(prog []int) {
|
||
|
p := intcode.NewProgram(prog)
|
||
|
//p.EnableDebug()
|
||
|
var lastOut int
|
||
|
go func() {
|
||
|
for {
|
||
|
if !p.NeedsOutput() && !p.NeedsInput() {
|
||
|
time.Sleep(1)
|
||
|
} else if p.NeedsOutput() {
|
||
|
lastOut = getOutput(p)
|
||
|
fmt.Print(string(byte(lastOut)))
|
||
|
} else if p.NeedsInput() {
|
||
|
instructions := "NOT A J\nNOT C T\nOR T J\nAND D J\nWALK\n"
|
||
|
//instructions := "NOT A J\nWALK\n"
|
||
|
for _, v := range []byte(instructions) {
|
||
|
fmt.Print(string(v))
|
||
|
p.Input(int(v))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/*
|
||
|
fmt.Println(getOutput(p))
|
||
|
return
|
||
|
*/
|
||
|
}()
|
||
|
p.Run()
|
||
|
fmt.Println("Damage:", lastOut)
|
||
|
}
|
||
|
|
||
|
func part2(prog []int) {
|
||
|
p := intcode.NewProgram(prog)
|
||
|
//p.EnableDebug()
|
||
|
var lastOut int
|
||
|
go func() {
|
||
|
for {
|
||
|
if !p.NeedsOutput() && !p.NeedsInput() {
|
||
|
time.Sleep(1)
|
||
|
} else if p.NeedsOutput() {
|
||
|
lastOut = getOutput(p)
|
||
|
fmt.Print(string(byte(lastOut)))
|
||
|
} else if p.NeedsInput() {
|
||
|
instructions := "NOT A J\nNOT B T\nOR T J\nNOT C T\nOR T J\nAND D J\nAND E T\nOR H T\nAND T J\nRUN\n"
|
||
|
for _, v := range []byte(instructions) {
|
||
|
fmt.Print(string(v))
|
||
|
p.Input(int(v))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/*
|
||
|
fmt.Println(getOutput(p))
|
||
|
return
|
||
|
*/
|
||
|
}()
|
||
|
p.Run()
|
||
|
fmt.Println("Damage:", lastOut)
|
||
|
}
|
||
|
|
||
|
func getOutput(p *intcode.Program) int {
|
||
|
for !p.NeedsOutput() {
|
||
|
time.Sleep(1)
|
||
|
}
|
||
|
return p.Output()
|
||
|
}
|