From 8be2fa3844a064342aa021bd43c2c2e465813962 Mon Sep 17 00:00:00 2001 From: efim Date: Fri, 22 Dec 2023 12:21:49 +0000 Subject: [PATCH] day22, part2 --- day22/sandSlabs.go | 2 +- day22/space.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/day22/sandSlabs.go b/day22/sandSlabs.go index 7d4f608..0792063 100644 --- a/day22/sandSlabs.go +++ b/day22/sandSlabs.go @@ -13,6 +13,6 @@ func Run() int { space := NewSpace(byZ) space.SettleAll() - result := space.ThirdTimeCountFreeBlocks() + result := space.CountChainReactoins() return result } diff --git a/day22/space.go b/day22/space.go index 0351ebc..0b81635 100644 --- a/day22/space.go +++ b/day22/space.go @@ -95,6 +95,31 @@ func (s *Space) ThirdTimeCountFreeBlocks() (result int) { return } +func (s *Space) CountChainReactoins() (result int) { + for rowNum, row := range s.SettledOnZ { + for blockNum, _ := range row { + newUnsettled := slices.Clone(s.SettledOnZ) + for rowNum, row := range newUnsettled { + newUnsettled[rowNum] = slices.Clone(row) + } + newUnsettled[rowNum] = slices.Delete(newUnsettled[rowNum], blockNum, blockNum+1) + // and now copy the blocks + for rowNum, row := range newUnsettled { + for blockNum, block := range row { + newBlock := *block + newUnsettled[rowNum][blockNum] = &newBlock + } + } + + newSpace := NewSpace(newUnsettled) + moved := newSpace.SettleAll() + result += moved + } + } + + return +} + func (s *Space) SettleAll() (totalMoved int) { for i := uint(1); i <= s.MaxZ; i++ { movedAfterLayer := s.SettleZ(i)