/
block.go
66 lines (54 loc) · 1.3 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package exercises
import (
"image"
"github.com/asphaltbuffet/advent-of-code/pkg/set"
)
type block struct {
up *adjBlock
right *adjBlock
left *adjBlock
down *adjBlock
dims image.Rectangle
adjascentZones set.Set[image.Rectangle]
}
// add b as a neighbor to a in direction dir
// and vice-versa with the inverse direction
func (bl *block) addAdjascent(bl2 *block, fromDir, toDir direction) (added bool) {
if bl.adjascentZones.Has(bl2.dims) {
return false
}
inverseToDir := toDir.invert()
inverseFromDir := fromDir.invert()
if bl.hasAdjascent(fromDir) || bl2.hasAdjascent(inverseToDir) {
return false
}
bl.setAdjascent(bl2, fromDir, toDir)
bl2.setAdjascent(bl, inverseToDir, inverseFromDir)
return true
}
func (bl *block) setAdjascent(bl2 *block, fromDir, toDir direction) {
switch fromDir {
case upD:
bl.up = &adjBlock{bl2, toDir}
case rightD:
bl.right = &adjBlock{bl2, toDir}
case leftD:
bl.left = &adjBlock{bl2, toDir}
case downD:
bl.down = &adjBlock{bl2, toDir}
}
bl.adjascentZones.Add(bl2.dims)
}
func (bl *block) hasAdjascent(dir direction) bool {
switch dir {
case upD:
return bl.up != nil
case rightD:
return bl.right != nil
case leftD:
return bl.left != nil
case downD:
return bl.down != nil
}
return false
}