code.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) []int {
  70. edge := len(program) - 1
  71. var instructionPointer int
  72. var results []int
  73. for instructionPointer < edge {
  74. switch program[instructionPointer] {
  75. case 0:
  76. registers[0].value = registers[0].value / (powerOfTwo(getCombo(program[instructionPointer+1], registers)))
  77. case 1:
  78. registers[1].value ^= program[instructionPointer+1]
  79. case 2:
  80. registers[1].value = getCombo(program[instructionPointer+1], registers) % 8
  81. case 3:
  82. if registers[0].value > 0 {
  83. instructionPointer = program[instructionPointer+1]
  84. continue
  85. }
  86. case 4:
  87. registers[1].value ^= registers[2].value
  88. case 5:
  89. results = append(results, getCombo(program[instructionPointer+1], registers)%8)
  90. case 6:
  91. registers[1].value = registers[0].value / (getCombo(program[instructionPointer+1], registers) * getCombo(program[instructionPointer+1], registers))
  92. case 7:
  93. registers[3].value = registers[0].value / (getCombo(program[instructionPointer+1], registers) * getCombo(program[instructionPointer+1], registers))
  94. }
  95. instructionPointer += 2
  96. }
  97. return results
  98. }
  99. func arrayToString(arr []int) string {
  100. strSlice := make([]string, len(arr))
  101. for i := range arr {
  102. strSlice[i] = fmt.Sprintf("%d", arr[i])
  103. }
  104. return strings.Join(strSlice, ",")
  105. }
  106. func main() {
  107. if len(os.Args) < 2 {
  108. log.Fatal("You need to specify a file!")
  109. }
  110. filePath := os.Args[1]
  111. file, err := os.Open(filePath)
  112. if err != nil {
  113. log.Fatalf("Failed to open %s!\n", filePath)
  114. }
  115. registers, program := readInput(file)
  116. fmt.Println("Part1:", arrayToString(process(registers, program)))
  117. }