Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My kata code #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions kata-boilerplate/src/getSucceedingGeneration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
function exist(generation, x, y)
{
return typeof generation[y] !== 'undefined' && typeof generation[y][x] !== 'undefined';
}

function isAlive(generation, x, y)
{
return generation[y][x] === '*';
}

function getNofAliveNeighbours(generation, x, y)
{
return [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]].reduce((counter, offset) => {
return counter + (exist(generation, x + offset[0], y + offset[1]) && isAlive(generation, x + offset[0], y + offset[1]) ? 1 : 0)
}, 0);
}

function getSucceedingGeneration(initialGenerationConfig, evolveByNGenerations = 1)
{
return initialGenerationConfig;
return [...Array(evolveByNGenerations)]
.reduce(
generation => { // evolving
return generation.map((row,y) => row.map((cell,x) => {
let nofAliveNeighbours = getNofAliveNeighbours(generation, x, y);
return cell === '.'
? (nofAliveNeighbours === 3 ? '*' : '.')
: (nofAliveNeighbours === 2 || nofAliveNeighbours === 3 ? '*' : '.');
}));
},
initialGenerationConfig.split(/\r?\n/)
.map(line => line.trim()) // remove irrelevant characters
.slice(1) // f*** the grid size
.map(line => line.split(''))
)
.map(line => line.join(''))
.join('\r\n');
}

module.exports = getSucceedingGeneration;
module.exports = {
exist,
isAlive,
getNofAliveNeighbours,
getSucceedingGeneration
};
6 changes: 6 additions & 0 deletions kata-boilerplate/test/examples/example02-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5 6
......
..*...
..**..
...*..
......
5 changes: 5 additions & 0 deletions kata-boilerplate/test/examples/example02-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
......
..**..
.*..*.
..**..
......
16 changes: 16 additions & 0 deletions kata-boilerplate/test/examples/example03-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
15 15
...............
...............
...............
...............
...............
.....*****.....
.....*****.....
.....*****.....
.....*****.....
.....*****.....
...............
...............
...............
...............
...............
15 changes: 15 additions & 0 deletions kata-boilerplate/test/examples/example03-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
...............
.......*.......
......*.*......
......*.*......
.......*.......
...............
..**.......**..
.*..*.....*..*.
..**.......**..
...............
.......*.......
......*.*......
......*.*......
.......*.......
...............
27 changes: 23 additions & 4 deletions kata-boilerplate/test/getSucceedingGeneration.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
const fs = require('fs');
const getSucceedingGeneration = require('../src/getSucceedingGeneration.js');
const { exist, isAlive, getNofAliveNeighbours, getSucceedingGeneration } = require('../src/getSucceedingGeneration.js');

test('if exist function check the cell existence', () => {
expect(exist([['.','.'],['.','*']], 1, 1)).toBeTruthy();
expect(exist([['.','.'],['.','*']], 2, 1)).toBeFalsy();
});

test('if isAlive function check the cell pzazz', () => {
expect(isAlive([['.','.'],['.','*']], 1, 1)).toBeTruthy();
expect(isAlive([['.','.'],['.','*']], 0, 1)).toBeFalsy();
});

test('if getNofAliveNeighbours function returns the correct number of alive neighbours', () => {
expect(getNofAliveNeighbours([['.','.'],['.','*']], 1, 1)).toBe(0);
expect(getNofAliveNeighbours([['.','.'],['.','*']], 0, 1)).toBe(1);
});

test.each([
[fs.readFileSync(__dirname + '/examples/example01-input.txt', 'utf8'), fs.readFileSync(__dirname + '/examples/example01-output.txt', 'utf8')],
])('if the second generation is valid', (initialGenerationConfig, expectedOutput) => {
expect(getSucceedingGeneration(initialGenerationConfig, 1)).toBe(expectedOutput);
[fs.readFileSync(__dirname + '/examples/example01-input.txt', 'utf8'), fs.readFileSync(__dirname + '/examples/example01-output.txt', 'utf8'), 1],
[fs.readFileSync(__dirname + '/examples/example02-input.txt', 'utf8'), fs.readFileSync(__dirname + '/examples/example02-output.txt', 'utf8'), 2],
[fs.readFileSync(__dirname + '/examples/example03-input.txt', 'utf8'), fs.readFileSync(__dirname + '/examples/example03-output.txt', 'utf8'), 11],
])('if the second generation is valid', (initialGenerationConfig, expectedOutput, evolveByNGenerations) => {
let output = getSucceedingGeneration(initialGenerationConfig, evolveByNGenerations);
console.log(output);
expect(output).toBe(expectedOutput);
});