day22: bug: not using Set - duplicate supports
This commit is contained in:
parent
8be2fa3844
commit
29528f23ac
|
@ -49,3 +49,26 @@ create new space and try to settle it
|
||||||
2023/12/22 12:12:24 block [Block 5 - x:0-2, y:1-1, z:4, h:0, isSettled true] moved from 3 to 4
|
2023/12/22 12:12:24 block [Block 5 - x:0-2, y:1-1, z:4, h:0, isSettled true] moved from 3 to 4
|
||||||
2023/12/22 12:12:24 block [Block 6 - x:1-1, y:1-1, z:5, h:1, isSettled true] moved from 4 to 5
|
2023/12/22 12:12:24 block [Block 6 - x:1-1, y:1-1, z:5, h:1, isSettled true] moved from 4 to 5
|
||||||
2023/12/22 12:12:24 for block [Block 1 - x:0-2, y:0-0, z:1, h:0, isSettled true] new space has 5 moved
|
2023/12/22 12:12:24 for block [Block 1 - x:0-2, y:0-0, z:1, h:0, isSettled true] new space has 5 moved
|
||||||
|
* ok. brute force with copying slices worked.
|
||||||
|
now i want to debug.
|
||||||
|
|
||||||
|
for each brick, when there is 0 falling, i want to check what are it's surroundings
|
||||||
|
** my initial was : 567
|
||||||
|
** checking example of badly determined:
|
||||||
|
>> for block [Block 291 - x:6-8, y:7-7, z:75, h:0, isSettled false]
|
||||||
|
checking under coord {X:6 Y:7}. found under [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]. ( 'overriding' ) with 35 ; maxZ 35
|
||||||
|
directly supporting blocks are [[Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]]
|
||||||
|
checking under coord {X:7 Y:7}. found under [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]. ( 'adding' ) with 35 ; maxZ 35
|
||||||
|
directly supporting blocks are [[Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true] [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]]
|
||||||
|
checking under coord {X:8 Y:7}. found under [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]. ( 'adding' ) with 35 ; maxZ 35
|
||||||
|
directly supporting blocks are [[Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true] [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true] [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]]
|
||||||
|
>> after settring block [Block 291 - x:6-8, y:7-7, z:36, h:0, isSettled true]. supported by [[Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true] [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true] [Block 698 - x:6-8, y:7-7, z:35, h:0, isSettled true]]
|
||||||
|
** ouch. duplicates in slices. because there's no easy set thingy
|
||||||
|
not doing this was my bug.
|
||||||
|
|
||||||
|
#+begin_src go
|
||||||
|
slices.SortFunc(block.SupportedBy, func(a *Block, b *Block) int {
|
||||||
|
return cmp.Compare(a.NameNum, b.NameNum)
|
||||||
|
})
|
||||||
|
block.SupportedBy = slices.Compact(block.SupportedBy)
|
||||||
|
#+end_src
|
||||||
|
|
|
@ -6,13 +6,13 @@ import (
|
||||||
|
|
||||||
func Run() int {
|
func Run() int {
|
||||||
fmt.Print("oi, hello day 22")
|
fmt.Print("oi, hello day 22")
|
||||||
filename := "day22/input"
|
filename := "day22/example"
|
||||||
blocks := ReadBlockFile(filename)
|
blocks := ReadBlockFile(filename)
|
||||||
byZ := BlocksByZ(blocks)
|
byZ := BlocksByZ(blocks)
|
||||||
|
|
||||||
space := NewSpace(byZ)
|
space := NewSpace(byZ)
|
||||||
space.SettleAll()
|
space.SettleAll()
|
||||||
|
|
||||||
result := space.CountChainReactoins()
|
result := space.CountFreeBlocks()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package day22
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "fmt"
|
// "fmt"
|
||||||
|
"cmp"
|
||||||
"log"
|
"log"
|
||||||
"slices"
|
"slices"
|
||||||
// "time"
|
// "time"
|
||||||
|
@ -42,8 +43,7 @@ func (s *Space) AgainCountFreeBlocks() (result int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Space) InitialCollectGoodToDisintegrate() (result []Block) {
|
||||||
func (s *Space) CountFreeBlocks() (result int) {
|
|
||||||
allBlocks := make(map[*Block]any)
|
allBlocks := make(map[*Block]any)
|
||||||
|
|
||||||
for _, row := range s.SettledOnZ {
|
for _, row := range s.SettledOnZ {
|
||||||
|
@ -57,16 +57,24 @@ func (s *Space) CountFreeBlocks() (result int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = len(allBlocks)
|
|
||||||
|
for block := range allBlocks {
|
||||||
|
result = append(result, *block)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Space) ThirdTimeCountFreeBlocks() (result int) {
|
func (s *Space) CountFreeBlocks() (result int) {
|
||||||
|
return len(s.InitialCollectGoodToDisintegrate())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Space) ThirdTimeCollectGoodToDisintegrate() (blocks []Block) {
|
||||||
// for each block create a new space without it. try to settle and check if 0 moved
|
// for each block create a new space without it. try to settle and check if 0 moved
|
||||||
log.Println(">>>>> starting hardcode count <<<<<")
|
log.Println(">>>>> starting hardcode count <<<<<")
|
||||||
for rowNum, row := range s.SettledOnZ {
|
for rowNum, row := range s.SettledOnZ {
|
||||||
for blockNum, block := range row {
|
for blockNum, block := range row {
|
||||||
log.Printf(">>> starting for block %+v (supports %+v)\n", block, block.Supports)
|
// log.Printf(">>> starting for block %+v (supports %+v)\n", block, block.Supports)
|
||||||
newUnsettled := slices.Clone(s.SettledOnZ)
|
newUnsettled := slices.Clone(s.SettledOnZ)
|
||||||
for rowNum, row := range newUnsettled {
|
for rowNum, row := range newUnsettled {
|
||||||
newUnsettled[rowNum] = slices.Clone(row)
|
newUnsettled[rowNum] = slices.Clone(row)
|
||||||
|
@ -83,18 +91,21 @@ func (s *Space) ThirdTimeCountFreeBlocks() (result int) {
|
||||||
newSpace := NewSpace(newUnsettled)
|
newSpace := NewSpace(newUnsettled)
|
||||||
moved := newSpace.SettleAll()
|
moved := newSpace.SettleAll()
|
||||||
if moved > 0 {
|
if moved > 0 {
|
||||||
log.Printf("for block %+v new space has %d moved\n\n", block, moved)
|
// log.Printf("for block %+v new space has %d moved\n\n", block, moved)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("for block %+v new space has %d moved\n\n", block, moved)
|
// log.Printf("for block %+v new space has %d moved\n\n", block, moved)
|
||||||
result += 1
|
blocks = append(blocks, *block)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Space) ThirdTimeCountFreeBlocks() (result int) {
|
||||||
|
return len(s.ThirdTimeCollectGoodToDisintegrate())
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Space) CountChainReactoins() (result int) {
|
func (s *Space) CountChainReactoins() (result int) {
|
||||||
for rowNum, row := range s.SettledOnZ {
|
for rowNum, row := range s.SettledOnZ {
|
||||||
for blockNum, _ := range row {
|
for blockNum, _ := range row {
|
||||||
|
@ -188,10 +199,15 @@ func (s *Space) SettleBlock(block *Block) (hasMoved bool) {
|
||||||
s.SettledOnZ[block.Z] = append(s.SettledOnZ[block.Z], block)
|
s.SettledOnZ[block.Z] = append(s.SettledOnZ[block.Z], block)
|
||||||
// fmt.Printf(">> after settring block %s. supported by %+v\n\n", block, block.SupportedBy)
|
// fmt.Printf(">> after settring block %s. supported by %+v\n\n", block, block.SupportedBy)
|
||||||
|
|
||||||
|
slices.SortFunc(block.SupportedBy, func(a *Block, b *Block) int {
|
||||||
|
return cmp.Compare(a.NameNum, b.NameNum)
|
||||||
|
})
|
||||||
|
block.SupportedBy = slices.Compact(block.SupportedBy)
|
||||||
|
|
||||||
// time.Sleep(500 * time.Millisecond)
|
// time.Sleep(500 * time.Millisecond)
|
||||||
hasMoved = initialZ != block.Z
|
hasMoved = initialZ != block.Z
|
||||||
if hasMoved {
|
// if hasMoved {
|
||||||
log.Printf("block %+v moved from %d to %d", block, initialZ, block.Z)
|
// log.Printf("block %+v moved from %d to %d", block, initialZ, block.Z)
|
||||||
}
|
// }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package day22
|
package day22
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestSpaceSettleSingle(t *testing.T) {
|
func TestSpaceSettleSingle(t *testing.T) {
|
||||||
filename := "example"
|
filename := "example"
|
||||||
|
@ -104,3 +107,29 @@ func TestExampleSpacePrint(t *testing.T) {
|
||||||
PrintSpace(space, "after-settping.png")
|
PrintSpace(space, "after-settping.png")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompareInitialAndBruteforce(t *testing.T) {
|
||||||
|
filename := "input"
|
||||||
|
blocks := ReadBlockFile(filename)
|
||||||
|
byZ := BlocksByZ(blocks)
|
||||||
|
|
||||||
|
space := NewSpace(byZ)
|
||||||
|
|
||||||
|
space.SettleAll()
|
||||||
|
|
||||||
|
initialBlocks := space.InitialCollectGoodToDisintegrate()
|
||||||
|
correct := space.ThirdTimeCollectGoodToDisintegrate()
|
||||||
|
|
||||||
|
t.Log("len of initial solution : ", len(initialBlocks))
|
||||||
|
t.Log("len of correct solution : ", len(correct))
|
||||||
|
|
||||||
|
for _, disintegratableInInitial := range initialBlocks {
|
||||||
|
indexInCorrect := slices.IndexFunc(correct, func(e Block) bool {
|
||||||
|
return e.NameNum == disintegratableInInitial.NameNum
|
||||||
|
})
|
||||||
|
if indexInCorrect == -1 {
|
||||||
|
t.Logf("> found %+v. falsly marked as disintegratable\n\n", disintegratableInInitial)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue