code.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type kind byte
  10. const (
  11. op kind = iota
  12. val
  13. )
  14. type monkey struct {
  15. name string
  16. spec kind
  17. left string
  18. right string
  19. op byte
  20. value int
  21. }
  22. func readInput(file *os.File) map[string]monkey {
  23. scanner := bufio.NewScanner(file)
  24. monkeys := make(map[string]monkey)
  25. for scanner.Scan() {
  26. line := scanner.Text()
  27. if line == "" {
  28. continue
  29. }
  30. var current monkey
  31. if strings.ContainsAny(line, "+-/*") {
  32. current.spec = op
  33. n, err := fmt.Sscanf(line, "%s %s %c %s", &current.name, &current.left, &current.op, &current.right)
  34. if n != 4 || err != nil {
  35. log.Fatal("Can't parse (op): ", line, err)
  36. }
  37. } else {
  38. current.spec = val
  39. n, err := fmt.Sscanf(line, "%s %d", &current.name, &current.value)
  40. if n != 2 || err != nil {
  41. log.Fatal("Can't parse: ", line, err)
  42. }
  43. }
  44. current.name = strings.TrimRight(current.name, ":")
  45. monkeys[current.name] = current
  46. }
  47. return monkeys
  48. }
  49. func processMonkey(being monkey, monkeys map[string]monkey) int {
  50. if being.spec == val {
  51. return being.value
  52. }
  53. switch being.op {
  54. case '+':
  55. return processMonkey(monkeys[being.left], monkeys) + processMonkey(monkeys[being.right], monkeys)
  56. case '-':
  57. return processMonkey(monkeys[being.left], monkeys) - processMonkey(monkeys[being.right], monkeys)
  58. case '*':
  59. return processMonkey(monkeys[being.left], monkeys) * processMonkey(monkeys[being.right], monkeys)
  60. case '/':
  61. return processMonkey(monkeys[being.left], monkeys) / processMonkey(monkeys[being.right], monkeys)
  62. }
  63. return 0
  64. }
  65. func main() {
  66. if len(os.Args) < 2 {
  67. log.Fatal("You need to specify a file!")
  68. }
  69. filePath := os.Args[1]
  70. file, err := os.Open(filePath)
  71. if err != nil {
  72. log.Fatalf("Failed to open %s!\n", filePath)
  73. }
  74. monkeys := readInput(file)
  75. fmt.Println("Part1:", processMonkey(monkeys["root"], monkeys))
  76. }