From d4c5e50a3894396c0b28b1246b332784a17244b1 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Sun, 18 Oct 2015 00:10:00 +0200 Subject: [PATCH 1/2] Moved gnieark to a dedicated subfolder --- Where-is-the-Arrow-Pointing/gnieark/input.txt | 1 + Where-is-the-Arrow-Pointing/{ => gnieark}/resolver.php | 0 Where-is-the-Arrow-Pointing/{ => gnieark}/resolvershorter.php | 0 3 files changed, 1 insertion(+) create mode 120000 Where-is-the-Arrow-Pointing/gnieark/input.txt rename Where-is-the-Arrow-Pointing/{ => gnieark}/resolver.php (100%) rename Where-is-the-Arrow-Pointing/{ => gnieark}/resolvershorter.php (100%) diff --git a/Where-is-the-Arrow-Pointing/gnieark/input.txt b/Where-is-the-Arrow-Pointing/gnieark/input.txt new file mode 120000 index 0000000..4a6d09b --- /dev/null +++ b/Where-is-the-Arrow-Pointing/gnieark/input.txt @@ -0,0 +1 @@ +../input.txt \ No newline at end of file diff --git a/Where-is-the-Arrow-Pointing/resolver.php b/Where-is-the-Arrow-Pointing/gnieark/resolver.php similarity index 100% rename from Where-is-the-Arrow-Pointing/resolver.php rename to Where-is-the-Arrow-Pointing/gnieark/resolver.php diff --git a/Where-is-the-Arrow-Pointing/resolvershorter.php b/Where-is-the-Arrow-Pointing/gnieark/resolvershorter.php similarity index 100% rename from Where-is-the-Arrow-Pointing/resolvershorter.php rename to Where-is-the-Arrow-Pointing/gnieark/resolvershorter.php From fc366a6ceab76aadc1bfd9b9c17c86773641e73c Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Sun, 18 Oct 2015 01:32:57 +0200 Subject: [PATCH 2/2] Initial version --- Where-is-the-Arrow-Pointing/moul/witap.go | 104 ++++++++++++++++++ .../moul/witap_test.go | 63 +++++++++++ 2 files changed, 167 insertions(+) create mode 100644 Where-is-the-Arrow-Pointing/moul/witap.go create mode 100644 Where-is-the-Arrow-Pointing/moul/witap_test.go diff --git a/Where-is-the-Arrow-Pointing/moul/witap.go b/Where-is-the-Arrow-Pointing/moul/witap.go new file mode 100644 index 0000000..ac2c671 --- /dev/null +++ b/Where-is-the-Arrow-Pointing/moul/witap.go @@ -0,0 +1,104 @@ +package witap + +import ( + "fmt" + "strings" +) + +type Resolver struct { + Lines []string +} + +type Position struct{ X, Y int } + +func (p *Position) Up() Position { return Position{X: p.X, Y: p.Y - 1} } +func (p *Position) Down() Position { return Position{X: p.X, Y: p.Y + 1} } +func (p *Position) Left() Position { return Position{X: p.X - 1, Y: p.Y} } +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"), + } +} + +func (r *Resolver) GetStartPosition() (Position, error) { + for y, line := range r.Lines { + if pos := strings.Index(line, "S"); pos != -1 { + return Position{pos, y}, nil + } + } + return Position{-1, -1}, fmt.Errorf("No such starting point") +} + +func (r *Resolver) Run() (string, error) { + startPos, err := r.GetStartPosition() + if err != nil { + return "", err + } + + return r.Step(startPos, Position{-1, -1}) +} + +func (r *Resolver) PrintMap() { + fmt.Println(strings.Join(r.Lines, "\n")) +} + +func (p *Position) Continue(lastPos Position) Position { + return Position{ + X: 2*p.X - lastPos.X, + Y: 2*p.Y - lastPos.Y, + } +} + +func (r *Resolver) GetChar(position Position) string { + return string(r.Lines[position.Y][position.X]) +} + +func (p *Position) AllDirections() []Position { + return []Position{ + p.Up(), p.Down(), p.Left(), p.Right(), + } +} + +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") + } + + 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") + } + + switch letter { + case "S", "+": + for _, nextPos := range curPos.AllDirections() { + if nextPos == lastPos { + continue + } + letter, err := r.Step(nextPos, curPos) + if err == nil { + return letter, err + } + } + return "", fmt.Errorf("Nothing to do, cannot continue") + case "-", "|": + return r.Step(curPos.Continue(lastPos), curPos) + case "V": + return r.Step(curPos.Down(), curPos) + case ">": + return r.Step(curPos.Right(), curPos) + case "<": + return r.Step(curPos.Left(), curPos) + case "^": + return r.Step(curPos.Up(), curPos) + case " ": + return "", fmt.Errorf("Invalid path, cannot continue") + default: + return letter, nil + } +} diff --git a/Where-is-the-Arrow-Pointing/moul/witap_test.go b/Where-is-the-Arrow-Pointing/moul/witap_test.go new file mode 100644 index 0000000..57e3c97 --- /dev/null +++ b/Where-is-the-Arrow-Pointing/moul/witap_test.go @@ -0,0 +1,63 @@ +package witap + +import ( + "fmt" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +var inputs = []string{ + ` +d S------+ b + | + | + c +--->a +`, ` +S-----+---a->c + | + V + b +`, ` +a S s + | | + V V + b c +`, ` +d s<+S+--V + ||| Q + -++ +`, ` +d s-+ +-S +--+ + +-->b | | | + | | +--+ | + +--+ A<----+ +`, ` +S-----+ +| +-^ ++---+->B + +---^ +`, +} +var outputs = []string{ + "a", "b", "b", "Q", "A", "B", +} + +func ExampleResolver_a() { + resolver := NewResolverFromString(inputs[0]) + fmt.Println(resolver) + // fixme: add output +} + +func TestResolver(t *testing.T) { + Convey("Testing resolver", t, func() { + for idx, input := range inputs { + Convey(fmt.Sprintf("input %d", idx+1), func() { + resolver := NewResolverFromString(input[1 : len(input)-1]) + output, err := resolver.Run() + So(err, ShouldBeNil) + So(output, ShouldEqual, outputs[idx]) + }) + } + }) +}