day16.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 main() {
  90. if len(os.Args) < 2 {
  91. log.Fatal("You need to specify a file!")
  92. }
  93. filePath := os.Args[1]
  94. file, err := os.Open(filePath)
  95. if err != nil {
  96. log.Fatalf("Failed to open %s!\n", filePath)
  97. }
  98. readFile(file)
  99. if err := file.Close(); err != nil {
  100. log.Fatalf("Failed to close file: %s", err)
  101. }
  102. fmt.Println("Part1:", sumBad())
  103. fmt.Println(validTickets)
  104. }