Langton's ant: Difference between revisions

→‎{{header|Elixir}}: change Set => MapSet, used in operator
(→‎{{header|Nim}}: Compiles with Nim Compiler Version 0.12.1 (include sequtils, annotate Color type of the final 'x'))
(→‎{{header|Elixir}}: change Set => MapSet, used in operator)
Line 1,766:
def ant(sizex, sizey) do
{px, py} = {div(sizex,2), div(sizey,2)} # start position
move(HashSetMapSet.new, sizex, sizey, px, py, {1,0}, 0)
end
Line 1,772:
print(plane, sx, sy, px, py, step)
defp move(plane, sx, sy, px, py, dir, step) do
{plane2, {dx,dy}} = if Set.member?(plane, {px,py}) in plane,
do: {SetMapSet.delete(plane, {px,py}), turnturn_right(dir, :right)},
else: {SetMapSet.put(plane, {px,py}), turnturn_left(dir, :left)}
move(plane2, sx, sy, px+dx, py+dy, {dx,dy}, step+1)
end
defp turnturn_right({dx, dy}, :right), do: {dy, -dx}
defp turnturn_left({dx, dy}, _), do: {-dy, dx}
defp print(plane, sx, sy, px, py, step) do
IO.puts "out of bounds after #{step} moves: (#{px}, #{py})"
Enum.each(0..sy, fn j ->
IO.puts Enum.map(0..sx, fn i -> if {i,j} in plane, do: "#", else: "." end)
if Set.member?(plane, {i,j}), do: "#", else: "."
end) |> Enum.join |> IO.puts
end)
end
Anonymous user