package one import ( "bytes" "cmp" "slices" "strings" log "github.com/sirupsen/logrus" ) func Solve(buf []byte) (int, error) { r := bytes.NewReader(buf) stripes, designs, err := parseLines(r) if err != nil { return 0, err } log.Debug(stripes, designs) var sum int for _, design := range designs { ok := isPossibleArrangement(design, stripes) if ok { log.Debugf("design: %s is possible", design) sum++ } } return sum, nil } func isPossibleArrangement(design string, stripes []string) bool { if len(design) == 0 { return true } greedy := []string{} for _, stripe := range stripes { log.Debugf("testing design: %s with stripe: %s", design, stripe) if strings.HasPrefix(design, stripe) { log.Debugf("design: %s has prefix %s", design, stripe) greedy = append(greedy, stripe) } } slices.SortFunc(greedy, func(a, b string) int { return cmp.Compare(len(b), len(a)) }) for _, g := range greedy { if isPossibleArrangement(design[len(g):], stripes) { return true } } return false }