Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 27179eb

Browse files
committed
Add solution day 19 part 2
1 parent f07cbd9 commit 27179eb

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ automatically rebuilt and redeployed every time the `common` module or their own
5151
| 03 | ⭐ ⭐ | 16 | ⭐ 🥸 |
5252
| 04 | ⭐ ⭐ | 17 | ⭐ ⭐ |
5353
| 05 | ⭐ ⭐ | 18 | ⭐ ⭐ |
54-
| 06 | ⭐ ⭐ | 19 | |
54+
| 06 | ⭐ ⭐ | 19 | |
5555
| 07 | ⭐ ⭐ | 20 | |
5656
| 08 | ⭐ ⭐ | 21 | |
5757
| 09 | ⭐ ⭐ | 22 | |

solutions/day19/main.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func main() {
11-
common.Setup(19, part1, nil)
11+
common.Setup(19, part1, part2)
1212
}
1313

1414
func part1(
@@ -31,3 +31,50 @@ func part1(
3131

3232
return fmt.Sprintf("%d of the %d desired designs are possible", count, len(p.desired))
3333
}
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+
}

0 commit comments

Comments
 (0)