day12, example optimized
This commit is contained in:
parent
3a43f90431
commit
cdf5a38512
|
@ -29,28 +29,16 @@ func Run() int {
|
||||||
blockLengthSum += blockLen
|
blockLengthSum += blockLen
|
||||||
}
|
}
|
||||||
|
|
||||||
variants := generatePermutations("", len(mask), blockLengs, blockLengthSum)
|
variants := generatePermutations("", len(mask), blockLengs, blockLengthSum, mask)
|
||||||
|
|
||||||
match := 0
|
match := 0
|
||||||
for _, variant := range variants {
|
for range variants {
|
||||||
if isVariantMatchesMask(variant, mask) {
|
|
||||||
match += 1
|
match += 1
|
||||||
}
|
}
|
||||||
}
|
log.Printf("for line %s blocks %+v matches %d\n", mask, blockLengs, match)
|
||||||
log.Printf("for line %s matches %d\n", mask, match)
|
|
||||||
sum += 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
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +63,11 @@ func ReadLine(line string) (string, []int) {
|
||||||
return mask, blockLengs
|
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)
|
// log.Printf("> entering with %s\n", curString)
|
||||||
if len(blockLengths) == 0 {
|
if len(blockLengths) == 0 {
|
||||||
if len(curString) > targetLength {
|
if len(curString) > targetLength {
|
||||||
|
@ -100,8 +92,8 @@ func generatePermutations(curString string, targetLength int, blockLengths []int
|
||||||
whenPass := curString + "."
|
whenPass := curString + "."
|
||||||
whenAdd := curString + strings.Repeat("#", nextBlock) + strings.Repeat(".", rightPointRepeat)
|
whenAdd := curString + strings.Repeat("#", nextBlock) + strings.Repeat(".", rightPointRepeat)
|
||||||
|
|
||||||
variantsWhenPass := generatePermutations(whenPass, targetLength, blockLengths, blockLengthsSum)
|
variantsWhenPass := generatePermutations(whenPass, targetLength, blockLengths, blockLengthsSum, mask)
|
||||||
variantsWhenAdd := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum - nextBlock)
|
variantsWhenAdd := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum - nextBlock, mask)
|
||||||
|
|
||||||
return append(variantsWhenAdd, variantsWhenPass...)
|
return append(variantsWhenAdd, variantsWhenPass...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue