|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "advent-of-code/util" |
| 5 | + "bufio" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +var A, B, C, ogB, ogC int |
| 12 | +var pc int |
| 13 | +var instructions []int |
| 14 | +var output []int |
| 15 | + |
| 16 | +func main() { |
| 17 | + file := util.FileFromArgs() |
| 18 | + scan := bufio.NewScanner(file) |
| 19 | + |
| 20 | + // Read input |
| 21 | + scan.Scan() |
| 22 | + A, _ = strconv.Atoi(scan.Text()[12:]) |
| 23 | + scan.Scan() |
| 24 | + ogB, _ = strconv.Atoi(scan.Text()[12:]) |
| 25 | + scan.Scan() |
| 26 | + ogC, _ = strconv.Atoi(scan.Text()[12:]) |
| 27 | + scan.Scan() |
| 28 | + scan.Scan() |
| 29 | + instructions = util.StringToInts(scan.Text()[9:], ",") |
| 30 | + |
| 31 | + fmt.Println(A, ogB, ogC, instructions) |
| 32 | + B, C = ogB, ogC |
| 33 | + |
| 34 | + // Part 1: Run the program |
| 35 | + for Tick() { |
| 36 | + } |
| 37 | + fmt.Println(strings.Join(util.IntsToStrings(output), ",")) |
| 38 | + |
| 39 | + // Part 2: Find the correct value for A |
| 40 | +part2: |
| 41 | + for i := 0; ; i++ { |
| 42 | + A, B, C, pc = i, ogB, ogC, 0 |
| 43 | + output = []int{} |
| 44 | + |
| 45 | + for Tick() { |
| 46 | + if len(output) > len(instructions) { |
| 47 | + continue part2 |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if len(output) == len(instructions) { |
| 52 | + for j := range output { |
| 53 | + if output[j] != instructions[j] { |
| 54 | + continue part2 |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + fmt.Println(i) |
| 59 | + break |
| 60 | + } |
| 61 | + |
| 62 | + if i%1e7 == 0 { |
| 63 | + fmt.Println(i / 1e7) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Tick executes the next instruction. The function will return true as long as the program is active |
| 69 | +func Tick() bool { |
| 70 | + if pc > len(instructions)-1 { |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + operand := instructions[pc+1] |
| 75 | + if instructions[pc] != 1 && instructions[pc] != 3 && instructions[pc] != 4 { |
| 76 | + switch operand { |
| 77 | + case 4: |
| 78 | + operand = A |
| 79 | + case 5: |
| 80 | + operand = B |
| 81 | + case 6: |
| 82 | + operand = C |
| 83 | + default: |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + operations[instructions[pc]](operand) |
| 88 | + |
| 89 | + pc += 2 |
| 90 | + |
| 91 | + return true |
| 92 | +} |
| 93 | + |
| 94 | +var operations = [8]func(int){ |
| 95 | + 0: func(operand int) { // adv |
| 96 | + A = A / util.IntPow(2, operand) |
| 97 | + }, |
| 98 | + 1: func(operand int) { // bxl |
| 99 | + B = B ^ operand |
| 100 | + }, |
| 101 | + 2: func(operand int) { // bst |
| 102 | + B = operand % 8 |
| 103 | + }, |
| 104 | + 3: func(operand int) { // jnz |
| 105 | + if A != 0 { |
| 106 | + pc = operand - 2 |
| 107 | + } |
| 108 | + }, |
| 109 | + 4: func(operand int) { // bxc |
| 110 | + B = B ^ C |
| 111 | + }, |
| 112 | + 5: func(operand int) { // out |
| 113 | + output = append(output, operand%8) |
| 114 | + }, |
| 115 | + 6: func(operand int) { // bdv |
| 116 | + B = A / util.IntPow(2, operand) |
| 117 | + }, |
| 118 | + 7: func(operand int) { // cdv |
| 119 | + C = A / util.IntPow(2, operand) |
| 120 | + }, |
| 121 | +} |
0 commit comments