day12, example part 2

This commit is contained in:
efim 2023-12-12 15:47:48 +00:00
parent c187a03076
commit 42b587918e
1 changed files with 23 additions and 4 deletions

View File

@ -11,7 +11,7 @@ import (
func Run() int {
fmt.Println("hello day 12.")
filename := "day12/input"
filename := "day12/example1"
bytes, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprintf("error reading file %s\n", filename))
@ -21,8 +21,7 @@ func Run() int {
text := string(bytes)
text = strings.TrimSpace(text)
// testMask := "???????#??.????####?"
// testBlocks := []int{1, 1, 1, 1, 1, 6}
// testMask, testBlocks := ReadLine(".??..??...?##. 1,1,3")
// blocksSum := 0
// for _, block := range testBlocks {
// blocksSum += block
@ -54,6 +53,17 @@ func Run() int {
return result
}
func myRepeat(line, sep string, amount int) string {
acc := ""
for i := 0; i < amount; i++ {
acc += sep
acc += line
}
acc, _ = strings.CutPrefix(acc, sep)
return acc
}
// ???.### 1,1,3
func ReadLine(line string) (string, []int) {
firstSplit := strings.Split(line, " ")
@ -61,7 +71,11 @@ func ReadLine(line string) (string, []int) {
panic(fmt.Sprintf("error splitting %s into 2", line))
}
mask := firstSplit[0]
blockLengthStrings := strings.Split(firstSplit[1], ",")
mask = myRepeat(mask, "?", 5)
blocks := firstSplit[1]
blocks = myRepeat(blocks, ",", 5)
// log.Printf(">> repeating blocks %s", blocks)
blockLengthStrings := strings.Split(blocks, ",")
blockLengs := make([]int, len(blockLengthStrings))
for i, blockLenStr := range blockLengthStrings {
@ -76,6 +90,11 @@ func ReadLine(line string) (string, []int) {
}
func generatePermutations(curString string, targetLength int, blockLengths []int, blockLengthsSum int, mask string) []string {
// fmt.Printf("> entering with \n%s\nfor map \n%s\n\n", curString, mask)
// time.Sleep(time.Second)
if !isVariantMatchesMask(curString, mask) {
return []string{}
}
// log.Printf("> entering with %s\n", curString)
if len(blockLengths) == 0 {