code.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type Register struct {
  11. id byte
  12. value int
  13. }
  14. func readInput(file *os.File) ([]Register, []int) {
  15. scanner := bufio.NewScanner(file)
  16. var registers []Register
  17. var program []int
  18. var registersRead bool
  19. for scanner.Scan() {
  20. line := scanner.Text()
  21. if line == "" {
  22. registersRead = true
  23. continue
  24. }
  25. if !registersRead {
  26. var register Register
  27. n, err := fmt.Sscanf(line, "Register %c: %d", &register.id, &register.value)
  28. if n != 2 || err != nil {
  29. log.Fatalf("Not able to parse register '%s': %s", line, err)
  30. }
  31. registers = append(registers, register)
  32. } else {
  33. numString := strings.TrimPrefix(line, "Program: ")
  34. parts := strings.Split(numString, ",")
  35. for _, part := range parts {
  36. num, err := strconv.Atoi(part)
  37. if err != nil {
  38. log.Fatalf("Not able to convert %s: %s", part, err)
  39. }
  40. program = append(program, num)
  41. }
  42. }
  43. }
  44. return registers, program
  45. }
  46. func getCombo(operand int, registers []Register) int {
  47. if operand >= 0 && operand <= 3 {
  48. return operand
  49. }
  50. switch operand {
  51. case 4:
  52. return registers[0].value
  53. case 5:
  54. return registers[1].value
  55. case 6:
  56. return registers[2].value
  57. case 7:
  58. log.Fatal("Bad instruction!")
  59. }
  60. return -1000000
  61. }
  62. func powerOfTwo(power int) int {
  63. result := 1
  64. for i := 0; i < power; i++ {
  65. result *= 2
  66. }
  67. return result
  68. }
  69. func process(registers []Register, program []int, withCheck bool) []int {
  70. edge := len(program) - 1
  71. var instructionPointer int
  72. var results []int
  73. var resultIndex int
  74. for instructionPointer < edge {
  75. switch program[instructionPointer] {
  76. case 0:
  77. registers[0].value = registers[0].value / (powerOfTwo(getCombo(program[instructionPointer+1], registers)))
  78. case 1:
  79. registers[1].value ^= program[instructionPointer+1]
  80. case 2:
  81. registers[1].value = getCombo(program[instructionPointer+1], registers) % 8
  82. case 3:
  83. if registers[0].value > 0 {
  84. instructionPointer = program[instructionPointer+1]
  85. continue
  86. }
  87. case 4:
  88. registers[1].value ^= registers[2].value
  89. case 5:
  90. result := getCombo(program[instructionPointer+1], registers) % 8
  91. if withCheck && result != program[resultIndex] {
  92. return []int{}
  93. }
  94. results = append(results, result)
  95. resultIndex++
  96. case 6:
  97. registers[1].value = registers[0].value / (powerOfTwo(getCombo(program[instructionPointer+1], registers)))
  98. case 7:
  99. registers[2].value = registers[0].value / (powerOfTwo(getCombo(program[instructionPointer+1], registers)))
  100. }
  101. instructionPointer += 2
  102. }
  103. return results
  104. }
  105. func arrayToString(arr []int) string {
  106. strSlice := make([]string, len(arr))
  107. for i := range arr {
  108. strSlice[i] = fmt.Sprintf("%d", arr[i])
  109. }
  110. return strings.Join(strSlice, ",")
  111. }
  112. func part2(registers []Register, program []int, initialA int) int {
  113. registers[0].value = initialA
  114. modifier := 1
  115. for {
  116. registers[0].value = initialA + modifier
  117. registers[1].value = 0
  118. registers[2].value = 0
  119. arr := process(registers, program, true)
  120. if len(arr) > 0 && len(arr) == len(program) {
  121. fmt.Println(arr, program)
  122. return initialA + modifier
  123. }
  124. modifier++
  125. }
  126. return -1
  127. }
  128. func main() {
  129. if len(os.Args) < 2 {
  130. log.Fatal("You need to specify a file!")
  131. }
  132. filePath := os.Args[1]
  133. file, err := os.Open(filePath)
  134. if err != nil {
  135. log.Fatalf("Failed to open %s!\n", filePath)
  136. }
  137. registers, program := readInput(file)
  138. initialA := registers[0].value
  139. fmt.Println("Part1:", arrayToString(process(registers, program, false)))
  140. fmt.Println("Part2:", part2(registers, program, initialA))
  141. }