diff --git a/2019/day07/main.go b/2019/day07/main.go index e8d00b3..d036214 100644 --- a/2019/day07/main.go +++ b/2019/day07/main.go @@ -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(settings[0]) + a2.Input(settings[1]) + a3.Input(settings[2]) + a4.Input(settings[3]) + a5.Input(settings[4]) - a1.Input(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) - } + a5Out := 0 + for { + a1.Input(a5Out) + a2.Input(a1.Output()) + a3.Input(a2.Output()) + a4.Input(a3.Output()) + a5.Input(a4.Output()) + for !a5.NeedsOutput() { + time.Sleep(1) + } + a5Out = a5.Output() + // Wait until amp 5 is either done, or waiting + for a5.State() != intcode.RET_DONE && !a5.NeedsInput() { + time.Sleep(1) + } + if a5.State() == intcode.RET_DONE { + break } - }() - for a5.State() != intcode.RET_DONE && !a5.NeedsOutput() { - time.Sleep(1) } - return a5.Output() + return a5Out } diff --git a/2019/intcode-processor/processor.go b/2019/intcode-processor/processor.go index a230d76..517b17d 100644 --- a/2019/intcode-processor/processor.go +++ b/2019/intcode-processor/processor.go @@ -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]