-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd19.go
54 lines (47 loc) · 1.02 KB
/
d19.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"strconv"
"strings"
)
var d19memo = make(map[string]int64)
func d19try(towels []string, design string) int64 {
options, been := d19memo[design]
if been {
return options
}
for _, t := range towels {
t = strings.TrimSpace(t)
cut, succ := strings.CutPrefix(design, t)
if !succ {
continue
}
if cut == "" {
options++
}
options += d19try(towels, cut)
}
d19memo[design] = options
return options
}
func (*methods) D19P1(input string) string {
parts := strings.Split(input, "\n\n")
towels := strings.Split(parts[0], ",")
designs := strings.Split(parts[1], "\n")
var possible int64
for _, d := range designs {
if d19try(towels, d) > 0 {
possible++
}
}
return strconv.FormatInt(possible, 10)
}
func (*methods) D19P2(input string) string {
parts := strings.Split(input, "\n\n")
towels := strings.Split(parts[0], ",")
designs := strings.Split(parts[1], "\n")
var possible int64
for _, d := range designs {
possible += d19try(towels, d)
}
return strconv.FormatInt(possible, 10)
}