diff --git a/2016/day23/main.go b/2016/day23/main.go index 775b471..ebbfff2 100644 --- a/2016/day23/main.go +++ b/2016/day23/main.go @@ -51,18 +51,14 @@ func main() { go readUserInput(eventChan) go sendNoneEvent(eventChan) curr = 0 - // Find a set of instructions that look like: - // inc a - // dec c - // jnz c -2 - // and convert them to: - // mlt a c a - // cpy 0 c + var ins []string for curr < len(instructions) || done { if debug { + lastInst := ins // print state and wait for user to step PrintState() - PrintInstructionState() + fmt.Println("Last Instruction:", lastInst) + //PrintInstructionState() fmt.Println("(q): quit (space): pause/unpause") ev := <-eventChan if ev.Type == termbox.EventKey { @@ -74,8 +70,9 @@ func main() { } } } - ins := strings.Fields(instructions[curr]) + ins = strings.Fields(instructions[curr]) curr++ + // Optimize multiplication /* // TODO: > cpy b c @@ -84,9 +81,43 @@ func main() { jnz c -2 dec d jnz d -5 - == mult b d a - and curr += 6 + == mlt b d a + and curr += 5 */ + if ins[0] == "cpy" { + + if strings.HasPrefix(instructions[curr+1], "inc") && strings.HasPrefix(instructions[curr+2], "dec") && strings.HasPrefix(instructions[curr+3], "jnz") && strings.HasPrefix(instructions[curr+4], "dec") && strings.HasPrefix(instructions[curr+5], "jnz") { + + allGood := true + // The right instruction set, make sure the arguments match up + ins1 := strings.Fields(instructions[curr+1]) + ins2 := strings.Fields(instructions[curr+2]) + ins3 := strings.Fields(instructions[curr+3]) + ins4 := strings.Fields(instructions[curr+4]) + ins5 := strings.Fields(instructions[curr+5]) + // Make sure all "c"s are correct + if ins[2] != ins2[1] && ins[2] != ins3[1] { + allGood = false + } + if allGood { + // Make sure all "b"s are correct + if ins[1] == ins1[1] || ins[1] == ins2[1] || ins[1] == ins4[1] { + allGood = false + } + } + if allGood { + // Make sure all "d"s are correct + if (ins[1] == ins4[1] || ins[2] == ins4[1]) || ins4[1] != ins5[1] { + allGood = false + } + } + if allGood { + // Go ahead and change the instruction + ins = []string{"mult", ins[1], ins4[1], ins1[1]} + curr += 5 + } + } + } switch ins[0] { case "jnz": // If we have a jnz c -2 it could be moving all of c into another register