day22, part2

This commit is contained in:
efim 2023-12-22 12:21:49 +00:00
parent 45d03e5ab3
commit 8be2fa3844
2 changed files with 26 additions and 1 deletions

View File

@ -13,6 +13,6 @@ func Run() int {
space := NewSpace(byZ)
space.SettleAll()
result := space.ThirdTimeCountFreeBlocks()
result := space.CountChainReactoins()
return result
}

View File

@ -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)