Rename ubiquitous 'data' to something more meaningful according to the context
This commit is contained in:
parent
5c0ef70220
commit
ffebb1efb8
7 changed files with 32 additions and 32 deletions
|
@ -21,14 +21,14 @@ CellSet.prototype.fromRectangle = function(definition) {
|
|||
};
|
||||
|
||||
CellSet.prototype.fromIsochrome = function(definition) {
|
||||
var originColor = definition.data[definition.row][definition.column];
|
||||
var originColor = definition.grid[definition.row][definition.column];
|
||||
var queue = [{i: definition.row, j: definition.column}];
|
||||
while(queue.length > 0) {
|
||||
var p = queue[0];
|
||||
this.add(p.i, p.j);
|
||||
for(var d = -1; d < 2; d += 2) {
|
||||
[{i: p.i + d, j: p.j}, {i: p.i, j: p.j + d}].forEach(
|
||||
gateKeeper(this, definition.data, queue, originColor)
|
||||
gateKeeper(this, definition.grid, queue, originColor)
|
||||
);
|
||||
}
|
||||
queue.shift();
|
||||
|
@ -90,11 +90,11 @@ function id(i, j) {
|
|||
return i + ':' + j;
|
||||
}
|
||||
|
||||
function gateKeeper(cellSet, data, queue, originColor) {
|
||||
function gateKeeper(cellSet, grid, queue, originColor) {
|
||||
return function(p1) {
|
||||
if(p1.i >= 0 && p1.i < data.length && p1.j >= 0 && p1.j < data.length
|
||||
if(p1.i >= 0 && p1.i < grid.length && p1.j >= 0 && p1.j < grid.length
|
||||
&& !cellSet.contains(p1.i, p1.j)
|
||||
&& data[p1.i][p1.j] == originColor) {
|
||||
&& grid[p1.i][p1.j] == originColor) {
|
||||
queue.push(p1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import {iter, square} from Grid.Util;
|
|||
|
||||
var grid = {
|
||||
element: document.getElementById('grid'),
|
||||
data: null,
|
||||
colors: null,
|
||||
missing: null,
|
||||
size: null
|
||||
};
|
||||
|
@ -35,11 +35,11 @@ function makeRow(config) {
|
|||
}
|
||||
|
||||
function clear() {
|
||||
grid.data = square(grid.size);
|
||||
grid.colors = square(grid.size);
|
||||
grid.missing = CellSet.make(
|
||||
{type: 'rectangle', row: 0, column: 0, width: 8, height: 8}
|
||||
);
|
||||
iter(grid.data, function(row, column) {
|
||||
iter(grid.colors, function(row, column) {
|
||||
cell(row, column).className = '';
|
||||
});
|
||||
}
|
||||
|
|
|
@ -9,14 +9,14 @@ return {
|
|||
|
||||
function colorize(row, column, color) {
|
||||
var grid = Grid.get();
|
||||
grid.data[row][column] = color || Toolbox.color();
|
||||
Grid.cell(row, column).className = 'color' + grid.data[row][column];
|
||||
grid.colors[row][column] = color || Toolbox.color();
|
||||
Grid.cell(row, column).className = 'color' + grid.colors[row][column];
|
||||
grid.missing.remove(row, column);
|
||||
}
|
||||
|
||||
function paint(row, column) {
|
||||
var cellSet = CellSet.make(
|
||||
{type: 'isochrome', row: row, column: column, data: Grid.get().data}
|
||||
{type: 'isochrome', row: row, column: column, grid: Grid.get().colors}
|
||||
);
|
||||
cellSet.iter(colorize);
|
||||
}
|
||||
|
|
|
@ -46,5 +46,5 @@ function setGridData(data) {
|
|||
}
|
||||
|
||||
function save() {
|
||||
File.save('data:text/json,' + JSON.stringify(Grid.get().data), "grid.json");
|
||||
File.save('data:text/json,' + JSON.stringify(Grid.get().colors), "grid.json");
|
||||
}
|
||||
|
|
|
@ -8,5 +8,5 @@ return {
|
|||
};
|
||||
|
||||
function onEnter() {
|
||||
console.log(Solver.step(Grid.get().data));
|
||||
console.log(Solver.step(Grid.get().colors));
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
function Encoder() {
|
||||
this.data = '';
|
||||
this.buffer = '';
|
||||
this.stack = 0;
|
||||
this.size = 0;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ Encoder.prototype.push = function(one) {
|
|||
};
|
||||
|
||||
Encoder.prototype.flush = function() {
|
||||
this.data = this.data + String.fromCharCode(this.stack);
|
||||
this.buffer = this.buffer + String.fromCharCode(this.stack);
|
||||
this.stack = 0;
|
||||
this.size = 0;
|
||||
};
|
||||
|
@ -23,7 +23,7 @@ Encoder.prototype.output = function() {
|
|||
this.push(0);
|
||||
}
|
||||
this.flush();
|
||||
return btoa(this.data);
|
||||
return btoa(this.buffer);
|
||||
};
|
||||
|
||||
Encoder.prototype.variableLength3 = function(n) {
|
||||
|
|
32
js/Solver.js
32
js/Solver.js
|
@ -4,29 +4,29 @@ return {
|
|||
step: step
|
||||
};
|
||||
|
||||
function step(matrix) {
|
||||
var zones = getZones(matrix);
|
||||
var grid = getGrid(matrix.length);
|
||||
var rowClusters = checkRowsInclusions(matrix);
|
||||
function step(grid) {
|
||||
var zones = getZones(grid);
|
||||
var lines = getLines(grid.length);
|
||||
var rowClusters = checkRowsInclusions(grid);
|
||||
if(rowClusters.length > 0) {
|
||||
rowClusters.forEach(function(rowCluster) {
|
||||
rowCluster.toClear = difference(
|
||||
rowCluster.colors.map(function(color) {return zones[color];}),
|
||||
rowCluster.rows.map(function(row) {return grid.rows[row];})
|
||||
rowCluster.rows.map(function(row) {return lines.rows[row];})
|
||||
);
|
||||
});
|
||||
}
|
||||
return rowClusters;
|
||||
}
|
||||
|
||||
function getZones(matrix) {
|
||||
function getZones(grid) {
|
||||
var zones = {};
|
||||
for(var row = 0; row < matrix.length; row++) {
|
||||
for(var column = 0; column < matrix[row].length; column++) {
|
||||
var color = matrix[row][column];
|
||||
for(var row = 0; row < grid.length; row++) {
|
||||
for(var column = 0; column < grid[row].length; column++) {
|
||||
var color = grid[row][column];
|
||||
if(zones[color] == undefined) {
|
||||
zones[color] = CellSet.make(
|
||||
{type: 'isochrome', row: row, column: column, data: matrix}
|
||||
{type: 'isochrome', row: row, column: column, grid: grid}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ function line(type, size, i) {
|
|||
}
|
||||
}
|
||||
|
||||
function getGrid(size) {
|
||||
function getLines(size) {
|
||||
var empty = Array.from({length: size});
|
||||
return {
|
||||
rows: empty.map(function(x, i) {return line('row', size, i);}),
|
||||
|
@ -54,11 +54,11 @@ function getGrid(size) {
|
|||
};
|
||||
}
|
||||
|
||||
function getColorsByRow(matrix) {
|
||||
function getColorsByRow(grid) {
|
||||
var colorsByRow = [];
|
||||
for(var row = 0; row < matrix.length; row++) {
|
||||
for(var row = 0; row < grid.length; row++) {
|
||||
colorsByRow.push(
|
||||
quotient(matrix[row], function(c0, c1) {return c0 == c1;}).map(
|
||||
quotient(grid[row], function(c0, c1) {return c0 == c1;}).map(
|
||||
function(colorClass) {return colorClass.specimen;}
|
||||
)
|
||||
);
|
||||
|
@ -66,8 +66,8 @@ function getColorsByRow(matrix) {
|
|||
return colorsByRow;
|
||||
}
|
||||
|
||||
function checkRowsInclusions(matrix) {
|
||||
var colorsByRow = getColorsByRow(matrix);
|
||||
function checkRowsInclusions(grid) {
|
||||
var colorsByRow = getColorsByRow(grid);
|
||||
var colorSets = quotient(colorsByRow, sameColorsSet);
|
||||
return colorSets.reduce(function(commands, colorSet) {
|
||||
if(colorSet.occurrences.length == colorSet.specimen.length) {
|
||||
|
|
Loading…
Reference in a new issue