Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,29 @@
defmodule Langtons do
def ant(sizex, sizey) do
{px, py} = {div(sizex,2), div(sizey,2)} # start position
move(HashSet.new, sizex, sizey, px, py, {1,0}, 0)
end
defp move(plane, sx, sy, px, py, _, step) when px<0 or sx<px or py<0 or sy<py, do:
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}),
do: {Set.delete(plane, {px,py}), turn(dir, :right)},
else: {Set.put(plane, {px,py}), turn(dir, :left)}
move(plane2, sx, sy, px+dx, py+dy, {dx,dy}, step+1)
end
defp turn({dx, dy}, :right), do: {dy, -dx}
defp turn({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 ->
Enum.map(0..sx, fn i ->
if Set.member?(plane, {i,j}), do: "#", else: "."
end) |> Enum.join |> IO.puts
end)
end
end
Langtons.ant(100, 100)