Day 21 setup

Plus some tweaks to make ASCII IntCode machines slightly easier to use.

I need to figure out some good criteria for coding this robot thing to avoid gaps of all sizes. #..#.# is the first thing I'm seeing that's giving me trouble, we'd need to know 4 squares ahead of that little island that it was coming since that's how long it takes to jump and come back down. But we can't jump too soon if the gap is actually 3 wide, for example, or if the gap on the other side of the island is also going to be a problem. Ugh.
This commit is contained in:
2022-06-23 17:22:35 -05:00
parent 34e6055782
commit 15b34197ee
5 changed files with 78 additions and 8 deletions

View File

@ -48,6 +48,7 @@ type Day17 struct {
func (d *Day17) Parse() {
d.program = u.LoadIntcodeProgram("17p")
// d.program.SetDebugASCIIPrint(true)
}
func (d Day17) Num() int {
@ -388,9 +389,8 @@ func (d *Day17) Part2() string {
row := 0
var outputState int
var lastOutput int64
var instructionStr string
d.program.RunIn(func(inputStep int) int64 {
return int64(instructionStr[inputStep-1])
panic("unexpected read")
}, func(val int64, state u.IntcodeProgramState) {
rVal := rune(val)
if outputState == 0 {
@ -401,7 +401,7 @@ func (d *Day17) Part2() string {
if rVal == '\n' && lastOutput == '\n' {
if outputState == 0 {
instructionStr = beforeGrid.solvePath(beforeBotLocation, beforeBotFacing)
d.program.FeedInputString(beforeGrid.solvePath(beforeBotLocation, beforeBotFacing))
}
outputState++
row = 0

39
days/21.go Normal file
View File

@ -0,0 +1,39 @@
package days
import (
"fmt"
"strings"
u "parnic.com/aoc2019/utilities"
)
type Day21 struct {
program u.IntcodeProgram
}
func (d *Day21) Parse() {
d.program = u.LoadIntcodeProgram("21p")
d.program.SetDebugASCIIPrint(true)
}
func (d Day21) Num() int {
return 21
}
func (d *Day21) Part1() string {
cmds := []string{
"NOT D T",
"OR T J",
"WALK",
}
instructionStr := strings.Join(cmds, "\n") + "\n"
d.program.FeedInputString(instructionStr)
res := d.program.Run()
return fmt.Sprintf("%s%d%s", u.TextBold, res, u.TextReset)
}
func (d *Day21) Part2() string {
return fmt.Sprintf("%s%d%s", u.TextBold, 0, u.TextReset)
}

1
inputs/21p.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -54,6 +54,7 @@ var dayMap = []day{
&days.Day18{},
&days.Day19{},
&days.Day20{},
&days.Day21{},
}
func main() {

View File

@ -28,6 +28,8 @@ type IntcodeProgram struct {
program []int64
relativeBase int
haltRequested bool
printASCII bool
feedInput []rune
}
type IntcodeProgramState struct {
@ -140,14 +142,15 @@ func (p *IntcodeProgram) Reset() {
p.relativeBase = 0
}
func (p *IntcodeProgram) Run() {
p.RunIn(func(int) int64 { return 0 }, func(int64, IntcodeProgramState) {})
func (p *IntcodeProgram) Run() int64 {
return p.RunIn(func(int) int64 { return 0 }, func(int64, IntcodeProgramState) {})
}
func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOutputFunc) {
func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOutputFunc) int64 {
p.init()
inputsRequested := 0
lastOutput := int64(0)
for instructionPointer := 0; instructionPointer < len(p.program) && !p.haltRequested; {
instruction := p.GetMemory(instructionPointer)
instructionPointer++
@ -184,13 +187,28 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
case opInput:
inputsRequested++
param1 := p.GetMemory(instructionPointer)
p.setMemory(int(param1), inputFunc(inputsRequested), paramModes[0])
var inputVal int64
if len(p.feedInput) > 0 {
inputVal = int64(p.feedInput[0])
p.feedInput = p.feedInput[1:]
} else {
inputVal = inputFunc(inputsRequested)
}
if p.printASCII && inputVal <= 255 {
fmt.Printf("%c", rune(inputVal))
}
p.setMemory(int(param1), inputVal, paramModes[0])
instructionPointer += 1
case opOutput:
param1 := p.GetMemory(instructionPointer)
outputFunc(p.getParamValue(int(param1), paramModes[0]), p.makeState(instructionPointer))
param1Val := p.getParamValue(int(param1), paramModes[0])
if p.printASCII && param1Val <= 255 {
fmt.Printf("%c", rune(param1Val))
}
outputFunc(param1Val, p.makeState(instructionPointer))
lastOutput = param1Val
instructionPointer += 1
@ -256,8 +274,19 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
}
p.haltRequested = false
return lastOutput
}
func (p *IntcodeProgram) Stop() {
p.haltRequested = true
}
func (p *IntcodeProgram) SetDebugASCIIPrint(enable bool) {
p.printASCII = enable
}
func (p *IntcodeProgram) FeedInputString(str string) {
p.feedInput = make([]rune, len(str))
copy(p.feedInput, []rune(str))
}