-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
85 lines (71 loc) · 1.27 KB
/
path.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import "fmt"
type Path struct {
Size int
Blocks []*Block
}
func (p *Path) String() string {
return fmt.Sprintf("{%d %v}", p.Size, p.Blocks)
}
func (p *Path) Contains(b *Block) bool {
for _, b1 := range p.Blocks {
if b.Equal(b1) {
return true
}
}
return false
}
// Panicable
func (p *Path) LastBlock() *Block {
return p.Blocks[len(p.Blocks)-1]
}
// Panicable <- Path.LastBlock
func (p *Path) LastNode() *Node {
b := p.LastBlock()
return b.Tail
}
// Panicable <- Path.LastBlock
func (p *Path) Evolve() []*Path {
b := p.LastBlock()
n := b.Tail
if n == nil {
return nil
}
ps := make([]*Path, 0, 4)
od := b.Dir.Opposite()
for _, d := range AllDirs {
if d == od {
continue
}
n1 := n.Move(d)
if n1 == nil {
continue
}
b1 := GetBlock(n, d)
if b1 == nil {
continue
}
if p.Contains(b1) {
continue
}
p1 := Path{p.Size, make([]*Block, 0, len(p.Blocks)+1)}
p1.Blocks = append(p1.Blocks, p.Blocks...)
p1.Blocks = append(p1.Blocks, b1)
ps = append(ps, &p1)
}
return ps
}
func (p *Path) Tax() float64 {
tax := 0.0
for _, b := range p.Blocks {
tax = b.Dir.UpdateTax(tax)
}
return tax
}
func (p *Path) Dirs() []Dir {
ds := make([]Dir, len(p.Blocks))
for i, b := range p.Blocks {
ds[i] = b.Dir
}
return ds
}