From 6398a4d468c5c5f73f3044b75a206ad20c4cd95a Mon Sep 17 00:00:00 2001 From: efim Date: Wed, 13 Dec 2023 02:35:41 +0000 Subject: [PATCH] day12, counts but too slow --- day12/dayTwelve.go | 74 +++++++++++++++++++++++++++++++--------------- day12/notes.org | 10 +++++++ 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/day12/dayTwelve.go b/day12/dayTwelve.go index af61b94..c6dc31e 100644 --- a/day12/dayTwelve.go +++ b/day12/dayTwelve.go @@ -6,12 +6,16 @@ import ( "os" "strconv" "strings" + "sync" + "time" ) func Run() int { fmt.Println("hello day 12.") - filename := "day12/example1" + start := time.Now() + + filename := "day12/input" bytes, err := os.ReadFile(filename) if err != nil { panic(fmt.Sprintf("error reading file %s\n", filename)) @@ -32,24 +36,46 @@ func Run() int { // fmt.Println(variant) // } - for _, line := range strings.Split(text, "\n") { - mask, blockLengs := ReadLine(line) + var wg sync.WaitGroup + lines := strings.Split(text, "\n") + wg.Add(len(lines)) - blockLengthSum := 0 - for _, blockLen := range blockLengs { - blockLengthSum += blockLen - } + matches := make(chan int) + go func() { + wg.Wait() + close(matches) + }() - variants := generatePermutations("", len(mask), blockLengs, blockLengthSum, mask) + for i, line := range lines { + go func(line string, lineNum int){ + mask, blockLengs := ReadLine(line) - match := 0 - for range variants { - match += 1 - } - fmt.Printf("for line %s blocks %+v matches %d\n", mask, blockLengs, match) - result += match + blockLengthSum := 0 + for _, blockLen := range blockLengs { + blockLengthSum += blockLen + } + + variantsCount := generatePermutations("", len(mask), blockLengs, blockLengthSum, mask) + + log.Printf("%d : for line %s blocks %+v matches %d\n", lineNum, mask, blockLengs, variantsCount) + matches <- variantsCount + wg.Done() + }(line, i) } + num := 0 + for match := range matches { + num += 1 + result += match + log.Printf("%d. intermediate: %d\n", num, result) + fmt.Printf("%d\n", result) + } + + end := time.Now() + + diff := end.Sub(start) + log.Printf("> calculated for %s", diff.String()) + return result } @@ -89,30 +115,30 @@ func ReadLine(line string) (string, []int) { return mask, blockLengs } -func generatePermutations(curString string, targetLength int, blockLengths []int, blockLengthsSum int, mask string) []string { +func generatePermutations(curString string, targetLength int, blockLengths []int, blockLengthsSum int, mask string) int { // fmt.Printf("> entering with \n%s\nfor map \n%s\n\n", curString, mask) // time.Sleep(time.Second) if !isVariantMatchesMask(curString, mask) { - return []string{} + return 0 } // log.Printf("> entering with %s\n", curString) if len(blockLengths) == 0 { if len(curString) > targetLength { - return []string{} + return 0 } variant := curString + strings.Repeat(".", targetLength-len(curString)) if !isVariantMatchesMask(variant, mask) { - return []string{} + return 0 } - return []string{variant} + return 1 } nextBlock := blockLengths[0] restBlocks := blockLengths[1:] - if len(curString)+blockLengthsSum > targetLength { - return []string{} + if len(curString) + blockLengthsSum + len(blockLengths) - 1 > targetLength { + return 0 } isLast := len(restBlocks) == 0 @@ -124,10 +150,10 @@ func generatePermutations(curString string, targetLength int, blockLengths []int 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) + passCount := generatePermutations(whenPass, targetLength, blockLengths, blockLengthsSum, mask) + addCount := generatePermutations(whenAdd, targetLength, restBlocks, blockLengthsSum-nextBlock, mask) - return append(variantsWhenAdd, variantsWhenPass...) + return passCount + addCount } func isVariantMatchesMask(variant, mask string) bool { diff --git a/day12/notes.org b/day12/notes.org index 4c2845c..ea8a877 100644 --- a/day12/notes.org +++ b/day12/notes.org @@ -92,3 +92,13 @@ for mask ???????#??.????####? and blocks [1 1 1 1 1 6] ...#.#.#.#.#.######. ...#.#.#.#.#..###### ...#.#.#.#..#.###### +* well, maybe overnight will calculate. +but i guess i needed to check whether blocks are 'always' taking full width +then i'll only need to calculate once, and then multiply +** for example +2023/12/12 20:40:41 699 : for line ??#?????#???.? ???#?????#???.????#?????#???.????#?????#???.????#?????#???.? blocks [3 1 2 1 3 1 2 1 3 1 2 1 3 1 2 1 3 1 2 1] matches 38294856 + +??#?? ???#?? ?.? +3,1,2,1 - 10+3 = 13 + +lowest s ###.#.##.#..... - plenty of space for additional