day12, part1
This commit is contained in:
@@ -11,16 +11,28 @@ import (
|
||||
func Run() int {
|
||||
fmt.Println("hello day 12.")
|
||||
|
||||
filename := "day12/example1"
|
||||
filename := "day12/input"
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error reading file %s\n", filename))
|
||||
}
|
||||
|
||||
sum := 0
|
||||
result := 0
|
||||
text := string(bytes)
|
||||
text = strings.TrimSpace(text)
|
||||
|
||||
// testMask := "???????#??.????####?"
|
||||
// testBlocks := []int{1, 1, 1, 1, 1, 6}
|
||||
// blocksSum := 0
|
||||
// for _, block := range testBlocks {
|
||||
// blocksSum += block
|
||||
// }
|
||||
// testVariants := generatePermutations("", len(testMask), testBlocks, blocksSum, testMask)
|
||||
// fmt.Printf("for mask %s and blocks %+v\n", testMask, testBlocks)
|
||||
// for _, variant := range testVariants {
|
||||
// fmt.Println(variant)
|
||||
// }
|
||||
|
||||
for _, line := range strings.Split(text, "\n") {
|
||||
mask, blockLengs := ReadLine(line)
|
||||
|
||||
@@ -35,11 +47,11 @@ func Run() int {
|
||||
for range variants {
|
||||
match += 1
|
||||
}
|
||||
log.Printf("for line %s blocks %+v matches %d\n", mask, blockLengs, match)
|
||||
sum += match
|
||||
fmt.Printf("for line %s blocks %+v matches %d\n", mask, blockLengs, match)
|
||||
result += match
|
||||
}
|
||||
|
||||
return sum
|
||||
return result
|
||||
}
|
||||
|
||||
// ???.### 1,1,3
|
||||
@@ -64,36 +76,37 @@ func ReadLine(line string) (string, []int) {
|
||||
}
|
||||
|
||||
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 {
|
||||
return []string{}
|
||||
}
|
||||
variant := curString + strings.Repeat(".", targetLength - len(curString))
|
||||
variant := curString + strings.Repeat(".", targetLength-len(curString))
|
||||
if !isVariantMatchesMask(variant, mask) {
|
||||
return []string{}
|
||||
}
|
||||
return []string{variant}
|
||||
}
|
||||
|
||||
nextBlock := blockLengths[0]
|
||||
restBlocks := blockLengths[1:]
|
||||
|
||||
if len(curString) + blockLengthsSum > targetLength {
|
||||
if len(curString)+blockLengthsSum > targetLength {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
isLast := len(restBlocks) == 0
|
||||
isLast := len(restBlocks) == 0
|
||||
rightPointRepeat := 1
|
||||
if isLast {
|
||||
rightPointRepeat = 0 }
|
||||
rightPointRepeat = 0
|
||||
}
|
||||
|
||||
whenPass := curString + "."
|
||||
whenAdd := curString + strings.Repeat("#", nextBlock) + strings.Repeat(".", rightPointRepeat)
|
||||
|
||||
variantsWhenPass := generatePermutations(whenPass, targetLength, blockLengths, blockLengthsSum, mask)
|
||||
variantsWhenAdd := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum - nextBlock, mask)
|
||||
variantsWhenAdd := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum-nextBlock, mask)
|
||||
|
||||
return append(variantsWhenAdd, variantsWhenPass...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user