code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type kind byte
  11. const (
  12. old kind = iota
  13. val
  14. )
  15. type variable struct {
  16. t kind
  17. val int
  18. }
  19. type operation struct {
  20. x variable
  21. y variable
  22. action byte
  23. }
  24. type monkey struct {
  25. items []int
  26. op operation
  27. test int
  28. iftrue int
  29. iffalse int
  30. }
  31. func readItems(line string) []int {
  32. line = strings.Replace(line, " Starting items: ", "", 1)
  33. parts := strings.Split(line, ", ")
  34. var result []int
  35. for i := range parts {
  36. n, err := strconv.Atoi(parts[i])
  37. if err != nil {
  38. log.Fatal("Can't parse", parts[i], err)
  39. }
  40. result = append(result, n)
  41. }
  42. return result
  43. }
  44. func readVariable(text string) variable {
  45. var result variable
  46. if text == "old" {
  47. result.t = old
  48. } else {
  49. result.t = val
  50. n, err := strconv.Atoi(text)
  51. if err != nil {
  52. log.Fatal("Can't parse", text, err)
  53. }
  54. result.val = n
  55. }
  56. return result
  57. }
  58. func readOperation(line string) operation {
  59. line = strings.Replace(line, " Operation: new = ", "", 1)
  60. parts := strings.Split(line, " ")
  61. if len(parts) != 3 {
  62. log.Fatal("Bad operation input:", line)
  63. }
  64. var result operation
  65. result.x = readVariable(parts[0])
  66. result.y = readVariable(parts[2])
  67. result.action = parts[1][0]
  68. return result
  69. }
  70. func readInt(line string, format string) int {
  71. var result int
  72. n, err := fmt.Sscanf(line, format, &result)
  73. if n != 1 || err != nil {
  74. log.Fatal("Can't parse", line, err)
  75. }
  76. return result
  77. }
  78. func readInput(file *os.File) []monkey {
  79. scanner := bufio.NewScanner(file)
  80. counter := 0
  81. var monkeys []monkey
  82. var currentMonkey monkey
  83. for scanner.Scan() {
  84. line := scanner.Text()
  85. if line == "" {
  86. monkeys = append(monkeys, currentMonkey)
  87. counter = 0
  88. currentMonkey = monkey{}
  89. continue
  90. }
  91. switch counter {
  92. case 1:
  93. currentMonkey.items = readItems(line)
  94. case 2:
  95. currentMonkey.op = readOperation(line)
  96. case 3:
  97. currentMonkey.test = readInt(line, " Test: divisible by %d")
  98. case 4:
  99. currentMonkey.iftrue = readInt(line, " If true: throw to monkey %d")
  100. case 5:
  101. currentMonkey.iffalse = readInt(line, " If false: throw to monkey %d")
  102. }
  103. counter++
  104. }
  105. monkeys = append(monkeys, currentMonkey)
  106. return monkeys
  107. }
  108. func main() {
  109. if len(os.Args) < 2 {
  110. log.Fatal("You need to specify a file!")
  111. }
  112. filePath := os.Args[1]
  113. file, err := os.Open(filePath)
  114. if err != nil {
  115. log.Fatalf("Failed to open %s!\n", filePath)
  116. }
  117. monkeys := readInput(file)
  118. fmt.Println(monkeys)
  119. }