adventofcode/2019/day17/part2.go

95 lines
1.6 KiB
Go
Raw Normal View History

2019-12-18 13:25:25 +00:00
package main
import (
"fmt"
"time"
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
)
func part2(prog []int) {
p := intcode.NewProgram(prog)
p.SetProgramValueAt(0, 2)
/*
A: L,4,L,4,L,10,R,4,R,4
B: L,4,L,4,R,8,R,10,
A: L,4,L,4,L,10,R,4,R,4
L,10,R,10,
A: L,4,L,4,L,10,R,4,R,4,
L,10,R,10,R,4,
L,4,L,4,R,8,R,10,R,4,
L,10,R,10,R,4,
L,10,R,10,R,4,
B: L,4,L,4,R,8,R,10
*/
var input []string
input = append(input, "A,B,A,C"+string(byte(10)))
input = append(input, "L,4,L,4,L,10,R,4,R,4"+string(byte(10)))
input = append(input, "L,4,L,4,R,8,R,10"+string(byte(10)))
input = append(input, "L,10"+string(byte(10)))
go func() {
printAllOutput(p)
for k := range input {
writeString(p, input[k])
printAllOutput(p)
}
for !p.NeedsInput() {
time.Sleep(1)
}
p.Input(int('y'))
for !p.NeedsInput() {
time.Sleep(1)
}
p.Input(10)
printAllOutput(p)
}()
fmt.Println("Running")
p.Run()
fmt.Println("Done")
}
func readString(p *intcode.Program) string {
var ret string
for {
if !p.NeedsOutput() {
time.Sleep(1)
}
out := p.Output()
if byte(out) == '\n' {
return ret
}
ret = ret + string(out)
}
}
func writeString(p *intcode.Program, str string) {
fmt.Println("Printing", str)
for k := range str {
if p.NeedsInput() {
fmt.Print(string(str[k]))
p.Input(int(str[k]))
printAllOutput(p)
}
if p.NeedsOutput() {
return
}
time.Sleep(1)
}
}
func printAllOutput(p *intcode.Program) {
for {
if p.NeedsInput() {
return
}
if p.NeedsOutput() {
fmt.Print(string(p.Output()))
}
time.Sleep(1)
}
}