day12, example optimized

This commit is contained in:
efim 2023-12-12 15:07:31 +00:00
parent 3a43f90431
commit cdf5a38512
1 changed files with 11 additions and 19 deletions

View File

@ -29,28 +29,16 @@ func Run() int {
blockLengthSum += blockLen
}
variants := generatePermutations("", len(mask), blockLengs, blockLengthSum)
variants := generatePermutations("", len(mask), blockLengs, blockLengthSum, mask)
match := 0
for _, variant := range variants {
if isVariantMatchesMask(variant, mask) {
match += 1
}
for range variants {
match += 1
}
log.Printf("for line %s matches %d\n", mask, match)
log.Printf("for line %s blocks %+v matches %d\n", mask, blockLengs, match)
sum += match
}
// totalVariants := 0
// for _, variang := range test1 {
// totalVariants += 1
// if isVariantMatchesMask(variang, line3) {
// match += 1
// fmt.Print("matches: ")
// }
// fmt.Println(variang)
// }
// fmt.Printf("total variants : %d\n", totalVariants)
return sum
}
@ -75,7 +63,11 @@ func ReadLine(line string) (string, []int) {
return mask, blockLengs
}
func generatePermutations(curString string, targetLength int, blockLengths []int, blockLengthsSum int) []string {
func generatePermutations(curString string, targetLength int, blockLengths []int, blockLengthsSum int, mask string) []string {
if !isVariantMatchesMask(curString, mask) {
return []string{}
}
// log.Printf("> entering with %s\n", curString)
if len(blockLengths) == 0 {
if len(curString) > targetLength {
@ -100,8 +92,8 @@ func generatePermutations(curString string, targetLength int, blockLengths []int
whenPass := curString + "."
whenAdd := curString + strings.Repeat("#", nextBlock) + strings.Repeat(".", rightPointRepeat)
variantsWhenPass := generatePermutations(whenPass, targetLength, blockLengths, blockLengthsSum)
variantsWhenAdd := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum - nextBlock)
variantsWhenPass := generatePermutations(whenPass, targetLength, blockLengths, blockLengthsSum, mask)
variantsWhenAdd := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum - nextBlock, mask)
return append(variantsWhenAdd, variantsWhenPass...)
}