2019 Day 7 Complete

This commit is contained in:
Brian Buller 2019-12-09 09:17:27 -06:00
parent 22c83634fa
commit a896f40ab5
2 changed files with 35 additions and 34 deletions

View File

@ -29,7 +29,7 @@ func main() {
prog = append(prog, helpers.Atoi(v))
}
// part1(prog)
part1(prog)
part2(prog)
}
@ -49,15 +49,13 @@ func part1(prog []int) {
a5 = intcode.NewProgram(prog)
go a5.Run()
fmt.Print(allPerms[k], ": ")
wrk := pt1RunWithPhaseSettings(allPerms[k])
fmt.Println(wrk)
if wrk > max {
max = wrk
bestPerm = allPerms[k]
}
}
fmt.Println("BEST: ", bestPerm, max)
fmt.Println("Part 1 BEST: ", bestPerm, max)
}
func pt1RunWithPhaseSettings(settings []int) int {
@ -85,56 +83,55 @@ func part2(prog []int) {
max := 0
for k := range allPerms {
a1 = intcode.NewProgram(prog)
a1.Reset()
go a1.Run()
a2 = intcode.NewProgram(prog)
a2.Reset()
go a2.Run()
a3 = intcode.NewProgram(prog)
a3.Reset()
go a3.Run()
a4 = intcode.NewProgram(prog)
a4.Reset()
go a4.Run()
a5 = intcode.NewProgram(prog)
a5.Reset()
go a5.Run()
fmt.Print(allPerms[k], ": ")
wrk := pt2RunWithPhaseSettings(allPerms[k])
fmt.Println(wrk)
if wrk > max {
max = wrk
bestPerm = allPerms[k]
}
}
fmt.Println("BEST: ", bestPerm, max)
fmt.Println("Part 2 BEST: ", bestPerm, max)
}
func pt2RunWithPhaseSettings(settings []int) int {
go func() {
a1.Input(settings[0])
a2.Input(settings[1])
a3.Input(settings[2])
a4.Input(settings[3])
a5.Input(settings[4])
a1.Input(0)
a5Out := 0
for {
if a5.NeedsOutput() {
a5Out := a5.Output()
fmt.Println("A5: ", a5Out)
a1.Input(a5Out)
}
a2.Input(a1.Output())
a3.Input(a2.Output())
a4.Input(a3.Output())
a5.Input(a4.Output())
if a5.State() == intcode.RET_DONE {
break
}
for !a5.NeedsOutput() {
time.Sleep(1)
}
}
}()
for a5.State() != intcode.RET_DONE && !a5.NeedsOutput() {
a5Out = a5.Output()
// Wait until amp 5 is either done, or waiting
for a5.State() != intcode.RET_DONE && !a5.NeedsInput() {
time.Sleep(1)
}
return a5.Output()
if a5.State() == intcode.RET_DONE {
break
}
}
return a5Out
}

View File

@ -204,6 +204,10 @@ func (p *Program) Step() int {
return RET_ERR
}
func (p *Program) GetCurrentOpCode() int {
return p.code[p.ptr]
}
func (p *Program) GetProgramValueAt(idx int) int {
p.ensureLength(idx)
return p.code[idx]