File tree 1 file changed +10
-3
lines changed
1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -11,10 +11,11 @@ import (
11
11
12
12
func part1 (input string ) int64 {
13
13
towels , patterns := parseInput (input )
14
+ memo := make (map [string ]bool )
14
15
count := int64 (0 )
15
16
16
17
for _ , pattern := range patterns {
17
- if isValidPattern (towels , pattern ) {
18
+ if isValidPattern (towels , pattern , memo ) {
18
19
count ++
19
20
}
20
21
}
@@ -34,18 +35,24 @@ func part2(input string) int64 {
34
35
return count
35
36
}
36
37
37
- func isValidPattern (towels []string , pattern string ) bool {
38
+ func isValidPattern (towels []string , pattern string , memo map [ string ] bool ) bool {
38
39
if pattern == "" {
39
40
return true
40
41
}
41
42
43
+ if isValid , ok := memo [pattern ]; ok {
44
+ return isValid
45
+ }
46
+
42
47
for _ , towel := range towels {
43
48
if len (towel ) > len (pattern ) {
44
49
continue
45
50
}
46
51
47
52
if strings .HasPrefix (pattern , towel ) {
48
- if isValidPattern (towels , pattern [len (towel ):]) {
53
+ isValid := isValidPattern (towels , pattern [len (towel ):], memo )
54
+ memo [pattern ] = isValid
55
+ if isValid {
49
56
return true
50
57
}
51
58
}
You can’t perform that action at this time.
0 commit comments