code.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. )
  11. type kind byte
  12. const (
  13. old kind = iota
  14. val
  15. )
  16. type variable struct {
  17. t kind
  18. val int
  19. }
  20. type operation struct {
  21. x variable
  22. y variable
  23. action byte
  24. }
  25. type monkey struct {
  26. items []int
  27. op operation
  28. test int
  29. iftrue int
  30. iffalse int
  31. counter int
  32. }
  33. func readItems(line string) []int {
  34. line = strings.Replace(line, " Starting items: ", "", 1)
  35. parts := strings.Split(line, ", ")
  36. var result []int
  37. for i := range parts {
  38. n, err := strconv.Atoi(parts[i])
  39. if err != nil {
  40. log.Fatal("Can't parse", parts[i], err)
  41. }
  42. result = append(result, n)
  43. }
  44. return result
  45. }
  46. func readVariable(text string) variable {
  47. var result variable
  48. if text == "old" {
  49. result.t = old
  50. } else {
  51. result.t = val
  52. n, err := strconv.Atoi(text)
  53. if err != nil {
  54. log.Fatal("Can't parse", text, err)
  55. }
  56. result.val = n
  57. }
  58. return result
  59. }
  60. func readOperation(line string) operation {
  61. line = strings.Replace(line, " Operation: new = ", "", 1)
  62. parts := strings.Split(line, " ")
  63. if len(parts) != 3 {
  64. log.Fatal("Bad operation input:", line)
  65. }
  66. var result operation
  67. result.x = readVariable(parts[0])
  68. result.y = readVariable(parts[2])
  69. result.action = parts[1][0]
  70. return result
  71. }
  72. func readInt(line string, format string) int {
  73. var result int
  74. n, err := fmt.Sscanf(line, format, &result)
  75. if n != 1 || err != nil {
  76. log.Fatal("Can't parse", line, err)
  77. }
  78. return result
  79. }
  80. func readInput(file *os.File) []monkey {
  81. scanner := bufio.NewScanner(file)
  82. counter := 0
  83. var monkeys []monkey
  84. var currentMonkey monkey
  85. for scanner.Scan() {
  86. line := scanner.Text()
  87. if line == "" {
  88. monkeys = append(monkeys, currentMonkey)
  89. counter = 0
  90. currentMonkey = monkey{}
  91. continue
  92. }
  93. switch counter {
  94. case 1:
  95. currentMonkey.items = readItems(line)
  96. case 2:
  97. currentMonkey.op = readOperation(line)
  98. case 3:
  99. currentMonkey.test = readInt(line, " Test: divisible by %d")
  100. case 4:
  101. currentMonkey.iftrue = readInt(line, " If true: throw to monkey %d")
  102. case 5:
  103. currentMonkey.iffalse = readInt(line, " If false: throw to monkey %d")
  104. }
  105. counter++
  106. }
  107. monkeys = append(monkeys, currentMonkey)
  108. return monkeys
  109. }
  110. func performOperation(mon monkey, itemIndex int) int {
  111. var x int
  112. if mon.op.x.t == old {
  113. x = mon.items[itemIndex]
  114. } else {
  115. x = mon.op.x.val
  116. }
  117. var y int
  118. if mon.op.y.t == old {
  119. y = mon.items[itemIndex]
  120. } else {
  121. y = mon.op.y.val
  122. }
  123. if mon.op.action == '+' {
  124. return x + y
  125. }
  126. return x * y
  127. }
  128. func processMonkey(index int, monkeys []monkey, relief int, divider int) []monkey {
  129. for i := range monkeys[index].items {
  130. worryLevel := performOperation(monkeys[index], i) / relief % divider
  131. if worryLevel%monkeys[index].test == 0 {
  132. monkeys[monkeys[index].iftrue].items = append(monkeys[monkeys[index].iftrue].items, worryLevel)
  133. } else {
  134. monkeys[monkeys[index].iffalse].items = append(monkeys[monkeys[index].iffalse].items, worryLevel)
  135. }
  136. monkeys[index].counter++
  137. }
  138. monkeys[index].items = []int{}
  139. return monkeys
  140. }
  141. func process(monkeys []monkey, rounds int, relief int, divider int) int {
  142. for i := 0; i < rounds; i++ {
  143. for m := range monkeys {
  144. monkeys = processMonkey(m, monkeys, relief, divider)
  145. }
  146. }
  147. sort.Slice(monkeys, func(i, j int) bool { return monkeys[i].counter > monkeys[j].counter })
  148. return monkeys[0].counter * monkeys[1].counter
  149. }
  150. func main() {
  151. if len(os.Args) < 2 {
  152. log.Fatal("You need to specify a file!")
  153. }
  154. filePath := os.Args[1]
  155. file, err := os.Open(filePath)
  156. if err != nil {
  157. log.Fatalf("Failed to open %s!\n", filePath)
  158. }
  159. monkeys := readInput(file)
  160. originalMonkeys := make([]monkey, len(monkeys))
  161. for i := range monkeys {
  162. originalMonkeys[i] = monkeys[i]
  163. }
  164. divider := 1
  165. for i := range monkeys {
  166. divider *= monkeys[i].test
  167. }
  168. fmt.Println("Part1:", process(monkeys, 20, 3, divider))
  169. fmt.Println("Part2:", process(originalMonkeys, 10000, 1, divider))
  170. }