This repository was archived by the owner on Dec 28, 2024. It is now read-only.
File tree 2 files changed +49
-2
lines changed
2 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ automatically rebuilt and redeployed every time the `common` module or their own
51
51
| 03 | ⭐ ⭐ | 16 | ⭐ 🥸 |
52
52
| 04 | ⭐ ⭐ | 17 | ⭐ ⭐ |
53
53
| 05 | ⭐ ⭐ | 18 | ⭐ ⭐ |
54
- | 06 | ⭐ ⭐ | 19 | ⭐ |
54
+ | 06 | ⭐ ⭐ | 19 | ⭐ ⭐ |
55
55
| 07 | ⭐ ⭐ | 20 | |
56
56
| 08 | ⭐ ⭐ | 21 | |
57
57
| 09 | ⭐ ⭐ | 22 | |
Original file line number Diff line number Diff line change 8
8
)
9
9
10
10
func main () {
11
- common .Setup (19 , part1 , nil )
11
+ common .Setup (19 , part1 , part2 )
12
12
}
13
13
14
14
func part1 (
@@ -31,3 +31,50 @@ func part1(
31
31
32
32
return fmt .Sprintf ("%d of the %d desired designs are possible" , count , len (p .desired ))
33
33
}
34
+
35
+ func part2 (
36
+ input string ,
37
+ ) string {
38
+ p , err := parse (input )
39
+ if err != nil {
40
+ return fmt .Sprintf ("Failed to parse input: %v" , err )
41
+ }
42
+
43
+ cache := make (map [string ]int )
44
+ count := 0
45
+ for _ , desired := range p .desired {
46
+ n := loop (p .available , desired , cache )
47
+ count += n
48
+ }
49
+
50
+ return fmt .Sprintf ("Number of available combinations: %d" , count )
51
+ }
52
+
53
+ func loop (
54
+ available []string ,
55
+ target string ,
56
+ cache map [string ]int ,
57
+ ) int {
58
+ cacheValue , isCached := cache [target ]
59
+ if isCached {
60
+ return cacheValue
61
+ }
62
+
63
+ l := len (target )
64
+ sum := 0
65
+ for _ , pattern := range available {
66
+ pl := len (pattern )
67
+ if pl > l {
68
+ continue
69
+ }
70
+
71
+ if pattern == target {
72
+ sum += 1
73
+ } else if target [:pl ] == pattern {
74
+ sum += loop (available , target [pl :], cache )
75
+ }
76
+ }
77
+
78
+ cache [target ] = sum
79
+ return sum
80
+ }
You can’t perform that action at this time.
0 commit comments