Langton's ant: Difference between revisions
Content added Content deleted
(Added Chapel Version) |
(Added Elixir) |
||
Line 1,695: | Line 1,695: | ||
__####______________________________________________________________________________________________ |
__####______________________________________________________________________________________________ |
||
___##_______________________________________________________________________________________________ |
___##_______________________________________________________________________________________________ |
||
</pre> |
|||
=={{header|Elixir}}== |
|||
{{trans|Ruby}} |
|||
<lang elixir>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)</lang> |
|||
{{out}} |
|||
<pre style="height:40ex;overflow:scroll"> |
|||
out of bounds after 11669 moves: (26, -1) |
|||
..........................#.#........................................................................ |
|||
........................##.#.#....................................................................... |
|||
.......................#.###.##...................................................................... |
|||
......................####.###.#..................................................................... |
|||
......................#####.#..##.................................................................... |
|||
.......................#...##.##.#................................................................... |
|||
........................###...#..##.................................................................. |
|||
.........................#...##.##.#................................................................. |
|||
..........................###...#..##................................................................ |
|||
...........................#...##.##.#............................................................... |
|||
............................###...#..##.............................................................. |
|||
.............................#...##.##.#............................................................. |
|||
..............................###...#..##............................................................ |
|||
...............................#...##.##.#........................................................... |
|||
................................###...#..##.......................................................... |
|||
.................................#...##.##.#......................................................... |
|||
..................................###...#..##........................................................ |
|||
...................................#...##.##.#....................................................... |
|||
....................................###...#..##...................................................... |
|||
.....................................#...##.##.#..................................................... |
|||
......................................###...#..##.................................................... |
|||
.......................................#...##.##.#................................................... |
|||
........................................###...#..##.................................................. |
|||
.........................................#...##.##.#................................................. |
|||
..........................................###...#..##................................................ |
|||
...........................................#...##.##.#............................................... |
|||
............................................###...#..##.............................................. |
|||
.............................................#...##.##.#............................................. |
|||
..............................................###...#..##............................................ |
|||
...............................................#...##.##.#........................................... |
|||
................................................###...#..##.......................................... |
|||
.................................................#...##.##.#..##..................................... |
|||
..................................................###...#..##..##.................................... |
|||
...................................................#...##.##..##...#................................. |
|||
.............................................####...###...#...#..###................................. |
|||
............................................#....#...#...##.####...#................................. |
|||
...........................................###....#...#.#......#.##.#................................ |
|||
...........................................###....#.##.....#.##..#.##................................ |
|||
............................................#....#...##.#.#.....##................................... |
|||
............................................#.#......#.#####..#...#.................................. |
|||
...........................................#...#####..........##.######.............................. |
|||
...........................................###..##..#.##.#.#.#...##.#.##............................. |
|||
.........................................##..#.#######.#...#..###....##.#............................ |
|||
........................................#..#..######.##...#..#.##...#...#............................ |
|||
.......................................#....#.#.##.#..######.#######...#............................. |
|||
.......................................#.####.##.#.####....##..##.#.##.#............................. |
|||
........................................#....####...#..#.######.##....###............................ |
|||
...........................................#...#.##.#.###.#..##..##...###............................ |
|||
..............................................#######....#..##.##.#.....#............................ |
|||
......................................####..##.##..####.##.##.##..#.....#............................ |
|||
.....................................#....#.#...###.##.###....#.####....#............................ |
|||
....................................###.......###.#.#.#####....#.#......#............................ |
|||
....................................#.#...###.####.##.#...##.###.##.....#............................ |
|||
..........................................##.##..####....####.#.#.#.....#............................ |
|||
.....................................#....#..##...###..###.....###......#............................ |
|||
.....................................##...##.###.####..#......###...##..#............................ |
|||
.....................................##.#.####.....#...#..#.##.###.##...#............................ |
|||
....................................####.##...##.####..#.#..#..#..###...#............................ |
|||
....................................#.##.###..#.#.##.#.#.....#.#.....#.#............................. |
|||
........................................#.#..#....##.##..#.#..###.##................................. |
|||
........................................##.#....#..#####.#....#....#..#.#............................ |
|||
.......................................#.##.#..#....##.##.#..###......###............................ |
|||
.....................................#.#...#..#..#..#..###...##..##....#............................. |
|||
....................................###.#.#####.######.###.#######.#.##.............................. |
|||
....................................#.#.#....#####...##..#####.#####................................. |
|||
......................................#..##...#......#..#.##..###.###................................ |
|||
...................................####...#####.#########...#.#...................................... |
|||
..............................##....#..#.....###.#.#...#.###..###.................................... |
|||
.............................#..#..####.##...###.##...###.##.....##.................................. |
|||
............................###....#.##.#.#####...#....#..#..##.###.................................. |
|||
............................#.#####.#.#...##..##.....#....#...#..#................................... |
|||
................................######.####..##.#...#..##..#.#.##.................................... |
|||
..............................##......#.###.##..####...#...###....................................... |
|||
...............................#..#.#####..#...#.##...#..#..#........................................ |
|||
...............................##.###.#######.....#.....#.##......................................... |
|||
..............................#.#..##.##......#...##....#............................................ |
|||
.............................#..#.####........###..##..#............................................. |
|||
.............................#.##.###............##..##.............................................. |
|||
..............................##..................................................................... |
|||
...............................##.................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
..................................................................................................... |
|||
</pre> |
</pre> |
||