Wireworld
Conway's Game of Life
It is capable of doing sophisticated computations with appropriate programs
(it is actually Turing complete),
and is much simpler to program for.

A Wireworld arena consists of a Cartesian grid of cells,
each of which can be in one of four states.
All cell transitions happen simultaneously.

The cell transition rules are this:



 Input State

 Output State

 Condition


 empty

 empty




 electron head 

 electron tail 




 electron tail 

 conductor




 conductor

 electron head 

 if 1 or 2 cells in the neighborhood of the cell are in the state “electron head”


 conductor

 conductor

 otherwise



Task

Create a program that reads a Wireworld program from a file and displays an animation of the processing. Here is a sample description file (using "H" for an electron head, "t" for a tail, "." for a conductor and a space for empty) you may wish to test with, which demonstrates two cycle-3 generators and an inhibit gate:

tH.........
.   .
   ...
.   .
Ht.. ......

While text-only implementations of this task are possible, mapping cells to pixels is advisable if you wish to be able to display large designs. The logic is not significantly more complex.




 
local map = {{'t', 'H', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
             {'.', ' ', ' ', ' ', '.'},
             {' ', ' ', ' ', '.', '.', '.'},
             {'.', ' ', ' ', ' ', '.'},
             {'H', 't', '.', '.', ' ', '.', '.', '.', '.', '.', '.'}}
 
function step(map)
    local next = {}
    for i = 1, #map do
        next[i] = {}
        for j = 1, #map[i] do
            next[i][j] = map[i][j]
            if map[i][j] == "H" then
                next[i][j] = "t"
            elseif map[i][j] == "t" then
                next[i][j] = "."
            elseif map[i][j] == "." then
                local count = 	((map[i-1] or {})[j-1] == "H" and 1 or 0) +
                                ((map[i-1] or {})[j] == "H" and 1 or 0) +
                                ((map[i-1] or {})[j+1] == "H" and 1 or 0) +
                                ((map[i] or {})[j-1] == "H" and 1 or 0) +
                                ((map[i] or {})[j+1] == "H" and 1 or 0) +
                                ((map[i+1] or {})[j-1] == "H" and 1 or 0) +
                                ((map[i+1] or {})[j] == "H" and 1 or 0) +
                                ((map[i+1] or {})[j+1] == "H" and 1 or 0)
                if count == 1 or count == 2 then
                    next[i][j] = "H"
                else
                    next[i][j] = "."
                end
            end
        end
    end
    return next
end
 
if not not love then
    local time, frameTime, size = 0, 0.25, 20
    local colors = {["."] = {255, 200, 0},
                    ["t"] = {255, 0, 0},
                    ["H"] = {0, 0, 255}}
    function love.update(dt)
        time = time + dt
        if time > frameTime then
            time = time - frameTime
            map = step(map)
        end
    end
 
    function love.draw()
        for i = 1, #map do
            for j = 1, #map[i] do
                love.graphics.setColor(colors[map[i][j]] or {0, 0, 0})
                love.graphics.rectangle("fill", j*size, i*size, size, size)
            end
        end
    end
else
    for iter = 1, 10 do
        print("\nstep "..iter.."\n")
        for i = 1, #map do
            for j = 1, #map[i] do
                io.write(map[i][j])
            end
            io.write("\n")
        end
        map = step(map)
    end
end
 