From 2b3c7f4ca63d5a29bd8d870294494b2453e47478 Mon Sep 17 00:00:00 2001 From: efim Date: Fri, 22 Dec 2023 09:59:41 +0000 Subject: [PATCH] day22, example --- day22/block.go | 15 ++++++++------- day22/sandSlabs.go | 10 +++++++++- day22/space.go | 41 +++++++++++++++++++++++++++++++++++++---- day22/space_test.go | 23 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/day22/block.go b/day22/block.go index 6e5e1c4..dd8b1db 100644 --- a/day22/block.go +++ b/day22/block.go @@ -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 { diff --git a/day22/sandSlabs.go b/day22/sandSlabs.go index 5964071..2476633 100644 --- a/day22/sandSlabs.go +++ b/day22/sandSlabs.go @@ -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 } diff --git a/day22/space.go b/day22/space.go index 2475cc6..e0c3e91 100644 --- a/day22/space.go +++ b/day22/space.go @@ -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 diff --git a/day22/space_test.go b/day22/space_test.go index 748b67e..5379601 100644 --- a/day22/space_test.go +++ b/day22/space_test.go @@ -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 +}