code.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "unicode"
  10. )
  11. type operation struct {
  12. op string
  13. a byte
  14. isBNumber bool
  15. b int
  16. bC byte
  17. }
  18. func readInput(file string) [][]operation {
  19. content, err := ioutil.ReadFile(file)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. lines := strings.Split(string(content), "\n")
  24. var input [][]operation
  25. var operations []operation
  26. for _, line := range lines {
  27. if line == "" {
  28. continue
  29. }
  30. parts := strings.Split(line, " ")
  31. if len(parts) < 2 {
  32. log.Fatal("Invalid line: ", line)
  33. }
  34. if parts[0] == "inp" {
  35. if len(operations) > 0 {
  36. input = append(input, operations)
  37. operations = []operation{}
  38. }
  39. continue
  40. }
  41. current := operation{op: parts[0], a: parts[1][0]}
  42. if len(parts) > 2 {
  43. if unicode.IsDigit(rune(parts[2][0])) {
  44. current.b, err = strconv.Atoi(parts[2])
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. current.isBNumber = true
  49. } else {
  50. current.bC = parts[2][0]
  51. }
  52. }
  53. operations = append(operations, current)
  54. }
  55. if len(operations) > 0 {
  56. input = append(input, operations)
  57. }
  58. return input
  59. }
  60. func do(action operation, variables map[byte]int) bool {
  61. switch action.op {
  62. case "add":
  63. if action.isBNumber {
  64. variables[action.a] += action.b
  65. } else {
  66. variables[action.a] += variables[action.bC]
  67. }
  68. return true
  69. case "mul":
  70. if action.isBNumber {
  71. variables[action.a] *= action.b
  72. } else {
  73. variables[action.a] *= variables[action.bC]
  74. }
  75. return true
  76. case "div":
  77. if action.isBNumber {
  78. if action.b == 0 {
  79. return false
  80. }
  81. variables[action.a] /= action.b
  82. } else {
  83. if variables[action.bC] == 0 {
  84. return false
  85. }
  86. variables[action.a] /= variables[action.bC]
  87. }
  88. return true
  89. case "mod":
  90. if variables[action.a] < 0 {
  91. return false
  92. }
  93. if action.isBNumber {
  94. if action.b <= 0 {
  95. return false
  96. }
  97. variables[action.a] %= action.b
  98. } else {
  99. if variables[action.bC] <= 0 {
  100. return false
  101. }
  102. variables[action.a] %= variables[action.bC]
  103. }
  104. return true
  105. case "eql":
  106. if action.isBNumber {
  107. if variables[action.a] == action.b {
  108. variables[action.a] = 1
  109. } else {
  110. variables[action.a] = 0
  111. }
  112. } else {
  113. if variables[action.a] == variables[action.bC] {
  114. variables[action.a] = 1
  115. } else {
  116. variables[action.a] = 0
  117. }
  118. }
  119. return true
  120. }
  121. return false
  122. }
  123. func printVariables(variables map[byte]int) string {
  124. var result string
  125. for k, v := range variables {
  126. result += fmt.Sprintf("%c: %d ", k, v)
  127. }
  128. return result
  129. }
  130. func doSequence(sequence []operation, variables map[byte]int) bool {
  131. for _, action := range sequence {
  132. if !do(action, variables) {
  133. return false
  134. }
  135. fmt.Println(action, printVariables(variables))
  136. }
  137. return true
  138. }
  139. func part1(input [][]operation) []int {
  140. var number []int
  141. for i := 0; i < 1; i++ {
  142. for j := 9; j >= 1; j-- {
  143. variables := map[byte]int{}
  144. variables['w'] = j
  145. if !doSequence(input[i], variables) {
  146. fmt.Println("Failed for ", j)
  147. }
  148. fmt.Println(printVariables(variables))
  149. if variables['z'] == 0 {
  150. number = append(number, j)
  151. break
  152. }
  153. }
  154. }
  155. return number
  156. }
  157. func main() {
  158. if len(os.Args) < 2 {
  159. log.Fatal("Please provide a file name as argument")
  160. }
  161. input := readInput(os.Args[1])
  162. fmt.Println("Part1:", part1(input))
  163. }