day22, example

This commit is contained in:
efim 2023-12-22 09:59:41 +00:00
parent 3ede691333
commit 2b3c7f4ca6
4 changed files with 77 additions and 12 deletions

View File

@ -21,6 +21,7 @@ type Block struct {
IsSettled bool IsSettled bool
ZHeight uint ZHeight uint
Supports []*Block Supports []*Block
SupportedBy []*Block
} }
func (b *Block) String() string { func (b *Block) String() string {

View File

@ -6,5 +6,13 @@ import (
func Run() int { func Run() int {
fmt.Print("oi, hello day 22") fmt.Print("oi, hello day 22")
return 0 filename := "day22/example"
blocks := ReadBlockFile(filename)
byZ := BlocksByZ(blocks)
space := NewSpace(byZ)
space.SettleAll()
result := space.CountFreeBlocks()
return result
} }

View File

@ -1,5 +1,7 @@
package day22 package day22
import "log"
type Space struct { type Space struct {
MaxZ uint MaxZ uint
SettledOnZ [][]*Block SettledOnZ [][]*Block
@ -10,15 +12,44 @@ type Space struct {
func NewSpace(blocksByZ [][]*Block) Space { func NewSpace(blocksByZ [][]*Block) Space {
return Space{ return Space{
UnsettledByZ: blocksByZ, UnsettledByZ: blocksByZ,
MaxZ: uint(len(blocksByZ)), MaxZ: uint(len(blocksByZ) - 1),
MaxSettledOnXY: make(map[XY]*Block), MaxSettledOnXY: make(map[XY]*Block),
SettledOnZ: make([][]*Block, len(blocksByZ)), SettledOnZ: make([][]*Block, len(blocksByZ)),
} }
} }
func (s *Space)CountFreeBlocks() (result int) {
allBlocks := make(map[*Block]any)
for _, row := range s.SettledOnZ {
for _, block := range row {
allBlocks[block] = struct{}{}
if len(block.SupportedBy) == 1 {
log.Printf("in block %+v. only support is %+v", block, block.SupportedBy)
log.Printf("should be NOT OK to remove %+v", block.SupportedBy)
delete(allBlocks, block.SupportedBy[0])
}
}
}
result = len(allBlocks)
return
}
func (s *Space)SettleAll() {
for i := uint(1); i <= s.MaxZ; i++ {
s.SettleZ(i)
}
}
// settle all blocks in Z, remove Z from UnsettledByZ // settle all blocks in Z, remove Z from UnsettledByZ
func (s *Space)SettleZ(z uint) { func (s *Space)SettleZ(z uint) {
blocksToSettle := s.UnsettledByZ[int(z)]
for _, block := range blocksToSettle {
s.SettleBlock(block)
}
s.UnsettledByZ[int(z)] = nil
} }
// for the block: // for the block:
@ -35,10 +66,11 @@ func (s *Space)SettleBlock(block *Block) {
underBlock, found := s.MaxSettledOnXY[xy] underBlock, found := s.MaxSettledOnXY[xy]
// if block.NameNum // if block.NameNum
if found { if found {
if underBlock.Z > underZMax { underBlockMaxZ := underBlock.Z + underBlock.ZHeight
if underBlockMaxZ > underZMax {
underZBlocks = []*Block{underBlock} underZBlocks = []*Block{underBlock}
underZMax = underBlock.Z underZMax = underBlockMaxZ
} else if underBlock.Z == underZMax { } else if underBlockMaxZ == underZMax {
underZBlocks = append(underZBlocks, underBlock) underZBlocks = append(underZBlocks, underBlock)
} }
} }
@ -47,6 +79,7 @@ func (s *Space)SettleBlock(block *Block) {
for _, settledUnderblock := range underZBlocks { for _, settledUnderblock := range underZBlocks {
settledUnderblock.Supports = append(settledUnderblock.Supports, block) settledUnderblock.Supports = append(settledUnderblock.Supports, block)
block.SupportedBy = append(block.SupportedBy, settledUnderblock)
} }
block.Z = underZMax + 1 block.Z = underZMax + 1

View File

@ -62,3 +62,26 @@ func TestSpaceSettleThirdOnTopFirst(t *testing.T) {
// and that overlaps 1-1, 0-2 AND 0-0, 0-2 // and that overlaps 1-1, 0-2 AND 0-0, 0-2
t.Logf("other blocks should not supt %+v", block3.Supports) t.Logf("other blocks should not supt %+v", block3.Supports)
} }
func TestSpaceExampleSettleAll(t *testing.T) {
filename := "example"
blocks := ReadBlockFile(filename)
byZ := BlocksByZ(blocks)
space := NewSpace(byZ)
space.SettleAll()
t.Logf("settled space %+v", space)
// maybe i can check via console.
i := 2
t.Logf("level %d is : %+v", i, space.SettledOnZ[i])
// it looks ok for the example.
// let's hope?
t.Logf("for example, free blocks amount is %d", space.CountFreeBlocks())
// oh, i need 'supported'?
// how do i need to count the task question
// i guess we can start with set of all blocks, then?
// run over all, if some block is only supported by some underBlock - remove that underblock
}