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 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
|
||||
* 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 {
|
||||
fmt.Print("oi, hello day 22")
|
||||
filename := "day22/input"
|
||||
filename := "day22/example"
|
||||
blocks := ReadBlockFile(filename)
|
||||
byZ := BlocksByZ(blocks)
|
||||
|
||||
space := NewSpace(byZ)
|
||||
space.SettleAll()
|
||||
|
||||
result := space.CountChainReactoins()
|
||||
result := space.CountFreeBlocks()
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package day22
|
|||
|
||||
import (
|
||||
// "fmt"
|
||||
"cmp"
|
||||
"log"
|
||||
"slices"
|
||||
// "time"
|
||||
|
@ -42,8 +43,7 @@ func (s *Space) AgainCountFreeBlocks() (result int) {
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
func (s *Space) CountFreeBlocks() (result int) {
|
||||
func (s *Space) InitialCollectGoodToDisintegrate() (result []Block) {
|
||||
allBlocks := make(map[*Block]any)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
log.Println(">>>>> starting hardcode count <<<<<")
|
||||
for rowNum, row := range s.SettledOnZ {
|
||||
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)
|
||||
for rowNum, row := range newUnsettled {
|
||||
newUnsettled[rowNum] = slices.Clone(row)
|
||||
|
@ -83,18 +91,21 @@ func (s *Space) ThirdTimeCountFreeBlocks() (result int) {
|
|||
newSpace := NewSpace(newUnsettled)
|
||||
moved := newSpace.SettleAll()
|
||||
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 {
|
||||
log.Printf("for block %+v new space has %d moved\n\n", block, moved)
|
||||
result += 1
|
||||
// log.Printf("for block %+v new space has %d moved\n\n", block, moved)
|
||||
blocks = append(blocks, *block)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Space) ThirdTimeCountFreeBlocks() (result int) {
|
||||
return len(s.ThirdTimeCollectGoodToDisintegrate())
|
||||
}
|
||||
|
||||
func (s *Space) CountChainReactoins() (result int) {
|
||||
for rowNum, row := range s.SettledOnZ {
|
||||
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)
|
||||
// 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)
|
||||
hasMoved = initialZ != block.Z
|
||||
if hasMoved {
|
||||
log.Printf("block %+v moved from %d to %d", block, initialZ, block.Z)
|
||||
}
|
||||
// if hasMoved {
|
||||
// log.Printf("block %+v moved from %d to %d", block, initialZ, block.Z)
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package day22
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSpaceSettleSingle(t *testing.T) {
|
||||
filename := "example"
|
||||
|
@ -104,3 +107,29 @@ func TestExampleSpacePrint(t *testing.T) {
|
|||
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