mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
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
|
|
}
|