code.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. const delta byte = 97
  10. func readInput(file *os.File) ([][]string, []string) {
  11. scanner := bufio.NewScanner(file)
  12. patterns := make([][]string, 25)
  13. var towels []string
  14. var patternsRead bool
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. patternsRead = true
  19. continue
  20. }
  21. if !patternsRead {
  22. parts := strings.Split(line, ", ")
  23. for _, part := range parts {
  24. patterns[part[0]-delta] = append(patterns[part[0]-delta], part)
  25. }
  26. } else {
  27. towels = append(towels, line)
  28. }
  29. }
  30. return patterns, towels
  31. }
  32. func patternAndIndex(pattern string, index int) string {
  33. return fmt.Sprintf("%s_%d", pattern, index)
  34. }
  35. func checkTowel(towel string, index int, patterns [][]string, checked map[string]bool) bool {
  36. if index >= len(towel) {
  37. return true
  38. }
  39. for _, pattern := range patterns[towel[index]-delta] {
  40. if checked[patternAndIndex(pattern, index)] {
  41. continue
  42. }
  43. checked[patternAndIndex(pattern, index)] = true
  44. patternMatch := true
  45. for i := range pattern {
  46. if index+i >= len(towel) || pattern[i] != towel[index+i] {
  47. patternMatch = false
  48. break
  49. }
  50. }
  51. if patternMatch && checkTowel(towel, index+len(pattern), patterns, checked) {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. func getPossibleTowers(patterns [][]string, towels []string) []string {
  58. var possible []string
  59. for _, towel := range towels {
  60. checked := make(map[string]bool)
  61. if checkTowel(towel, 0, patterns, checked) {
  62. possible = append(possible, towel)
  63. }
  64. }
  65. return possible
  66. }
  67. func checkTowel2(towel string, index int, patterns [][]string, badOnes map[string]bool) int {
  68. var count int
  69. if index >= len(towel) {
  70. return 1
  71. }
  72. edge := len(towel) - 1
  73. for _, pattern := range patterns[towel[index]-delta] {
  74. if index+len(pattern)-1 > edge {
  75. badOnes[patternAndIndex(pattern, index)] = true
  76. continue
  77. }
  78. if badOnes[patternAndIndex(pattern, index)] {
  79. continue
  80. }
  81. patternMatch := true
  82. for i := range pattern {
  83. if index+i >= len(towel) || pattern[i] != towel[index+i] {
  84. patternMatch = false
  85. break
  86. }
  87. }
  88. if patternMatch {
  89. count += checkTowel2(towel, index+len(pattern), patterns, badOnes)
  90. } else {
  91. badOnes[patternAndIndex(pattern, index)] = true
  92. }
  93. }
  94. return count
  95. }
  96. func getMax(patterns [][]string) int {
  97. var maxPatterns int
  98. for _, letter := range patterns {
  99. maxPatterns += len(letter)
  100. }
  101. return maxPatterns
  102. }
  103. func part2(patterns [][]string, towels []string) int {
  104. var count int
  105. for _, towel := range towels {
  106. badOnes := make(map[string]bool)
  107. count += checkTowel2(towel, 0, patterns, badOnes)
  108. }
  109. return count
  110. }
  111. func main() {
  112. if len(os.Args) < 2 {
  113. log.Fatal("You need to specify a file!")
  114. }
  115. filePath := os.Args[1]
  116. file, err := os.Open(filePath)
  117. if err != nil {
  118. log.Fatalf("Failed to open %s!\n", filePath)
  119. }
  120. patterns, towels := readInput(file)
  121. possible := getPossibleTowers(patterns, towels)
  122. fmt.Println("Part1:", len(possible))
  123. fmt.Println("Part2:", part2(patterns, possible))
  124. }