Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Created April 22, 2015 19:03
Show Gist options
  • Save G33kDude/35be81cf6ee41df05210 to your computer and use it in GitHub Desktop.
Save G33kDude/35be81cf6ee41df05210 to your computer and use it in GitHub Desktop.
Input =
(
$.O...O...
...O......
..........
O..O..O...
..........
O..O..O...
..........
......OO..
O..O....@@
........@@
)
ShrekPos := ""
Grid := []
for y, line in StrSplit(Input, "`n", "`r")
{
if (!ShrekPos && x := InStr(Line, "@"))
ShrekPos := [y, x]
Grid.Push(StrSplit(Line))
}
Path := {Join(",", ShrekPos): ShrekPos}
Positions := [ShrekPos]
While Pos := Positions.Pop()
{
for each, Dir in [[-1,0],[1,0],[0,1],[0,-1]]
{
NewPos := [Dir[1]+Pos[1], Dir[2]+Pos[2]]
if Path[Join(",", NewPos)]
continue
else
Path[Join(",", NewPos)] := Pos
; MsgBox, % NewPos[1] "," NewPos[2]
AtEnd := False, CanMove := True
for each, Cell in [[0,0],[0,1],[1,0],[1,1]]
{
Char := Grid[NewPos[1]+Cell[1], NewPos[2]+Cell[2]]
if (Char == "$")
AtEnd := True
else if (Char == "o")
CanMove := False
else if (Char != "." && Char != "@")
CanMove := False
}
if CanMove
{
Positions.Push(NewPos)
if AtEnd
{
; throw Exception("It worked!" )
Loop
{
for each, Cell in [[0,0],[0,1],[1,0],[1,1]]
Grid[NewPos[1]+Cell[1], NewPos[2]+Cell[2]] := "@"
}
until (NewPos:=Path[Join(",",NewPos)]) == ShrekPos
Out := ""
for each, Row in Grid
Out .= Join(" ", Row) "`n"
Gui, Font,, Courier New
Gui, Add, Edit, ReadOnly, %Out%
Gui, Show
Positions := [] ; Kill the stack! Stop the looping! Lazy man's break!
}
}
}
}
if !Out
MsgBox, No path found
Join(s, a)
{
for k, v in a
o .= s v
return SubStr(o, 1+StrLen(s))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment