day12, part1

This commit is contained in:
efim 2023-12-12 15:33:56 +00:00
parent cdf5a38512
commit c187a03076
3 changed files with 1112 additions and 13 deletions

View File

@ -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...)
}

1000
day12/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,3 +6,89 @@ i take length of the mask, that's max size
then for each step, either put . or put n # from the input.
add to current string, and do 2 recursive calls, one with diminished 'queue', one with same
* wrong answer on input
it's too high
and log shows:
2023/12/12 15:07:52 for line ???#?.?#?#.?#???#..? blocks [4 4 5 1] matches 2
and it should be 0
** huh, nope this looks good:
testMask := "???#?.?#?#.?#???#..?"
testBlocks := []int{4,4,5,1}
testVariants := generatePermutations("", len(testMask), testBlocks, 14, testMask)
fmt.Printf("for mask %s and blocks %+v\n", testMask, testBlocks)
fmt.Println(testVariants)
for mask ???#?.?#?#.?#???#..? and blocks [4 4 5 1]
[####..####..#####..# .####.####..#####..#]
** let's check this : for line ??????#???????? blocks [7 2] matches 21
** or this for line ?????.??#????? blocks [3 3 2 1] matches 3
looks ok
** this for line ??..??#?????#?##? blocks [1 1 1 1 4] matches 15
looks good
** for line ?#??#??#???.??.??.? blocks [1 2 3 1 1 1] matches 20
seems ok
** for line ???????#??.????####? blocks [1 1 1 1 1 6] matches 58
bingo?
for mask ???????#??.????####? and blocks [1 1 1 1 1 6]
#.#.#..#.#.######...
#.#.#..#.#..######..
#.#.#..#.#...######.
#.#.#..#.#....######
#.#.#..#...#.######.
#.#.#..#...#..######
#.#.#..#....#.######
#.#..#.#.#.######...
#.#..#.#.#..######..
#.#..#.#.#...######.
#.#..#.#.#....######
#.#..#.#...#.######.
#.#..#.#...#..######
#.#..#.#....#.######
#.#....#.#.#.######.
#.#....#.#.#..######
#.#....#.#..#.######
#..#.#.#.#.######...
#..#.#.#.#..######..
#..#.#.#.#...######.
#..#.#.#.#....######
#..#.#.#...#.######.
#..#.#.#...#..######
#..#.#.#....#.######
#..#...#.#.#.######.
#..#...#.#.#..######
#..#...#.#..#.######
#...#..#.#.#.######.
#...#..#.#.#..######
#...#..#.#..#.######
#....#.#.#.#.######.
#....#.#.#.#..######
#....#.#.#..#.######
.#.#.#.#.#.######...
.#.#.#.#.#..######..
.#.#.#.#.#...######.
.#.#.#.#.#....######
.#.#.#.#...#.######.
.#.#.#.#...#..######
.#.#.#.#....#.######
.#.#...#.#.#.######.
.#.#...#.#.#..######
.#.#...#.#..#.######
.#..#..#.#.#.######.
.#..#..#.#.#..######
.#..#..#.#..#.######
.#...#.#.#.#.######.
.#...#.#.#.#..######
.#...#.#.#..#.######
..#.#..#.#.#.######.
..#.#..#.#.#..######
..#.#..#.#..#.######
..#..#.#.#.#.######.
..#..#.#.#.#..######
..#..#.#.#..#.######
...#.#.#.#.#.######.
...#.#.#.#.#..######
...#.#.#.#..#.######