Fix infinite recursions

master
Manfred Touron 9 years ago
parent b27151efe1
commit 3fdedcb83d
No known key found for this signature in database
GPG Key ID: 0DCB9CE0CABAE1B5

@ -6,7 +6,8 @@ import (
)
type Resolver struct {
Lines []string
Lines []string
Tryouts map[Position]bool
}
type Position struct{ X, Y int }
@ -18,7 +19,8 @@ func (p *Position) Right() Position { return Position{X: p.X + 1, Y: p.Y} }
func NewResolverFromString(theMap string) Resolver {
return Resolver{
Lines: strings.Split(theMap, "\n"),
Lines: strings.Split(theMap, "\n"),
Tryouts: make(map[Position]bool),
}
}
@ -37,6 +39,10 @@ func (r *Resolver) Run() (string, error) {
return "", err
}
fmt.Println("===============================")
r.PrintMap()
fmt.Println("")
return r.Step(startPos, Position{-1, -1})
}
@ -65,13 +71,29 @@ func (r *Resolver) Step(curPos, lastPos Position) (string, error) {
if curPos.X < 0 || curPos.Y < 0 || curPos.Y >= len(r.Lines) || curPos.X >= len(r.Lines[curPos.Y]) {
return "", fmt.Errorf("Invalid position")
}
if _, exists := r.Tryouts[curPos]; exists {
return "", fmt.Errorf("Location already tried")
}
r.Tryouts[curPos] = true
letter := r.GetChar(curPos)
if letter == "S" && lastPos.X != -1 {
return "", fmt.Errorf("S must be the first position")
} else if letter == "+" && lastPos.X == -1 {
return "", fmt.Errorf("Cannot do this symbol after a start")
// fmt.Println(curPos, r.GetChar(curPos))
switch letter {
case "S":
if lastPos.X != -1 {
return "", fmt.Errorf("'S' must be the first position")
}
case "+":
if lastPos.X == -1 {
return "", fmt.Errorf("Cannot do this symbol after a start")
}
case "V", ">", "<", "^":
if r.GetChar(lastPos) == "+" {
return "", fmt.Errorf("Cannot finish just after a '+'")
}
}
switch letter {

@ -37,10 +37,43 @@ S-----+
| +-^
+---+->B
+---^
`,
`
+---+
| |
+---S---+
| |
+++++>A
+++++>B
+++++>C
|
>Y
`,
`
+-------+ +-------+--+-------+ +------+
| | | +-+ | | | | ++
| +----+ | | | | | +----+ | +-+ |
| | | | | | | | | | | |
| +--+ | +-+ ++ | +--+ | | | |
S----+ | | +--+ | | | | | +--->B
| +--+ | +-+ ++ | +--+ | | | |
| | | | | | | | | | | |
| | | | | | | +----+ | +-+ |
| | | | | | | | | ++
+--+-------+--+ +--+ +-------+--+------+
`,
`
+--S--+
O<| | |>O
+-+++-+
+-+++-+
+-+++-+
O<| | |>O
+->E<-+
`,
}
var outputs = []string{
"a", "b", "b", "Q", "A", "B",
"a", "b", "b", "Q", "A", "B", "Y", "B", "E",
}
func ExampleResolver_a() {

Loading…
Cancel
Save