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

Commit

Permalink
Add solution day 19 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalnode committed Dec 19, 2024
1 parent f07cbd9 commit 27179eb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ automatically rebuilt and redeployed every time the `common` module or their own
| 03 | ⭐ ⭐ | 16 | ⭐ 🥸 |
| 04 | ⭐ ⭐ | 17 | ⭐ ⭐ |
| 05 | ⭐ ⭐ | 18 | ⭐ ⭐ |
| 06 | ⭐ ⭐ | 19 | |
| 06 | ⭐ ⭐ | 19 | |
| 07 | ⭐ ⭐ | 20 | |
| 08 | ⭐ ⭐ | 21 | |
| 09 | ⭐ ⭐ | 22 | |
Expand Down
49 changes: 48 additions & 1 deletion solutions/day19/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
common.Setup(19, part1, nil)
common.Setup(19, part1, part2)
}

func part1(
Expand All @@ -31,3 +31,50 @@ func part1(

return fmt.Sprintf("%d of the %d desired designs are possible", count, len(p.desired))
}

func part2(
input string,
) string {
p, err := parse(input)
if err != nil {
return fmt.Sprintf("Failed to parse input: %v", err)
}

cache := make(map[string]int)
count := 0
for _, desired := range p.desired {
n := loop(p.available, desired, cache)
count += n
}

return fmt.Sprintf("Number of available combinations: %d", count)
}

func loop(
available []string,
target string,
cache map[string]int,
) int {
cacheValue, isCached := cache[target]
if isCached {
return cacheValue
}

l := len(target)
sum := 0
for _, pattern := range available {
pl := len(pattern)
if pl > l {
continue
}

if pattern == target {
sum += 1
} else if target[:pl] == pattern {
sum += loop(available, target[pl:], cache)
}
}

cache[target] = sum
return sum
}

0 comments on commit 27179eb

Please sign in to comment.