code.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 process(registers []Register, program []int) []int {
  63. edge := len(program) - 1
  64. var instructionPointer int
  65. var results []int
  66. for instructionPointer < edge {
  67. switch program[instructionPointer] {
  68. case 0:
  69. registers[0].value = registers[0].value / (getCombo(program[instructionPointer+1], registers) * getCombo(program[instructionPointer+1], registers))
  70. case 1:
  71. registers[1].value ^= program[instructionPointer+1]
  72. case 2:
  73. registers[1].value = getCombo(program[instructionPointer+1], registers) % 8
  74. case 3:
  75. if registers[0].value > 0 {
  76. instructionPointer = program[instructionPointer+1]
  77. continue
  78. }
  79. case 4:
  80. registers[1].value ^= registers[2].value
  81. case 5:
  82. results = append(results, getCombo(program[instructionPointer+1], registers)%8)
  83. case 6:
  84. registers[1].value = registers[0].value / (getCombo(program[instructionPointer+1], registers) * getCombo(program[instructionPointer+1], registers))
  85. case 7:
  86. registers[3].value = registers[0].value / (getCombo(program[instructionPointer+1], registers) * getCombo(program[instructionPointer+1], registers))
  87. }
  88. instructionPointer += 2
  89. fmt.Println(instructionPointer, registers, results)
  90. }
  91. return results
  92. }
  93. func main() {
  94. if len(os.Args) < 2 {
  95. log.Fatal("You need to specify a file!")
  96. }
  97. filePath := os.Args[1]
  98. file, err := os.Open(filePath)
  99. if err != nil {
  100. log.Fatalf("Failed to open %s!\n", filePath)
  101. }
  102. registers, program := readInput(file)
  103. fmt.Println(process(registers, program))
  104. }