day19.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type rule struct {
  11. value string
  12. mapping [][]int
  13. }
  14. var rules map[int]rule
  15. func init() {
  16. rules = make(map[int]rule)
  17. }
  18. func readRule(line string) {
  19. var newRule rule
  20. parts := strings.Split(line, ": ")
  21. if len(parts) != 2 {
  22. log.Fatalf("Invalid line: %s", line)
  23. }
  24. id, err := strconv.Atoi(parts[0])
  25. if err != nil {
  26. log.Fatalf("Error processing rule id for %s: %s", line, err)
  27. }
  28. var currentMapping []int
  29. for _, item := range strings.Split(parts[1], " ") {
  30. if strings.Contains(item, "\"") {
  31. newRule.value = strings.ReplaceAll(item, "\"", "")
  32. break
  33. }
  34. if item == "|" {
  35. newRule.mapping = append(newRule.mapping, currentMapping)
  36. currentMapping = []int{}
  37. continue
  38. }
  39. itemID, err := strconv.Atoi(item)
  40. if err != nil {
  41. log.Fatalf("Error processing id for %s: %s", item, err)
  42. }
  43. currentMapping = append(currentMapping, itemID)
  44. }
  45. newRule.mapping = append(newRule.mapping, currentMapping)
  46. rules[id] = newRule
  47. }
  48. var messages []string
  49. func readFile(file *os.File) {
  50. scanner := bufio.NewScanner(file)
  51. currentFunction := readRule
  52. changed := false
  53. for scanner.Scan() {
  54. line := scanner.Text()
  55. if line == "" {
  56. if changed {
  57. break
  58. }
  59. currentFunction = func(line string) { messages = append(messages, line) }
  60. changed = true
  61. continue
  62. }
  63. currentFunction(line)
  64. }
  65. if err := scanner.Err(); err != nil {
  66. log.Fatalf("Scanner error: %s", err)
  67. }
  68. }
  69. func buildStringsForRule(ruleID int, stringsSoFar []string) []string {
  70. var newStrings []string
  71. if rules[ruleID].value != "" {
  72. for _, item := range stringsSoFar {
  73. item += rules[ruleID].value
  74. newStrings = append(newStrings, item)
  75. }
  76. return newStrings
  77. }
  78. for _, item := range stringsSoFar {
  79. for _, array := range rules[ruleID].mapping {
  80. partial := []string{item}
  81. for _, id := range array {
  82. partial = buildStringsForRule(id, partial)
  83. }
  84. newStrings = append(newStrings, partial...)
  85. }
  86. }
  87. return newStrings
  88. }
  89. var notMatched []string
  90. func part1(validStrings []string) int {
  91. valid := 0
  92. for _, message := range messages {
  93. matched := false
  94. for _, item := range validStrings {
  95. if message == item {
  96. valid++
  97. matched = true
  98. break
  99. }
  100. }
  101. if !matched {
  102. notMatched = append(notMatched, message)
  103. }
  104. }
  105. return valid
  106. }
  107. func main() {
  108. if len(os.Args) < 2 {
  109. log.Fatal("You need to specify a file!")
  110. }
  111. filePath := os.Args[1]
  112. file, err := os.Open(filePath)
  113. if err != nil {
  114. log.Fatalf("Failed to open %s!\n", filePath)
  115. }
  116. readFile(file)
  117. if err := file.Close(); err != nil {
  118. log.Fatalf("Failed to close file: %s", err)
  119. }
  120. validStrings := buildStringsForRule(0, []string{""})
  121. fmt.Println("Part1:", part1(validStrings))
  122. fmt.Println(notMatched)
  123. fmt.Println("42:", buildStringsForRule(42, []string{""}))
  124. fmt.Println("31:", buildStringsForRule(31, []string{""}))
  125. }