code.go 2.5 KB

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