From d6163b873e9c3aca3e01a2a5670806948dea37a1 Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Tue, 8 Dec 2020 18:51:29 -0500 Subject: [PATCH] refactored intcode --- 2020/day08/intcode.go | 61 +++++++++++++++++++++++ 2020/day08/main.go | 53 ++------------------ 2020/day08/prompt.md | 111 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 50 deletions(-) create mode 100644 2020/day08/intcode.go create mode 100755 2020/day08/prompt.md diff --git a/2020/day08/intcode.go b/2020/day08/intcode.go new file mode 100644 index 0000000..426cb4b --- /dev/null +++ b/2020/day08/intcode.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "strings" +) + +func newComputerFromInput(input string) computer { + var instructions []instruction + + for _, l := range strings.Split(input, "\n") { + inst := instruction{} + fmt.Sscanf(l, "%s %d", &inst.operation, &inst.argument) + instructions = append(instructions, inst) + } + + return computer{instructions: instructions} +} + +type computer struct { + instructions []instruction + index int + accumulator int +} + +type instruction struct { + operation string + argument int +} + +func (c *computer) step() { + switch inst := c.instructions[c.index]; inst.operation { + case "acc": + c.accumulator += inst.argument + c.index++ + case "jmp": + c.index += inst.argument + case "nop": + c.index++ + default: + panic("unhandled operation type" + inst.operation) + } +} + +// func isInfiniteLoop(comp computer) (finalAccumulatorVal int, isLoop bool) { +// ranInstructionsIndices := map[int]bool{} +// for comp.index < len(comp.instructions) { +// nextInst := comp.index +// // is an infinite loop, return out +// if ranInstructionsIndices[nextInst] { +// return 0, true +// } +// ranInstructionsIndices[nextInst] = true + +// comp.step() +// } + +// // instructions finished, return final accumulator & indicate it was not an +// // infinite loop +// return comp.accumulator, false +// } diff --git a/2020/day08/main.go b/2020/day08/main.go index db8cb25..cdec30f 100644 --- a/2020/day08/main.go +++ b/2020/day08/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "strings" "github.com/alexchao26/advent-of-code-go/util" ) @@ -51,11 +50,11 @@ func part2(input string) int { newComputer := newComputerFromInput(input) // flip this index's instruction if a jmp or nop - switch newComputer.instructions[i].instType { + switch newComputer.instructions[i].operation { case "jmp": - newComputer.instructions[i].instType = "nop" + newComputer.instructions[i].operation = "nop" case "nop": - newComputer.instructions[i].instType = "jmp" + newComputer.instructions[i].operation = "jmp" case "acc": continue } @@ -71,52 +70,6 @@ func part2(input string) int { return -1 } -type instruction struct { - instType string - value int -} - -func newComputerFromInput(input string) computer { - var instructions []instruction - - lines := strings.Split(input, "\n") - for _, l := range lines { - inst := instruction{} - fmt.Sscanf(l, "%s %d", &inst.instType, &inst.value) - instructions = append(instructions, inst) - } - - return computer{instructions: instructions} -} - -type computer struct { - instructions []instruction - index int - accumulator int -} - -func (c *computer) acc(val int) { - c.accumulator += val - c.index++ -} -func (c *computer) jmp(val int) { - c.index += val -} -func (c *computer) nop(val int) { - c.index++ -} - -func (c *computer) step() { - switch inst := c.instructions[c.index]; inst.instType { - case "acc": - c.acc(inst.value) - case "jmp": - c.jmp(inst.value) - case "nop": - c.nop(inst.value) - } -} - func isInfiniteLoop(comp computer) (finalAccumulatorVal int, isLoop bool) { ranInstructionsIndices := map[int]bool{} for comp.index < len(comp.instructions) { diff --git a/2020/day08/prompt.md b/2020/day08/prompt.md new file mode 100755 index 0000000..4403fbc --- /dev/null +++ b/2020/day08/prompt.md @@ -0,0 +1,111 @@ + +--- Day 8: Handheld Halting --- +Your flight to the major airline hub reaches cruising altitude without incident. While you consider checking the in-flight menu for one of those drinks that come with a little umbrella, you are interrupted by the kid sitting next to you. + + +Their handheld game console won't turn on! They ask if you can take a look. + + +You narrow the problem down to a strange infinite loop in the boot code (your puzzle input) of the device. You should be able to fix it, but first you need to be able to run the code in isolation. + + +The boot code is represented as a text file with one instruction per line of text. Each instruction consists of an operation (acc, jmp, or nop) and an argument (a signed number like +4 or -20). + + + +acc increases or decreases a single global value called the accumulator by the value given in the argument. For example, acc +7 would increase the accumulator by 7. The accumulator starts at 0. After an acc instruction, the instruction immediately below it is executed next. +jmp jumps to a new instruction relative to itself. The next instruction to execute is found using the argument as an offset from the jmp instruction; for example, jmp +2 would skip the next instruction, jmp +1 would continue to the instruction immediately below it, and jmp -20 would cause the instruction 20 lines above to be executed next. +nop stands for No OPeration - it does nothing. The instruction immediately below it is executed next. + + + +For example, consider the following program: + + +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 + + + +These instructions are visited in this order: + + +nop +0 | 1 +acc +1 | 2, 8(!) +jmp +4 | 3 +acc +3 | 6 +jmp -3 | 7 +acc -99 | +acc +1 | 4 +jmp -4 | 5 +acc +6 | + + + +First, the nop +0 does nothing. Then, the accumulator is increased from 0 to 1 (acc +1) and jmp +4 sets the next instruction to the other acc +1 near the bottom. After it increases the accumulator from 1 to 2, jmp -4 executes, setting the next instruction to the only acc +3. It sets the accumulator to 5, and jmp -3 causes the program to continue back at the first acc +1. + + +This is an infinite loop: with this sequence of jumps, the program will run forever. The moment the program tries to run any instruction a second time, you know it will never terminate. + + +Immediately before the program would run an instruction a second time, the value in the accumulator is 5. + + +Run your copy of the boot code. Immediately before any instruction is executed a second time, what value is in the accumulator? + + +--- Part Two --- +After some careful analysis, you believe that exactly one instruction is corrupted. + + +Somewhere in the program, either a jmp is supposed to be a nop, or a nop is supposed to be a jmp. (No acc instructions were harmed in the corruption of this boot code.) + + +The program is supposed to terminate by attempting to execute an instruction immediately after the last instruction in the file. By changing exactly one jmp or nop, you can repair the boot code and make it terminate correctly. + + +For example, consider the same program from above: + + +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 + + + +If you change the first instruction from nop +0 to jmp +0, it would create a single-instruction infinite loop, never leaving that instruction. If you change almost any of the jmp instructions, the program will still eventually find another jmp instruction and loop forever. + + +However, if you change the second-to-last instruction (from jmp -4 to nop -4), the program terminates! The instructions are visited in this order: + + +nop +0 | 1 +acc +1 | 2 +jmp +4 | 3 +acc +3 | +jmp -3 | +acc -99 | +acc +1 | 4 +nop -4 | 5 +acc +6 | 6 + + + +After the last instruction (acc +6), the program terminates by attempting to run the instruction below the last instruction in the file. With this change, after the program terminates, the accumulator contains the value 8 (acc +1, acc +1, acc +6). + + +Fix the program so that it terminates normally by changing exactly one jmp (to nop) or nop (to jmp). What is the value of the accumulator after the program terminates? +