lot/libs/lot.js

295 lines
6.8 KiB
JavaScript
Raw Permalink Normal View History

2017-09-08 09:45:53 +00:00
#!/usr/bin/env node
const m = [],
prev = [],
mo = [],
star = [],
trip = [],
results = [];
const line = [];
let re = [];
const resultsObj = {};
function sortNumber(a, b) {
return a - b;
}
function dynamicSort(property) {
let sortOrder = 1;
if (property[0] === '-') {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
const result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
};
}
function buildArray() {
for (let x = 0; x < 51; x++) {
const l = [];
for (let y = 0; y < 51; y++)
l.push(0);
m[x] = l;
}
}
function othercalc() {
let i = 0;
// var p = 1 / ((-1)*re.length);
const p = 1.0 / (re.length);
// console.log('P', p);
for (i = 0; i < re.length - 1; i++)
// console.log((p * i));
// console.log(re[i]);
for (let s = 1; s < 6; s++) {
const cv = re[i][s];
// console.log("row: " + cv);
for (let n = 1; n < 6; n++)
if (n !== s) {
workVal = re[i][n];
// console.log(workVal);
// m[cv-1][workVal-1]++;
let flag = false;
for (var t = 0; t < mo.length; t++)
if (mo[t].a === cv && mo[t].b === workVal) {
if (typeof mo[t].c === 'undefined' || mo[t].c === null)
mo[t].c = 0;
mo[t].c++;
flag = true;
// mo[t].v++;
/*
if (i < (re.length/2))
mo[t].v = mo[t].v - (p * i)
else
*/
// original weighting
//mo[t].v = mo[t].v + (p * i);
//reverse weighting..
mo[t].v = mo[t].v + (1 - (p * i));
// console.log(mo[t], 1 - (p * i));
//;
//mo[t].v = mo[t].v + 1;
// ;
}
if (!flag)
mo.push({
'a': cv,
'b': workVal,
'v': 1
});
}
}
console.log('******************************************');
mo.sort(dynamicSort('v'));
//for (var t = (mo.length - 20); t < mo.length; t++) {
var t = mo.length - 1;
while (line.length < 5) {
if (line.indexOf(mo[t].a) == -1 && line.length < 5)
line.push(mo[t].a);
if (line.indexOf(mo[t].b) == -1 && line.length < 5)
line.push(mo[t].b);
t = t - 1;
}
// console.log(line);
console.log(line.sort());
resultsObj.mainline = line.sort();
resultsObj.otherLine = [];
resultsObj.otherLine.push(mo[mo.length - 1].a);
resultsObj.otherLine.push(mo[mo.length - 1].b);
for (var t = mo.length - 1; t > (mo.length - 3); t--)
// if (mo[t].v >= 3) {
if (mo[t].v > 1)
console.log(`${mo[t].a }, ${ mo[t].b }, ${ mo[t].v}`);
// buildTable();
}
function tripCalc() {
const blist = [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]];
const p = 1.0 / (re.length);
let a, b, c, cur;
//var trip = [];
//console.log
for (i = 0; i < re.length - 1; i++)
for (let t = 0; t < blist.length; t++) {
cur = blist[t];
a = re[i][cur[0] + 1];
b = re[i][cur[1] + 1];
c = re[i][cur[2] + 1];
// console.log(a + ", " + b + "," + c);
let found = false;
for (y = 0; y < trip.length; y++) {
const nums = trip[y].nums;
if (!found)
if ((nums.indexOf(a) !== -1) && (nums.indexOf(b) !== -1) && (nums.indexOf(c) !== -1)) {
/* console.log("nums:" + nums);
console.log("want: " + a + ", " + b + "," + c);
console.log("A:" + nums.indexOf(a));
console.log("B:" + nums.indexOf(b));
console.log("C:" + nums.indexOf(c));
*/
trip[y].cnt = trip[y].cnt + (p * i);
// trip[y].cnt = trip[y].cnt + 1;
// console.log(">>>" + trip[y].cnt);
found = true;
}
}
if (!found)
//trip.push({nums:[a,b,c],cnt:(p * i)});
trip.push({
'nums': [a, b, c],
'cnt': 1
});
}
let max = 0;
for (y = 0; y < trip.length; y++)
if (trip[y].cnt > max) max = trip[y].cnt;
//console.log(`Max: ${ max}`);
for (y = 0; y < trip.length; y++)
if (trip[y].cnt === max) {
// console.log(`${trip[y].cnt }, ${ trip[y].nums}`);
resultsObj.otherLine = resultsObj.otherLine.concat(trip[y].nums).sort();
}
// console.log(JSON.stringify(trip));
}
function starcalc() {
let i = 0;
const p = 1 / re.length;
for (i = 0; i < re.length - 1; i++)
// console.log(re[i]);
for (let s = 6; s < 8; s++) {
const cv = re[i][s];
// console.log("row: " + cv);
for (let n = 6; n < 8; n++)
if (n !== s) {
workVal = re[i][n];
// console.log(workVal);
// m[cv-1][workVal-1]++;
let flag = false;
for (var t = 0; t < mo.length; t++)
if (mo[t].a === cv && mo[t].b === workVal) {
flag = true;
mo[t].v++;
mo[t].v = mo[t].v + (p * i);
}
if (!flag)
mo.push({
'a': cv,
'b': workVal,
'v': 1
});
}
}
// console.log('******************************************');
mo.sort(dynamicSort('v'));
var t = mo.length - 1;
const starOutput = [];
starOutput.push(mo[t].a);
starOutput.push(mo[t].b);
resultsObj.mainstars = [];
resultsObj.mainstars.push(starOutput.sort()[0]);
resultsObj.mainstars.push(starOutput.sort()[1]);
resultsObj.otherStars = [];
resultsObj.otherStars.push(mo[mo.length - 3].a);
resultsObj.otherStars.push(mo[mo.length - 3].b);
//console.log('Stars: ' + mo[t].a + ', ' + mo[t].b);
/* console.log(`Stars: ${ starOutput.sort()[0] }, ${ starOutput.sort()[1]}`);
for (var t = mo.length - 6; t < mo.length; t++)
if (mo[t].v >= 3)
console.log(`${mo[t].a }, ${ mo[t].b }, ${ mo[t].v}`);
*/
// buildTable();
}
function performCalcs() {
buildArray();
//buildTable();
othercalc();
console.log('---------------------------------------');
starcalc();
tripCalc();
}
function prepareResults() {
resultsObj.mainline = resultsObj.mainline.sort(sortNumber);
resultsObj.otherLine = resultsObj.otherLine.sort(sortNumber);
resultsObj.mainstars = resultsObj.mainstars.sort(sortNumber);
resultsObj.otherStars = resultsObj.otherStars.sort(sortNumber);
return resultsObj;
}
function calculate(data) {
re = data;
performCalcs();
prepareResults();
console.log(resultsObj);
return resultsObj;
}
module.exports.calculate = calculate;