code.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. return true
  66. } else {
  67. variables[action.a] += variables[action.bC]
  68. return true
  69. }
  70. case "mul":
  71. if action.isBNumber {
  72. variables[action.a] *= action.b
  73. return true
  74. } else {
  75. variables[action.a] *= variables[action.bC]
  76. return true
  77. }
  78. case "div":
  79. if action.isBNumber {
  80. if action.b == 0 {
  81. return false
  82. }
  83. variables[action.a] /= action.b
  84. } else {
  85. if variables[action.bC] == 0 {
  86. return false
  87. }
  88. variables[action.a] /= variables[action.bC]
  89. }
  90. return true
  91. case "mod":
  92. if variables[action.a] < 0 {
  93. return false
  94. }
  95. if action.isBNumber {
  96. if action.b <= 0 {
  97. return false
  98. }
  99. variables[action.a] %= action.b
  100. } else {
  101. if variables[action.bC] <= 0 {
  102. return false
  103. }
  104. variables[action.a] %= variables[action.bC]
  105. }
  106. return true
  107. case "eql":
  108. if action.isBNumber {
  109. if variables[action.a] == action.b {
  110. variables[action.a] = 1
  111. } else {
  112. variables[action.a] = 0
  113. }
  114. } else {
  115. if variables[action.a] == variables[action.bC] {
  116. variables[action.a] = 1
  117. } else {
  118. variables[action.a] = 0
  119. }
  120. }
  121. return true
  122. }
  123. return false
  124. }
  125. func doSequence(sequence []operation, variables map[byte]int) bool {
  126. for _, action := range sequence {
  127. if !do(action, variables) {
  128. fmt.Println(action, variables)
  129. return false
  130. }
  131. }
  132. return true
  133. }
  134. func part1(input [][]operation) []int {
  135. var number []int
  136. for i := 0; i < 14; i++ {
  137. for j := 9; j >= 1; j-- {
  138. variables := map[byte]int{}
  139. variables['w'] = j
  140. if !doSequence(input[i], map[byte]int{}) {
  141. fmt.Println("Failed for ", j)
  142. }
  143. if variables['z'] == 0 {
  144. number = append(number, j)
  145. break
  146. }
  147. }
  148. }
  149. return number
  150. }
  151. func main() {
  152. if len(os.Args) < 2 {
  153. log.Fatal("Please provide a file name as argument")
  154. }
  155. input := readInput(os.Args[1])
  156. fmt.Println("Part1:", part1(input))
  157. }