• Questions about "Life Counter" mission

Question related to mission Life Counter

Hi everybody,

I am not sure if the assertions of this mission are correct. The first assertion has only 7 rows, while the picture in the mission shows 8 rows. If I add [0,0,0,0,0,0,0,0] to the initial state then the correct result will appear.

In the second example my algorithm returns 11 and not 15. I don't know why that is. 11 seems correct. Also there seems to be a row missing, too.

In the third example the result should be 4 and not 5. Here are the states: Initial: [0, 1, 0] [0, 0, 1] [1, 1, 1]

After 1st tick: [0, 0, 0] [1, 0, 1] [0, 1, 1]

After 2nd tick: [0, 0, 0] [0, 1, 1] [0, 1, 1]

And from now on nothing will change. So why should there be 5 alive cells after tick 50?

I hope someone can clarify what I am missunderstanding here?!?

Cheers, Hendrik

Here is my code:

"use strict";
function getLivingNeighbours(tempCells,position){
    var sum = 0;
    for(var i=Math.max(position[0]-1,0);i<Math.min(position[0]+2,tempCells.length);i++){
            for(var j=Math.max(position[1]-1,0);j<Math.min(position[1]+2, tempCells[i].length);j++){
                if(i!==position[0] || j!==position[1]){
                    if(tempCells[i][j]===1){
                        sum++;   
                    }
                }
            }
        }
        return sum;
    }

    function getLivingCells(cells){
        var sum = 0;
        for(var i=0;i<cells.length;i++){
            for(var j=0;j<cells[i].length;j++){
                if(cells[i][j]===1){
                    sum++;   
                }
            }
        }
        console.log(sum);
        return sum;   
    }

    function copyArray(a){
        var b = [];
        for(var i=0;i<a.length;i++){
            b[i] = [];
            for(var j=0;j<a[i].length;j++){
                b[i].push(a[i][j]);    
            }
        }
        return b;
    }

    function lifeCounter(cells, n) {
        var newCells = [];
        for(var nc = 0;nc<cells.length;nc++){
            newCells.push([]);        
        }
        for(var s=0;s<n;s++){
            for(var i=0;i<cells.length;i++){
                for(var j=0;j<cells[i].length;j++){
                    switch(cells[i][j]){
                        case 0:
                            if((getLivingNeighbours(cells,[i,j])===3)){
                                newCells[i][j]=1; 
                            }else{
                                newCells[i][j]=0;    
                            }
                            break;
                        case 1:
                            var neighbours = getLivingNeighbours(cells,[i,j]);
                            if(neighbours<2 || neighbours >3){
                            newCells[i][j]=0;   
                        }else{
                            newCells[i][j]=1;    
                        }
                        break;
                }
            }
        }
        cells = copyArray(newCells);
        console.log(cells);
    }
    return getLivingCells(cells);
}

var assert = require('assert');

if (!global.is_checking) {
    assert.equal(lifeCounter([[0, 1, 0, 0, 0, 0, 0],
                              [0, 0, 1, 0, 0, 0, 0],
                              [1, 1, 1, 0, 0, 0, 0],
                              [0, 0, 0, 0, 0, 1, 1],
                              [0, 0, 0, 0, 0, 1, 1],
                              [0, 0, 0, 0, 0, 0, 0],
                              [1, 1, 1, 0, 0, 0, 0]], 15), 14, "Little later");
    assert.equal(lifeCounter([[0, 1, 0, 0, 0, 0, 0],
                              [0, 0, 1, 0, 0, 0, 0],
                              [1, 1, 1, 0, 0, 0, 0],
                              [0, 0, 0, 0, 0, 1, 1],
                              [0, 0, 0, 0, 0, 1, 1],
                              [0, 0, 0, 0, 0, 0, 0],
                              [1, 1, 1, 0, 0, 0, 0]], 4), 15, "Example");
    assert.equal(lifeCounter([[0, 1, 0],
                              [0, 0, 1],
                              [1, 1, 1]], 50), 5, "Glider");
    assert.equal(lifeCounter([[1, 1, 0, 1, 1],
                              [1, 1, 0, 1, 1],
                              [0, 0, 0, 0, 0],
                              [1, 1, 0, 1, 1],
                              [1, 1, 0, 1, 1]], 100), 16, "Stones");
    console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}