2018 day16 opcode, dependency graph

This commit is contained in:
alexchao26
2020-12-03 22:24:20 -05:00
parent 397aac8fa6
commit ddd3220aea
4 changed files with 4486 additions and 0 deletions
+4092
View File
File diff suppressed because it is too large Load Diff
+222
View File
@@ -0,0 +1,222 @@
package main
import (
"flag"
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
examples, _ := parseInput(input) // ignore instructions for part 1
var threePlusBehaviors int
for _, e := range examples {
var matches int
for _, opcodeFunc := range opcodeNamesToFuncs {
before, instructions, after := e[0], e[1], e[2]
if after == opcodeFunc(before, instructions) {
matches++
}
}
if matches >= 3 {
threePlusBehaviors++
}
}
return threePlusBehaviors
}
func part2(input string) int {
examples, instructions := parseInput(input) // ignore instructions for part 1
// for each num, names that it _COULD_ be
opCodeNumToNameGraph := map[int]map[string]bool{}
for _, e := range examples {
for name, opcodeFunc := range opcodeNamesToFuncs {
before, instructions, after := e[0], e[1], e[2]
if after == opcodeFunc(before, instructions) {
if opCodeNumToNameGraph[instructions[0]] == nil {
opCodeNumToNameGraph[instructions[0]] = map[string]bool{}
}
opCodeNumToNameGraph[instructions[0]][name] = true
}
}
}
derivedOpcodeNumToFunc := map[int]opcodeFunc{}
for len(derivedOpcodeNumToFunc) < 16 {
for num, edges := range opCodeNumToNameGraph {
if len(edges) == 1 {
for name := range edges { // only way to get the one val out of a map?
derivedOpcodeNumToFunc[num] = opcodeNamesToFuncs[name]
// delete name from all other graph edges b/c it's settled
for _, edges := range opCodeNumToNameGraph {
delete(edges, name)
}
}
// break to restart the main loop form the beginning
break
}
}
}
// run all instructions
var registers [4]int
for _, inst := range instructions {
opcodeFunc := derivedOpcodeNumToFunc[inst[0]]
registers = opcodeFunc(registers, inst)
}
return registers[0]
}
func parseInput(input string) ([][3][4]int, [][4]int) {
lines := strings.Split(input, "\n\n\n\n")
inputExamples := lines[0]
inputInstructions := lines[1]
var examples [][3][4]int
for _, e := range strings.Split(inputExamples, "\n\n") {
var before, op, after [4]int
fmt.Sscanf(e, "Before: [%d, %d, %d, %d]\n%d %d %d %d\nAfter: [%d, %d, %d, %d]",
&before[0], &before[1], &before[2], &before[3],
&op[0], &op[1], &op[2], &op[3],
&after[0], &after[1], &after[2], &after[3],
)
examples = append(examples, [3][4]int{before, op, after})
}
var instructions [][4]int
for _, i := range strings.Split(inputInstructions, "\n") {
var inst [4]int
fmt.Sscanf(i, "%d %d %d %d", &inst[0], &inst[1], &inst[2], &inst[3])
instructions = append(instructions, inst)
}
return examples, instructions
}
var opcodeNamesToFuncs = map[string]opcodeFunc{
"addr": addr, "addi": addi,
"multr": multr, "multi": multi,
"banr": banr, "bani": bani,
"borr": borr, "bori": bori,
"setr": setr, "seti": seti,
"gtir": gtir, "gtri": gtri, "gtrr": gtrr,
"eqir": eqir, "eqri": eqri, "eqrr": eqrr,
}
type opcodeFunc func([4]int, [4]int) [4]int
func addr(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] + registers[instructions[2]]
return registers
}
func addi(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] + instructions[2]
return registers
}
func multr(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] * registers[instructions[2]]
return registers
}
func multi(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] * instructions[2]
return registers
}
func banr(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] & registers[instructions[2]]
return registers
}
func bani(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] & instructions[2]
return registers
}
func borr(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] | registers[instructions[2]]
return registers
}
func bori(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]] | instructions[2]
return registers
}
func setr(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = registers[instructions[1]]
return registers
}
func seti(registers [4]int, instructions [4]int) [4]int {
registers[instructions[3]] = instructions[1]
return registers
}
func gtir(registers [4]int, instructions [4]int) [4]int {
if instructions[1] > registers[instructions[2]] {
registers[instructions[3]] = 1
} else {
registers[instructions[3]] = 0
}
return registers
}
func gtri(registers [4]int, instructions [4]int) [4]int {
if registers[instructions[1]] > instructions[2] {
registers[instructions[3]] = 1
} else {
registers[instructions[3]] = 0
}
return registers
}
func gtrr(registers [4]int, instructions [4]int) [4]int {
if registers[instructions[1]] > registers[instructions[2]] {
registers[instructions[3]] = 1
} else {
registers[instructions[3]] = 0
}
return registers
}
func eqir(registers [4]int, instructions [4]int) [4]int {
if instructions[1] == registers[instructions[2]] {
registers[instructions[3]] = 1
} else {
registers[instructions[3]] = 0
}
return registers
}
func eqri(registers [4]int, instructions [4]int) [4]int {
if registers[instructions[1]] == instructions[2] {
registers[instructions[3]] = 1
} else {
registers[instructions[3]] = 0
}
return registers
}
func eqrr(registers [4]int, instructions [4]int) [4]int {
if registers[instructions[1]] == registers[instructions[2]] {
registers[instructions[3]] = 1
} else {
registers[instructions[3]] = 0
}
return registers
}
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
var tests1 = []struct {
name string
want int
input string
}{
{"example", 1, `Before: [3, 2, 1, 1]
9 2 1 2
After: [3, 2, 2, 1]
9 2 0 0`},
{"actual", 607, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
for _, tt := range tests1 {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}
var tests2 = []struct {
name string
want int
input string
}{
{"actual", 577, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
for _, tt := range tests2 {
t.Run(tt.name, func(t *testing.T) {
if got := part2(tt.input); got != tt.want {
t.Errorf("part2() = %v, want %v", got, tt.want)
}
})
}
}
+122
View File
@@ -0,0 +1,122 @@
--- Day 16: Chronal Classification ---
As you see the Elves defend their hot chocolate successfully, you go back to falling through time. This is going to become a problem.
If you're ever going to return to your own time, you need to understand how this device on your wrist works. You have a little while before you reach your next destination, and with a bit of trial and error, you manage to pull up a programming manual on the device's tiny screen.
According to the manual, the device has four registers (numbered 0 through 3) that can be manipulated by instructions containing one of 16 opcodes. The registers start with the value 0.
Every instruction consists of four values: an opcode, two inputs (named A and B), and an output (named C), in that order. The opcode specifies the behavior of the instruction and how the inputs are interpreted. The output, C, is always treated as a register.
In the opcode descriptions below, if something says "value A", it means to take the number given as A literally. (This is also called an "immediate" value.) If something says "register A", it means to use the number given as A to read from (or write to) the register with that number. So, if the opcode addi adds register A and value B, storing the result in register C, and the instruction addi 0 7 3 is encountered, it would add 7 to the value contained by register 0 and store the sum in register 3, never modifying registers 0, 1, or 2 in the process.
Many opcodes are similar except for how they interpret their arguments. The opcodes fall into seven general categories:
Addition:
addr (add register) stores into register C the result of adding register A and register B.
addi (add immediate) stores into register C the result of adding register A and value B.
Multiplication:
mulr (multiply register) stores into register C the result of multiplying register A and register B.
muli (multiply immediate) stores into register C the result of multiplying register A and value B.
Bitwise AND:
banr (bitwise AND register) stores into register C the result of the bitwise AND of register A and register B.
bani (bitwise AND immediate) stores into register C the result of the bitwise AND of register A and value B.
Bitwise OR:
borr (bitwise OR register) stores into register C the result of the bitwise OR of register A and register B.
bori (bitwise OR immediate) stores into register C the result of the bitwise OR of register A and value B.
Assignment:
setr (set register) copies the contents of register A into register C. (Input B is ignored.)
seti (set immediate) stores value A into register C. (Input B is ignored.)
Greater-than testing:
gtir (greater-than immediate/register) sets register C to 1 if value A is greater than register B. Otherwise, register C is set to 0.
gtri (greater-than register/immediate) sets register C to 1 if register A is greater than value B. Otherwise, register C is set to 0.
gtrr (greater-than register/register) sets register C to 1 if register A is greater than register B. Otherwise, register C is set to 0.
Equality testing:
eqir (equal immediate/register) sets register C to 1 if value A is equal to register B. Otherwise, register C is set to 0.
eqri (equal register/immediate) sets register C to 1 if register A is equal to value B. Otherwise, register C is set to 0.
eqrr (equal register/register) sets register C to 1 if register A is equal to register B. Otherwise, register C is set to 0.
Unfortunately, while the manual gives the name of each opcode, it doesn't seem to indicate the number. However, you can monitor the CPU to see the contents of the registers before and after instructions are executed to try to work them out. Each opcode has a number from 0 through 15, but the manual doesn't say which is which. For example, suppose you capture the following sample:
Before: [3, 2, 1, 1]
9 2 1 2
After: [3, 2, 2, 1]
This sample shows the effect of the instruction 9 2 1 2 on the registers. Before the instruction is executed, register 0 has value 3, register 1 has value 2, and registers 2 and 3 have value 1. After the instruction is executed, register 2's value becomes 2.
The instruction itself, 9 2 1 2, means that opcode 9 was executed with A=2, B=1, and C=2. Opcode 9 could be any of the 16 opcodes listed above, but only three of them behave in a way that would cause the result shown in the sample:
Opcode 9 could be mulr: register 2 (which has a value of 1) times register 1 (which has a value of 2) produces 2, which matches the value stored in the output register, register 2.
Opcode 9 could be addi: register 2 (which has a value of 1) plus value 1 produces 2, which matches the value stored in the output register, register 2.
Opcode 9 could be seti: value 2 matches the value stored in the output register, register 2; the number given for B is irrelevant.
None of the other opcodes produce the result captured in the sample. Because of this, the sample above behaves like three opcodes.
You collect many of these samples (the first section of your puzzle input). The manual also includes a small test program (the second section of your puzzle input) - you can ignore it for now.
Ignoring the opcode numbers, how many samples in your puzzle input behave like three or more opcodes?
--- Part Two ---
Using the samples you collected, work out the number of each opcode and execute the test program (the second section of your puzzle input).
What value is contained in register 0 after executing the test program?