Day 5 solution

This required an overhaul of the intcode machine to actually be its own type that could operate on its own memory and stuff. So I had to touch day 2 to make it adhere to the new API.

Feeling good about this foundation now. Until I get gobsmacked at some point later, which I expect to happen.
This commit is contained in:
2022-06-13 15:29:18 -05:00
parent bdd007bb4d
commit acef5fdc12
5 changed files with 390 additions and 34 deletions

View File

@ -18,19 +18,20 @@ func (d Day02) Num() int {
return 2
}
func (d *Day02) getProgramWithParams(param1, param2 int64) utilities.IntcodeProgram {
program := make(utilities.IntcodeProgram, len(d.program))
copy(program, d.program)
program[1] = param1
program[2] = param2
return program
func (d *Day02) setParams(param1, param2 int64) {
d.program.Reset()
d.program.SetMemory(1, param1)
d.program.SetMemory(2, param2)
}
func (d *Day02) Part1() string {
program := d.getProgramWithParams(12, 2)
program.Run()
d.setParams(12, 2)
d.program.Run()
return fmt.Sprintf("Position 0 = %s%d%s", utilities.TextBold, program[0], utilities.TextReset)
if d.program.GetMemory(0) != 4138658 {
panic("")
}
return fmt.Sprintf("Position 0 = %s%d%s", utilities.TextBold, d.program.GetMemory(0), utilities.TextReset)
}
func (d *Day02) Part2() string {
@ -41,10 +42,10 @@ func (d *Day02) Part2() string {
found := false
for noun = 0; noun <= 99; noun++ {
for verb = 0; verb <= 99; verb++ {
program := d.getProgramWithParams(noun, verb)
program.Run()
d.setParams(noun, verb)
d.program.Run()
if program[0] == sentinel {
if d.program.GetMemory(0) == sentinel {
found = true
break
}
@ -58,6 +59,9 @@ func (d *Day02) Part2() string {
if !found {
panic("!found")
}
if noun != 72 || verb != 64 {
panic("")
}
return fmt.Sprintf("%d created by noun=%d, verb=%d. 100 * noun + verb = %s%d%s",
sentinel,

188
days/05.go Normal file
View File

@ -0,0 +1,188 @@
package days
import (
"fmt"
"parnic.com/aoc2019/utilities"
)
type Day05 struct {
program utilities.IntcodeProgram
}
func (d *Day05) Parse() {
d.program = utilities.LoadIntcodeProgram("05p")
d.test()
}
func (d Day05) Num() int {
return 5
}
func (d Day05) test() {
// Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
program := utilities.ParseIntcodeProgram("3,9,8,9,10,9,4,9,99,-1,8")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 0 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1 {
panic("")
}
})
// Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
program = utilities.ParseIntcodeProgram("3,9,7,9,10,9,4,9,99,-1,8")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 0 {
panic("")
}
})
// Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
program = utilities.ParseIntcodeProgram("3,3,1108,-1,8,3,4,3,99")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 0 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1 {
panic("")
}
})
// Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
program = utilities.ParseIntcodeProgram("3,3,1107,-1,8,3,4,3,99")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 0 {
panic("")
}
})
// jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero
// position mode
program = utilities.ParseIntcodeProgram("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 0 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1 {
panic("")
}
})
// jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero
// immediate mode
program = utilities.ParseIntcodeProgram("3,3,1105,-1,9,1101,0,0,12,4,12,99,1")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 0 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1 {
panic("")
}
})
// uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.
program = utilities.ParseIntcodeProgram("3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99")
program.RunIn(func(int) int64 {
return 0
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 999 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 8
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1000 {
panic("")
}
})
program.Reset()
program.RunIn(func(int) int64 {
return 9
}, func(val int64, state utilities.IntcodeProgramState) {
if val != 1001 {
panic("")
}
})
}
func (d *Day05) Part1() string {
diagCode := int64(-1)
d.program.RunIn(func(int) int64 {
return 1
}, func(val int64, state utilities.IntcodeProgramState) {
if state.IsHalting() {
diagCode = val
} else if val != 0 {
panic("test failed")
}
})
return fmt.Sprintf("Diagnostic code: %s%d%s", utilities.TextBold, diagCode, utilities.TextReset)
}
func (d *Day05) Part2() string {
d.program.Reset()
diagCode := int64(-1)
d.program.RunIn(func(int) int64 {
return 5
}, func(val int64, state utilities.IntcodeProgramState) {
if !state.IsHalting() {
panic("unexpected output received")
}
diagCode = val
})
return fmt.Sprintf("Diagnostic code: %s%d%s", utilities.TextBold, diagCode, utilities.TextReset)
}