Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
59
Task/Langtons-ant/EchoLisp/langtons-ant.echolisp
Normal file
59
Task/Langtons-ant/EchoLisp/langtons-ant.echolisp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
(lib 'plot)
|
||||
(lib 'types)
|
||||
|
||||
(define (move iter x dir constant: plane turns cmax width xmax (cidx 0))
|
||||
(while (> iter 0)
|
||||
;; get color index of current square
|
||||
(set! cidx (vector-ref plane x))
|
||||
|
||||
;; turn
|
||||
(if (vector-ref turns cidx)
|
||||
(set! dir (if (= dir 3) 0 (1+ dir))) ;; right is #t
|
||||
(set! dir (if (= dir 0) 3 (1- dir))))
|
||||
|
||||
;; rotate colors
|
||||
(set! cidx (if (= cidx cmax) 0 (1+ cidx)))
|
||||
(vector-set! plane x cidx)
|
||||
|
||||
;; move
|
||||
;; x = v + h*width for a pixel at (h,v)
|
||||
(set! x
|
||||
(cond
|
||||
((= dir 0) (1+ x))
|
||||
((= dir 1) (+ x width))
|
||||
((= dir 2) (1- x))
|
||||
((= dir 3) (- x width))))
|
||||
|
||||
(when (or (< x 0) (>= x xmax)) (set! iter -666)) ;; out of bounds
|
||||
(set! iter (1- iter)))
|
||||
iter)
|
||||
|
||||
;; a color table of 16 colors
|
||||
(define colors
|
||||
(list 0 (rgb 1 1 1) (rgb 1 0 0) (rgb 0 1 0) (rgb 0 0 1) (rgb 1 1 0) (rgb 1 0 1) (rgb 0 1 1)))
|
||||
(define colors (list->vector (append colors colors)))
|
||||
|
||||
;; transform color index into rgb color, using colors table.
|
||||
(define (colorize plane xmax)
|
||||
(for ((x xmax))
|
||||
(vector-set! plane x (vector-ref colors (vector-ref plane x))))
|
||||
(vector->pixels plane)
|
||||
xmax )
|
||||
|
||||
;; ant's patterns
|
||||
(define turns #(#t #t #f #f #f #t #f #f #f #t #t #t)) ;; RRLLLRLLLRRR
|
||||
;;(define turns #(#t #t #f #f #f #t #t #f)) ; RRLLLRRL
|
||||
;;(define turns #(#t #f)) ; RL : basic ant
|
||||
|
||||
(define (ant (iter 100000))
|
||||
(plot-clear)
|
||||
(define width (first (pixels-dim))) ;; plane dimensions
|
||||
(define height (rest (pixels-dim)))
|
||||
(define plane (pixels->uint32-vector))
|
||||
(define x (+ (quotient width 2) (* width (quotient height 2)))) ;; middle of plane
|
||||
(define xmax (* width height))
|
||||
|
||||
(move iter x 0 plane turns (1- (vector-length turns)) width xmax)
|
||||
(colorize plane xmax))
|
||||
|
||||
(ant) ;; run
|
||||
132
Task/Langtons-ant/Elm/langtons-ant.elm
Normal file
132
Task/Langtons-ant/Elm/langtons-ant.elm
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
import Maybe as M
|
||||
import Matrix
|
||||
import Time exposing (Time, every, second)
|
||||
import List exposing (..)
|
||||
import String exposing (join)
|
||||
import Html exposing (div, h1, text)
|
||||
import Html.App exposing (program)
|
||||
import Svg
|
||||
import Svg.Attributes exposing (version, viewBox, cx, cy, r, x, y, x1, y1, x2, y2, fill,style, width, height, preserveAspectRatio)
|
||||
|
||||
w = 700
|
||||
h = 700
|
||||
dt = 0.0001
|
||||
|
||||
type Direction = North | West | South | East
|
||||
|
||||
type alias Model =
|
||||
{ rows : Int
|
||||
, cols : Int
|
||||
, boxes : Matrix.Matrix Bool
|
||||
, location : Matrix.Location
|
||||
, direction : Direction
|
||||
}
|
||||
|
||||
initModel : Int -> Int -> Model
|
||||
initModel cols rows =
|
||||
{ rows = rows
|
||||
, cols = cols
|
||||
, boxes = Matrix.matrix rows cols (\location -> False)
|
||||
, location = (rows//2,cols//2)
|
||||
, direction = North
|
||||
}
|
||||
|
||||
view model =
|
||||
let
|
||||
borderLineStyle = style "stroke:black;stroke-width:0.3"
|
||||
|
||||
x1Min = x1 <| toString 0
|
||||
y1Min = y1 <| toString 0
|
||||
x1Max = x1 <| toString model.cols
|
||||
y1Max = y1 <| toString model.rows
|
||||
x2Min = x2 <| toString 0
|
||||
y2Min = y2 <| toString 0
|
||||
x2Max = x2 <| toString model.cols
|
||||
y2Max = y2 <| toString model.rows
|
||||
|
||||
borders = [ Svg.line [ x1Min, y1Min, x2Max, y2Min, borderLineStyle ] []
|
||||
, Svg.line [ x1Max, y1Min, x2Max, y2Max, borderLineStyle ] []
|
||||
, Svg.line [ x1Max, y1Max, x2Min, y2Max, borderLineStyle ] []
|
||||
, Svg.line [ x1Min, y1Max, x2Min, y2Min, borderLineStyle ] []
|
||||
]
|
||||
|
||||
circleInBox (row,col) color =
|
||||
Svg.circle [ r "0.25"
|
||||
, fill (color)
|
||||
, cx (toString (toFloat col + 0.5))
|
||||
, cy (toString (toFloat row + 0.5))
|
||||
] []
|
||||
|
||||
showUnvisited location box =
|
||||
if box then [circleInBox location "black" ]
|
||||
else []
|
||||
|
||||
unvisited = model.boxes
|
||||
|> Matrix.mapWithLocation showUnvisited
|
||||
|> Matrix.flatten
|
||||
|> concat
|
||||
|
||||
maze = [ Svg.g [] <| borders ++ unvisited ]
|
||||
|
||||
in
|
||||
div
|
||||
[]
|
||||
[ h1 [] [text "Langton's Ant"]
|
||||
, Svg.svg
|
||||
[ version "1.1"
|
||||
, width (toString w)
|
||||
, height (toString h)
|
||||
, viewBox (join " "
|
||||
[ 0 |> toString
|
||||
, 0 |> toString
|
||||
, model.cols |> toString
|
||||
, model.rows |> toString ])
|
||||
]
|
||||
maze
|
||||
]
|
||||
|
||||
updateModel : Model -> Model
|
||||
updateModel model =
|
||||
let current = model.location
|
||||
inBox = snd current >= 0 && snd current < model.cols
|
||||
&& fst current >= 0 && fst current < model.rows
|
||||
in if not inBox then
|
||||
model
|
||||
else
|
||||
let currentValue = Matrix.get current model.boxes |> M.withDefault False
|
||||
|
||||
dir = case (model.direction, currentValue) of
|
||||
(North, True) -> East
|
||||
(East, True) -> South
|
||||
(South, True) -> West
|
||||
(West, True) -> North
|
||||
|
||||
(North, False) -> West
|
||||
(East, False) -> North
|
||||
(South, False) -> East
|
||||
(West, False) -> South
|
||||
|
||||
next = case dir of
|
||||
North -> (fst current+1, snd current)
|
||||
South -> (fst current-1, snd current)
|
||||
East -> (fst current, snd current+1)
|
||||
West -> (fst current, snd current-1)
|
||||
|
||||
boxes = Matrix.set current (not currentValue) model.boxes
|
||||
|
||||
in {model | boxes=boxes, location=next, direction=dir}
|
||||
|
||||
type Msg = Tick Time
|
||||
|
||||
subscriptions model = every (dt * second) Tick
|
||||
|
||||
main =
|
||||
let
|
||||
update msg model = (updateModel model, Cmd.none)
|
||||
init = (initModel 100 100 , Cmd.none)
|
||||
in program
|
||||
{ init = init
|
||||
, view = view
|
||||
, update = update
|
||||
, subscriptions = subscriptions
|
||||
}
|
||||
65
Task/Langtons-ant/FreeBASIC/langtons-ant.freebasic
Normal file
65
Task/Langtons-ant/FreeBASIC/langtons-ant.freebasic
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
' version 16-10-2016
|
||||
' compile with: fbc -s gui
|
||||
|
||||
' a cell size of 4 x 4 pixels is used
|
||||
' In FreeBASIC the 0,0 is the top left corner
|
||||
|
||||
ScreenRes 400,400,8 ' give a 100 by 100 field
|
||||
Dim As UByte Ptr p = ScreenPtr
|
||||
If p = 0 Then End ' p does not point to screen
|
||||
|
||||
Palette 0, 0, 0, 0 ' index 0 = black
|
||||
Palette 255, 255, 255, 255 ' index 225 = white
|
||||
|
||||
Line (0, 0) - (799, 799), 255, bf ' draw box and fill it with white color
|
||||
|
||||
Dim As Integer count, offset, x = 199, y = 199
|
||||
Dim As UByte col ' = color
|
||||
' direction, 0 = up, 1 = right, 2 = down, 3 = left
|
||||
Dim As UByte d ' d = 0, looking up
|
||||
|
||||
Do
|
||||
offset = x + y * 400
|
||||
col = p[offset]
|
||||
|
||||
If col = 0 Then
|
||||
d = (d -1) And 3
|
||||
Else
|
||||
d = (d +1) And 3
|
||||
EndIf
|
||||
|
||||
col = col Xor 255 ' flip the color
|
||||
|
||||
ScreenLock ' don't update screen while we are drawing
|
||||
|
||||
' draw a 4*4 block and paint it with palette color [0 | 255]
|
||||
Line (x, y) - (x +3, y -3), col, bf
|
||||
|
||||
ScreenUnLock ' allow screen update's
|
||||
|
||||
'Sleep 100 ' slow the program down if needed
|
||||
|
||||
' true = 0, false = -1
|
||||
If (d And 1) = 1 Then
|
||||
x = x + (d = 1) * 4 - (d = 3) * 4
|
||||
Else
|
||||
y = y - (d = 0) * 4 + (d = 2) * 4
|
||||
End If
|
||||
|
||||
count += 1
|
||||
' update step count window title bar
|
||||
WindowTitle "Langton's ant step: " + Str(count)
|
||||
|
||||
' has user clicked on close window "X" then end program
|
||||
If InKey = Chr(255) + "k" Then End
|
||||
|
||||
Loop Until x < 1 Or x > 398 Or y < 1 Or y > 398
|
||||
|
||||
' display total count in window title bar
|
||||
WindowTitle "Langton's ant has left the field in " + Str(count) + " steps"
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
'Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
93
Task/Langtons-ant/GFA-Basic/langtons-ant.gfa
Normal file
93
Task/Langtons-ant/GFA-Basic/langtons-ant.gfa
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
'
|
||||
' Langton's ant
|
||||
'
|
||||
' World is a global boolean array, 100x100 in size
|
||||
width%=100
|
||||
height%=100
|
||||
DIM world!(width%,height%)
|
||||
ARRAYFILL world!(),FALSE
|
||||
' Time in world
|
||||
time%=0
|
||||
' Ant is represented by a global three-element array
|
||||
' holding: x, y, direction [0=north,1=west,2=south,3=east]
|
||||
DIM ant%(3)
|
||||
'
|
||||
@setup_ant
|
||||
@run_ant
|
||||
@display_world
|
||||
'
|
||||
' Displays the world to file "langton.out": . for false, # for true
|
||||
'
|
||||
PROCEDURE display_world
|
||||
LOCAL i%,j%
|
||||
OPEN "o",#1,"langton.out"
|
||||
PRINT #1,"Time in world: ";time%;" ticks"
|
||||
FOR i%=0 TO width%-1
|
||||
FOR j%=0 TO height%-1
|
||||
IF world!(i%,j%)
|
||||
PRINT #1,"#";
|
||||
ELSE
|
||||
PRINT #1,".";
|
||||
ENDIF
|
||||
NEXT j%
|
||||
PRINT #1,""
|
||||
NEXT i%
|
||||
CLOSE #1
|
||||
RETURN
|
||||
'
|
||||
' Set up the ant to start at (50,50) facing north
|
||||
'
|
||||
PROCEDURE setup_ant
|
||||
ant%(0)=50
|
||||
ant%(1)=50
|
||||
ant%(2)=0
|
||||
RETURN
|
||||
'
|
||||
' check if ant position is within world's bounds
|
||||
'
|
||||
FUNCTION ant_in_world
|
||||
RETURN ant%(0)>=0 AND ant%(0)<width% AND ant%(1)>=0 AND ant%(1)<height%
|
||||
ENDFUNC
|
||||
'
|
||||
' Turn ant direction to left
|
||||
'
|
||||
PROCEDURE ant_turn_left
|
||||
ant%(2)=(ant%(2)+1) MOD 4
|
||||
RETURN
|
||||
'
|
||||
' Turn ant direction to right
|
||||
'
|
||||
PROCEDURE ant_turn_right
|
||||
ant%(2)=(ant%(2)+3) MOD 4
|
||||
RETURN
|
||||
'
|
||||
' Ant takes a step forward in current direction
|
||||
'
|
||||
PROCEDURE ant_step_forward
|
||||
SELECT ant%(2)
|
||||
CASE 0
|
||||
ant%(0)=ant%(0)+1
|
||||
CASE 1
|
||||
ant%(1)=ant%(1)+1
|
||||
CASE 2
|
||||
ant%(0)=ant%(0)-1
|
||||
CASE 3
|
||||
ant%(1)=ant%(1)-1
|
||||
ENDSELECT
|
||||
RETURN
|
||||
'
|
||||
' Run the ant until it falls out of the world
|
||||
'
|
||||
PROCEDURE run_ant
|
||||
WHILE @ant_in_world
|
||||
time%=time%+1
|
||||
IF world!(ant%(0),ant%(1)) ! true for white
|
||||
world!(ant%(0),ant%(1))=FALSE
|
||||
@ant_turn_left
|
||||
ELSE ! false for black
|
||||
world!(ant%(0),ant%(1))=TRUE
|
||||
@ant_turn_right
|
||||
ENDIF
|
||||
@ant_step_forward
|
||||
WEND
|
||||
RETURN
|
||||
34
Task/Langtons-ant/Nim/langtons-ant.nim
Normal file
34
Task/Langtons-ant/Nim/langtons-ant.nim
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import strutils, sequtils
|
||||
|
||||
type
|
||||
Direction = enum up, right, down, left
|
||||
Color = enum white, black
|
||||
|
||||
const
|
||||
width = 75
|
||||
height = 52
|
||||
maxSteps = 12_000
|
||||
|
||||
var
|
||||
m: array[height, array[width, Color]]
|
||||
dir = up
|
||||
x = width div 2
|
||||
y = height div 2
|
||||
|
||||
var i = 0
|
||||
while i < maxSteps and x in 0 .. < width and y in 0 .. < height:
|
||||
let turn = m[y][x] == black
|
||||
m[y][x] = if m[y][x] == black: white else: black
|
||||
|
||||
dir = Direction((4 + int(dir) + (if turn: 1 else: -1)) mod 4)
|
||||
case dir
|
||||
of up: dec y
|
||||
of right: dec x
|
||||
of down: inc y
|
||||
of left: inc x
|
||||
|
||||
inc i
|
||||
|
||||
for row in m:
|
||||
echo map(row, proc(x: Color): string =
|
||||
if x == white: "." else: "#").join("")
|
||||
15
Task/Langtons-ant/Phix/langtons-ant.phix
Normal file
15
Task/Langtons-ant/Phix/langtons-ant.phix
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
sequence grid = repeat(repeat(' ',100),100)
|
||||
integer aX = 50, aY = 50,
|
||||
gXY, angle = 1 -- ' '/'#'; 0,1,2,3 = NESW
|
||||
constant dX = {0,-1,0,1} -- (dY = reverse(dX))
|
||||
|
||||
while aX>=1 and aX<=100
|
||||
and aY>=1 and aY<=100 do
|
||||
gXY = grid[aX][aY]
|
||||
grid[aX][aY] = 67-gXY -- ' '<=>'#', aka 32<->35
|
||||
angle = mod(angle+2*gXY+3,4) -- +/-1, ie 0,1,2,3 -> 1,2,3,0 or 3,0,1,2
|
||||
aX += dX[angle+1]
|
||||
aY += dX[4-angle]
|
||||
end while
|
||||
|
||||
puts(1,join(grid,"\n"))
|
||||
55
Task/Langtons-ant/Ring/langtons-ant.ring
Normal file
55
Task/Langtons-ant/Ring/langtons-ant.ring
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
load "guilib.ring"
|
||||
load "stdlib.ring"
|
||||
|
||||
new qapp
|
||||
{
|
||||
win1 = new qwidget() {
|
||||
setwindowtitle("drawing using qpainter")
|
||||
setgeometry(100,100,500,500)
|
||||
label1 = new qlabel(win1) {
|
||||
setgeometry(10,10,400,400)
|
||||
settext("")
|
||||
}
|
||||
new qpushbutton(win1) {
|
||||
setgeometry(200,400,100,30)
|
||||
settext("draw")
|
||||
setclickevent("draw()")
|
||||
}
|
||||
show()
|
||||
}
|
||||
exec()
|
||||
}
|
||||
|
||||
func draw
|
||||
p1 = new qpicture()
|
||||
color = new qcolor() {
|
||||
setrgb(0,0,255,255)
|
||||
}
|
||||
pen = new qpen() {
|
||||
setcolor(color)
|
||||
setwidth(1)
|
||||
}
|
||||
new qpainter() {
|
||||
begin(p1)
|
||||
setpen(pen)
|
||||
|
||||
fieldsize=100
|
||||
field = newlist(fieldsize,fieldsize)
|
||||
x=fieldsize/2
|
||||
y=fieldsize/2
|
||||
d=0
|
||||
while x<=fieldsize and x>=0 and y<=fieldsize and y>=0
|
||||
if field[x][y]=0 field[x][y]=1 d-=1 else field[x][y]=0 d+=1 ok
|
||||
drawpoint(x*2, y*2)
|
||||
d=(d+4) % 4
|
||||
switch d
|
||||
on 0 y+=1
|
||||
on 1 x+=1
|
||||
on 2 y-=1
|
||||
on 3 x-=1
|
||||
off
|
||||
end
|
||||
|
||||
endpaint()
|
||||
}
|
||||
label1 { setpicture(p1) show() }
|
||||
24
Task/Langtons-ant/Sidef/langtons-ant.sidef
Normal file
24
Task/Langtons-ant/Sidef/langtons-ant.sidef
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
define dirs = [[1,0], [0,-1], [-1,0], [0,1]]
|
||||
define size = 100
|
||||
|
||||
enum |White, Black|
|
||||
var plane = size.of { size.of (White) }
|
||||
|
||||
var (x, y) = @|([size/2 -> int]*2)
|
||||
var dir = dirs.len.irand
|
||||
|
||||
var moves = 0
|
||||
loop {
|
||||
(x >= 0) && (y >= 0) && (x < size) && (y < size) || break
|
||||
|
||||
given(plane[x][y]) {
|
||||
when (White) { dir--; plane[x][y] = Black }
|
||||
when (Black) { dir++; plane[x][y] = White }
|
||||
}
|
||||
|
||||
++moves
|
||||
[\x, \y]:dirs[dir %= dirs.len] -> each {|a,b| *a += b }
|
||||
}
|
||||
|
||||
say "Out of bounds after #{moves} moves at (#{x}, #{y})"
|
||||
plane.map{.map {|square| square == Black ? '#' : '.' }}.each{.join.say}
|
||||
73
Task/Langtons-ant/Swift/langtons-ant.swift
Normal file
73
Task/Langtons-ant/Swift/langtons-ant.swift
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import Foundation
|
||||
|
||||
let WIDTH = 100
|
||||
let HEIGHT = 100
|
||||
|
||||
struct Point {
|
||||
var x:Int
|
||||
var y:Int
|
||||
}
|
||||
|
||||
enum Direction: Int {
|
||||
case North = 0, East, West, South
|
||||
}
|
||||
|
||||
class Langton {
|
||||
let leftTurn = [Direction.West, Direction.North, Direction.South, Direction.East]
|
||||
let rightTurn = [Direction.East, Direction.South, Direction.North, Direction.West]
|
||||
let xInc = [0, 1,-1, 0]
|
||||
let yInc = [-1, 0, 0, 1]
|
||||
var isBlack:[[Bool]]
|
||||
var origin:Point
|
||||
var antPosition = Point(x:0, y:0)
|
||||
var outOfBounds = false
|
||||
var antDirection = Direction.East
|
||||
|
||||
init(width:Int, height:Int) {
|
||||
self.origin = Point(x:width / 2, y:height / 2)
|
||||
self.isBlack = Array(count: width, repeatedValue: Array(count: height, repeatedValue: false))
|
||||
}
|
||||
|
||||
func moveAnt() {
|
||||
self.antPosition.x += xInc[self.antDirection.rawValue]
|
||||
self.antPosition.y += yInc[self.antDirection.rawValue]
|
||||
}
|
||||
|
||||
func step() -> Point {
|
||||
if self.outOfBounds {
|
||||
println("Ant tried to move while out of bounds.")
|
||||
exit(0)
|
||||
}
|
||||
|
||||
var ptCur = Point(x:self.antPosition.x + self.origin.x, y:self.antPosition.y + self.origin.y)
|
||||
let black = self.isBlack[ptCur.x][ptCur.y]
|
||||
let direction = self.antDirection.rawValue
|
||||
|
||||
self.antDirection = (black ? self.leftTurn : self.rightTurn)[direction]
|
||||
|
||||
self.isBlack[ptCur.x][ptCur.y] = !self.isBlack[ptCur.x][ptCur.y]
|
||||
|
||||
self.moveAnt()
|
||||
ptCur = Point(x:self.antPosition.x + self.origin.x, y:self.antPosition.y + self.origin.y)
|
||||
self.outOfBounds =
|
||||
ptCur.x < 0 ||
|
||||
ptCur.x >= self.isBlack.count ||
|
||||
ptCur.y < 0 ||
|
||||
ptCur.y >= self.isBlack[0].count
|
||||
|
||||
return self.antPosition
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let ant = Langton(width: WIDTH, height: HEIGHT)
|
||||
while !ant.outOfBounds {
|
||||
ant.step()
|
||||
}
|
||||
|
||||
for row in 0 ..< WIDTH {
|
||||
for col in 0 ..< HEIGHT {
|
||||
print(ant.isBlack[col][row] ? "#" : " ")
|
||||
}
|
||||
println()
|
||||
}
|
||||
58
Task/Langtons-ant/jq/langtons-ant.jq
Normal file
58
Task/Langtons-ant/jq/langtons-ant.jq
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
def matrix(m; n; init):
|
||||
if m == 0 then [range(0;n)] | map(init)
|
||||
elif m > 0 then [range(0;m)][ range(0;m) ] = matrix(0;n;init)
|
||||
else error("matrix\(m);_;_) invalid")
|
||||
end;
|
||||
|
||||
def printout:
|
||||
. as $grid
|
||||
| ($grid|length) as $height
|
||||
| ($grid[0]|length) as $width
|
||||
| reduce range(0;$height) as $i ("\u001BH";
|
||||
. + reduce range(0;$width) as $j ("\n";
|
||||
. + if $grid[$i][$j] then " " else "#" end ) );
|
||||
|
||||
|
||||
def langtons_ant(grid_size):
|
||||
|
||||
def flip(ant):
|
||||
# Flip the color of the current square
|
||||
.[ant[0]][ant[1]] = (.[ant[0]][ant[1]] | not)
|
||||
;
|
||||
|
||||
# input/output: the ant's state: [x, y, direction]
|
||||
# where direction is one of (0,1,2,3)
|
||||
def move(grid):
|
||||
# If the cell is black, it changes to white and the ant turns left;
|
||||
# If the cell is white, it changes to black and the ant turns right;
|
||||
(if grid[.[0]][.[1]] then 1 else 3 end) as $turn
|
||||
| .[2] = ((.[2] + $turn) % 4)
|
||||
| if .[2] == 0 then .[0] += 1
|
||||
elif .[2] == 1 then .[1] += 1
|
||||
elif .[2] == 2 then .[0] += -1
|
||||
else .[1] += -1
|
||||
end
|
||||
;
|
||||
|
||||
# state: [ant, grid]
|
||||
def iterate:
|
||||
.[0] as $ant | .[1] as $grid
|
||||
# exit if the ant is outside the grid
|
||||
| if $ant[0] < 1 or $ant[0] > grid_size
|
||||
or $ant[1] < 1 or $ant[1] > grid_size
|
||||
then [ $ant, $grid ]
|
||||
else
|
||||
($grid | flip($ant)) as $grid
|
||||
| ($ant | move($grid)) as $ant
|
||||
| [$ant, $grid] | iterate
|
||||
end
|
||||
;
|
||||
|
||||
((grid_size/2) | floor | [ ., ., 0]) as $ant
|
||||
| matrix(grid_size; grid_size; true) as $grid
|
||||
| [$ant, $grid] | iterate
|
||||
| .[1]
|
||||
| printout
|
||||
;
|
||||
|
||||
langtons_ant(100)
|
||||
Loading…
Add table
Add a link
Reference in a new issue