by Laurens
A programming language based in Lua and Javascript.

Killa is a programming language based in Lua and Javascript. It uses the Lua virtual machine and the Lua external C API with almost no changes but it has a different syntax similar to Javascript without the prototype mechanism and the ugly parts.

Killa is a work in progress but it is already functional and has been tested inside popular game engines like the Love engin

var USE_BC = true // Set this to false if you don't have bc library.
var one = (USE_BC)? require("bc").number(1) : 1

var IN = 1
var OUT = 2

// Insert board to state.
// If the board is already present just increase the board counter.
// Board data is stored as: [boardCount, boardArray]
function insertBoard(state, boardArray, boardCount) {
    var boardKey = table.concat(boardArray, ',')
    if (state[boardKey] == null) {
        state[boardKey] = [boardCount, boardArray]
    }
    else {
        state[boardKey][0] += boardCount
    }
}

function clone(t) {
    var out = []
    for (var k = 0; k < $t; k += 1) {
        out[k] = t[k]
    }
    return out
}

function euler_393(n) {
    // Initial board has no ants moving IN or OUT (all zero)
    var initBoard = []
    for (var k = 0; k < n; k += 1) {
        initBoard[k] = 0
    }

    // Initial state contains only the initial board
    var state = {}
    var init = table.concat(initBoard, ',')

    // We are going to use a data type: [counter, board] for storing boards
    state[init] = [one, initBoard];

    // Columns
    for (var col = 0; col < n; col += 1) {
        // Rows
        for (var row = 0; row < n; row += 1) {
            var newState = {}
            var newBoard
            for each (var _,data in pairs(state)) {
                var boardCount = data[0]
                var board = data[1]
                if (board[row] == 0) {
                    if ((row < n - 1) && (col < n - 1)) {
                        if (board[row + 1] != IN) {
                            newBoard = clone(board) 
                            newBoard[row] = OUT
                            newBoard[row + 1] += IN
                            insertBoard(newState, newBoard, boardCount)
                        }
                        if (board[row + 1] != OUT) {
                            newBoard = clone(board)
                            newBoard[row] = IN
                            newBoard[row + 1] += OUT
                            insertBoard(newState, newBoard, boardCount)
                        }
                    }
                }
                else if (board[row] == IN) {
                    if ((row < n - 1) && (board[row + 1] != IN)) {
                        newBoard = clone(board)
                        newBoard[row] = 0
                        newBoard[row + 1] += IN
                        insertBoard(newState, newBoard, boardCount)
                    }
                    if (col < n - 1) {
                        insertBoard(newState, board, boardCount)
                    }
                }
                else if (board[row] == OUT) {
                    if ((row < n - 1) && (board[row + 1] != OUT)) {
                        newBoard = clone(board)
                        newBoard[row] = 0
                        newBoard[row + 1] += OUT
                        insertBoard(newState, newBoard, boardCount)
                    }
                    if (col < n - 1) {
                        insertBoard(newState, board, boardCount)
                    }
                }
                else {
                    // board[row] == IN + OUT
                    newBoard = clone(board)
                    newBoard[row] = 0
                    insertBoard(newState, newBoard, boardCount)
                }
            }
            state = newState
        }
    }
    return (state[init] != null)? state[init][0] : 0
}

var tm = os.clock()
print(euler_393(10))
print("Time: ", os.clock() - tm)
Information updated 12/27/19
Note: This entry has not yet been moderated.
View Comments