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

@ -14,13 +14,14 @@ type XY struct {
}
type Block struct {
NameNum int
XMin, XMax uint
YMin, YMax uint
Z uint
IsSettled bool
ZHeight uint
Supports []*Block
NameNum int
XMin, XMax uint
YMin, YMax uint
Z uint
IsSettled bool
ZHeight uint
Supports []*Block
SupportedBy []*Block
}
func (b *Block) String() string {

View File

@ -6,5 +6,13 @@ import (
func Run() int {
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
import "log"
type Space struct {
MaxZ uint
SettledOnZ [][]*Block
@ -10,15 +12,44 @@ type Space struct {
func NewSpace(blocksByZ [][]*Block) Space {
return Space{
UnsettledByZ: blocksByZ,
MaxZ: uint(len(blocksByZ)),
MaxZ: uint(len(blocksByZ) - 1),
MaxSettledOnXY: make(map[XY]*Block),
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
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:
@ -35,10 +66,11 @@ func (s *Space)SettleBlock(block *Block) {
underBlock, found := s.MaxSettledOnXY[xy]
// if block.NameNum
if found {
if underBlock.Z > underZMax {
underBlockMaxZ := underBlock.Z + underBlock.ZHeight
if underBlockMaxZ > underZMax {
underZBlocks = []*Block{underBlock}
underZMax = underBlock.Z
} else if underBlock.Z == underZMax {
underZMax = underBlockMaxZ
} else if underBlockMaxZ == underZMax {
underZBlocks = append(underZBlocks, underBlock)
}
}
@ -47,6 +79,7 @@ func (s *Space)SettleBlock(block *Block) {
for _, settledUnderblock := range underZBlocks {
settledUnderblock.Supports = append(settledUnderblock.Supports, block)
block.SupportedBy = append(block.SupportedBy, settledUnderblock)
}
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
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
}