day16.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type rule struct {
  11. name string
  12. firstSegment [2]int
  13. secondSegment [2]int
  14. }
  15. var rules []rule
  16. func readRule(line string) {
  17. var newRule rule
  18. parts := strings.Split(line, ":")
  19. if len(parts) != 2 {
  20. log.Fatalf("Invalid line: %s", line)
  21. }
  22. newRule.name = parts[0]
  23. n, err := fmt.Sscanf(parts[1], "%d-%d or %d-%d\n", &newRule.firstSegment[0], &newRule.firstSegment[1], &newRule.secondSegment[0], &newRule.secondSegment[1])
  24. if err != nil || n != 4 {
  25. log.Fatalf("Error scanning '%s': %s", line, err)
  26. }
  27. rules = append(rules, newRule)
  28. }
  29. type ticket []int
  30. var tickets []ticket
  31. func readTicket(line string) {
  32. var newTicket ticket
  33. for _, item := range strings.Split(line, ",") {
  34. field, err := strconv.Atoi(item)
  35. if err != nil {
  36. log.Fatalf("Error parsing field from %s: %s", item, err)
  37. }
  38. newTicket = append(newTicket, field)
  39. }
  40. tickets = append(tickets, newTicket)
  41. }
  42. func readFile(file *os.File) {
  43. scanner := bufio.NewScanner(file)
  44. currentFunction := readRule
  45. for scanner.Scan() {
  46. line := scanner.Text()
  47. if line == "" {
  48. continue
  49. }
  50. if strings.Contains(line, "your ticket:") {
  51. currentFunction = readTicket
  52. continue
  53. }
  54. if strings.Contains(line, "nearby tickets:") {
  55. continue
  56. }
  57. currentFunction(line)
  58. }
  59. if err := scanner.Err(); err != nil {
  60. log.Fatalf("Scanner error: %s", err)
  61. }
  62. }
  63. func checkAllRulesOnField(field int) bool {
  64. for _, currentRule := range rules {
  65. if (field >= currentRule.firstSegment[0] && field <= currentRule.firstSegment[1]) || (field >= currentRule.secondSegment[0] && field <= currentRule.secondSegment[1]) {
  66. return true
  67. }
  68. }
  69. return false
  70. }
  71. var validTickets []ticket
  72. func sumBad() int {
  73. numberOfTickets := len(tickets)
  74. sum := 0
  75. for i := 1; i < numberOfTickets; i++ {
  76. validTicket := true
  77. for _, field := range tickets[i] {
  78. if !checkAllRulesOnField(field) {
  79. sum += field
  80. validTicket = false
  81. }
  82. }
  83. if validTicket {
  84. validTickets = append(validTickets, tickets[i])
  85. }
  86. }
  87. return sum
  88. }
  89. func checkRuleOnField(currentRule rule, field int) bool {
  90. if (field >= currentRule.firstSegment[0] && field <= currentRule.firstSegment[1]) || (field >= currentRule.secondSegment[0] && field <= currentRule.secondSegment[1]) {
  91. return true
  92. }
  93. return false
  94. }
  95. var fieldsAndRules []int
  96. func ruleTaken(ruleID int) bool {
  97. for _, item := range fieldsAndRules {
  98. if item == ruleID {
  99. return true
  100. }
  101. }
  102. return false
  103. }
  104. func establishOrder() {
  105. numberOfFields := len(validTickets[0])
  106. numberOfRules := len(rules)
  107. for i := 0; i < numberOfFields; i++ {
  108. j := 0
  109. for ; j < numberOfRules; j++ {
  110. if ruleTaken(j) {
  111. continue
  112. }
  113. ruleValid := true
  114. for _, item := range validTickets {
  115. if !checkRuleOnField(rules[j], item[i]) {
  116. ruleValid = false
  117. break
  118. }
  119. }
  120. if ruleValid {
  121. fieldsAndRules = append(fieldsAndRules, j)
  122. break
  123. }
  124. }
  125. }
  126. }
  127. func checkMyTicket() int {
  128. myTicket := tickets[0]
  129. result := 1
  130. for i, ruleID := range fieldsAndRules {
  131. if !strings.Contains(rules[ruleID].name, "departure") {
  132. continue
  133. }
  134. result *= myTicket[i]
  135. }
  136. return result
  137. }
  138. func main() {
  139. if len(os.Args) < 2 {
  140. log.Fatal("You need to specify a file!")
  141. }
  142. filePath := os.Args[1]
  143. file, err := os.Open(filePath)
  144. if err != nil {
  145. log.Fatalf("Failed to open %s!\n", filePath)
  146. }
  147. readFile(file)
  148. if err := file.Close(); err != nil {
  149. log.Fatalf("Failed to close file: %s", err)
  150. }
  151. fmt.Println("Part1:", sumBad())
  152. establishOrder()
  153. fmt.Println("Part2:", checkMyTicket())
  154. }