day16.go 4.5 KB

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