day22, rewrite with Set
This commit is contained in:
parent
29528f23ac
commit
c3acf211c3
|
@ -7,6 +7,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"github.com/deckarep/golang-set/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type XY struct {
|
type XY struct {
|
||||||
|
@ -20,8 +21,8 @@ type Block struct {
|
||||||
Z uint
|
Z uint
|
||||||
IsSettled bool
|
IsSettled bool
|
||||||
ZHeight uint
|
ZHeight uint
|
||||||
Supports []*Block
|
Supports mapset.Set[*Block]
|
||||||
SupportedBy []*Block
|
SupportedBy mapset.Set[*Block]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) String() string {
|
func (b *Block) String() string {
|
||||||
|
@ -53,6 +54,9 @@ func ReadBlock(line string, num int) (b Block) {
|
||||||
b.Z = uint(min(z1, z2))
|
b.Z = uint(min(z1, z2))
|
||||||
b.ZHeight = uint(max(z1, z2)) - b.Z
|
b.ZHeight = uint(max(z1, z2)) - b.Z
|
||||||
|
|
||||||
|
b.Supports = mapset.NewSet[*Block]()
|
||||||
|
b.SupportedBy = mapset.NewSet[*Block]()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,3 +72,5 @@ slices.SortFunc(block.SupportedBy, func(a *Block, b *Block) int {
|
||||||
})
|
})
|
||||||
block.SupportedBy = slices.Compact(block.SupportedBy)
|
block.SupportedBy = slices.Compact(block.SupportedBy)
|
||||||
#+end_src
|
#+end_src
|
||||||
|
* maybe rewrite with Set?
|
||||||
|
* should have done that from the start
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
func Run() int {
|
func Run() int {
|
||||||
fmt.Print("oi, hello day 22")
|
fmt.Print("oi, hello day 22")
|
||||||
filename := "day22/example"
|
filename := "day22/input"
|
||||||
blocks := ReadBlockFile(filename)
|
blocks := ReadBlockFile(filename)
|
||||||
byZ := BlocksByZ(blocks)
|
byZ := BlocksByZ(blocks)
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
package day22
|
package day22
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "fmt"
|
|
||||||
"cmp"
|
|
||||||
"log"
|
"log"
|
||||||
"slices"
|
"slices"
|
||||||
// "time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Space struct {
|
type Space struct {
|
||||||
|
@ -29,8 +26,8 @@ func (s *Space) AgainCountFreeBlocks() (result int) {
|
||||||
for _, block := range row {
|
for _, block := range row {
|
||||||
thisSupports := block.Supports
|
thisSupports := block.Supports
|
||||||
canDisintegrate := true
|
canDisintegrate := true
|
||||||
for _, blockThisSupports := range thisSupports {
|
for blockThisSupports := range thisSupports.Iter() {
|
||||||
if len(blockThisSupports.SupportedBy) == 1 {
|
if blockThisSupports.SupportedBy.Cardinality() == 1 {
|
||||||
// we cannot disintigrate this block
|
// we cannot disintigrate this block
|
||||||
canDisintegrate = false
|
canDisintegrate = false
|
||||||
}
|
}
|
||||||
|
@ -49,8 +46,8 @@ func (s *Space) InitialCollectGoodToDisintegrate() (result []Block) {
|
||||||
for _, row := range s.SettledOnZ {
|
for _, row := range s.SettledOnZ {
|
||||||
for _, block := range row {
|
for _, block := range row {
|
||||||
allBlocks[block] = struct{}{}
|
allBlocks[block] = struct{}{}
|
||||||
if len(block.SupportedBy) == 1 {
|
if block.SupportedBy.Cardinality() == 1 {
|
||||||
onlySupport := block.SupportedBy[0]
|
onlySupport, _ := block.SupportedBy.Pop()
|
||||||
log.Printf("in block %+v. only support is %+v", block, onlySupport)
|
log.Printf("in block %+v. only support is %+v", block, onlySupport)
|
||||||
log.Printf("should be NOT OK to remove %+v", onlySupport)
|
log.Printf("should be NOT OK to remove %+v", onlySupport)
|
||||||
delete(allBlocks, onlySupport)
|
delete(allBlocks, onlySupport)
|
||||||
|
@ -189,8 +186,9 @@ func (s *Space) SettleBlock(block *Block) (hasMoved bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, settledUnderblock := range underZBlocks {
|
for _, settledUnderblock := range underZBlocks {
|
||||||
settledUnderblock.Supports = append(settledUnderblock.Supports, block)
|
settledUnderblock.Supports.Add(block)
|
||||||
block.SupportedBy = append(block.SupportedBy, settledUnderblock)
|
block.SupportedBy.Add(settledUnderblock)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
block.Z = underZMax + 1
|
block.Z = underZMax + 1
|
||||||
|
@ -199,11 +197,6 @@ func (s *Space) SettleBlock(block *Block) (hasMoved bool) {
|
||||||
s.SettledOnZ[block.Z] = append(s.SettledOnZ[block.Z], block)
|
s.SettledOnZ[block.Z] = append(s.SettledOnZ[block.Z], block)
|
||||||
// fmt.Printf(">> after settring block %s. supported by %+v\n\n", block, block.SupportedBy)
|
// fmt.Printf(">> after settring block %s. supported by %+v\n\n", block, block.SupportedBy)
|
||||||
|
|
||||||
slices.SortFunc(block.SupportedBy, func(a *Block, b *Block) int {
|
|
||||||
return cmp.Compare(a.NameNum, b.NameNum)
|
|
||||||
})
|
|
||||||
block.SupportedBy = slices.Compact(block.SupportedBy)
|
|
||||||
|
|
||||||
// time.Sleep(500 * time.Millisecond)
|
// time.Sleep(500 * time.Millisecond)
|
||||||
hasMoved = initialZ != block.Z
|
hasMoved = initialZ != block.Z
|
||||||
// if hasMoved {
|
// if hasMoved {
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -2,7 +2,10 @@ module sunshine.industries/aoc2023
|
||||||
|
|
||||||
go 1.21.4
|
go 1.21.4
|
||||||
|
|
||||||
require github.com/tidwall/pinhole v0.0.0-20210130162507-d8644a7c3d19
|
require (
|
||||||
|
github.com/deckarep/golang-set/v2 v2.5.0
|
||||||
|
github.com/tidwall/pinhole v0.0.0-20210130162507-d8644a7c3d19
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fogleman/gg v1.3.0 // indirect
|
github.com/fogleman/gg v1.3.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
||||||
|
github.com/deckarep/golang-set/v2 v2.5.0 h1:hn6cEZtQ0h3J8kFrHR/NrzyOoTnjgW1+FmNJzQ7y/sA=
|
||||||
|
github.com/deckarep/golang-set/v2 v2.5.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
|
||||||
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||||
|
|
Loading…
Reference in New Issue