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)