code.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, checked map[string]int, maxPatterns int) int {
  68. var count int
  69. if index >= len(towel) {
  70. return 1
  71. }
  72. for _, pattern := range patterns[towel[index]-delta] {
  73. if checked[patternAndIndex(pattern, index)] > maxPatterns {
  74. continue
  75. }
  76. checked[patternAndIndex(pattern, index)]++
  77. patternMatch := true
  78. for i := range pattern {
  79. if index+i >= len(towel) || pattern[i] != towel[index+i] {
  80. patternMatch = false
  81. break
  82. }
  83. }
  84. if patternMatch {
  85. count += checkTowel2(towel, index+len(pattern), patterns, checked, maxPatterns)
  86. }
  87. }
  88. return count
  89. }
  90. func getMax(patterns [][]string) int {
  91. var maxPatterns int
  92. for _, letter := range patterns {
  93. maxPatterns += len(letter)
  94. }
  95. return maxPatterns
  96. }
  97. func part2(patterns [][]string, towels []string) int {
  98. var count int
  99. maxPatterns := getMax(patterns) * 2
  100. for _, towel := range towels {
  101. checked := make(map[string]int)
  102. count += checkTowel2(towel, 0, patterns, checked, maxPatterns)
  103. }
  104. return count
  105. }
  106. func main() {
  107. if len(os.Args) < 2 {
  108. log.Fatal("You need to specify a file!")
  109. }
  110. filePath := os.Args[1]
  111. file, err := os.Open(filePath)
  112. if err != nil {
  113. log.Fatalf("Failed to open %s!\n", filePath)
  114. }
  115. patterns, towels := readInput(file)
  116. possible := getPossibleTowers(patterns, towels)
  117. fmt.Println("Part1:", len(possible))
  118. fmt.Println("Part2:", part2(patterns, possible))
  119. }