Advertisement
Guest User

Fuzz

a guest
Feb 8th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     let solution_1 = function(seedBn, rows, cols, colorsNum) {
  3.         const SEED = BigInt(seedBn);
  4.         const ROWS = rows;
  5.         const COLS = cols;
  6.         const COLORS_NUM = colorsNum;
  7.      
  8.         let LCGRand = function() {
  9.             this.next = SEED;
  10.             this.A = BigInt('6364136223846793005');
  11.             this.C = BigInt('1442695040888963407');
  12.             this.M = BigInt('0xFFFFFFFFFFFFFFFF');
  13.             this.SHIFT = BigInt('32');
  14.             return function() {
  15.                 this.next = (((this.next * this.A) & this.M) + this.C) & this.M;
  16.                 return this.next >> this.SHIFT;
  17.             };
  18.         }();
  19.      
  20.         function matrixAt(matrix, i, j) {
  21.             return matrix[i * COLS + j];
  22.         }
  23.            
  24.         function generateMatrix() {
  25.             let matrix = [];
  26.             for (let i = 0; i < ROWS * COLS; i++) {
  27.                 matrix.push({
  28.                     color: Number(LCGRand()) % COLORS_NUM,
  29.                     visited: false
  30.                 });
  31.             }
  32.             return matrix;
  33.         }
  34.      
  35.         function isValidCoords(i, j) {
  36.             return (i >= 0) && (i < ROWS) && (j >= 0) && (j < COLS);
  37.         }
  38.      
  39.         function visitNode(matrix, i, j) {
  40.             const MOVES = [
  41.                 [ -1, 0 ],
  42.                 [ +1, 0 ],
  43.                 [ 0, -1 ],
  44.                 [ 0, +1 ],
  45.             ];
  46.            
  47.             let vec = [[i, j]];
  48.             let blockSize = 0;
  49.             let currentNode = matrixAt(matrix, i, j);
  50.             const targetColor = currentNode.color;
  51.             currentNode.visited = true;
  52.             while (vec.length > 0) {
  53.                 [i, j] = vec.pop();
  54.                 blockSize++;
  55.                
  56.                 for (let move of MOVES) {
  57.                     let movedI = i + move[0], movedJ = j + move[1];
  58.                     if (isValidCoords(movedI, movedJ)) {
  59.                         let node = matrixAt(matrix, movedI, movedJ);
  60.                         if (node.color === targetColor && !node.visited) {
  61.                             node.visited = true;
  62.                             vec.push([movedI, movedJ]);
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.             return blockSize;
  68.         }
  69.      
  70.         function main() {
  71.             let matrix = generateMatrix();
  72.             let maxBlockSize = 0;
  73.            
  74.             let startTime = performance.now();
  75.             for (let i = 0; i < ROWS; i++) {
  76.                 for (let j = 0; j < COLS; j++) {
  77.                     if (!matrixAt(matrix, i, j).visited) {
  78.                         const blockSize = visitNode(matrix, i, j);
  79.                         if (maxBlockSize < blockSize) {
  80.                             maxBlockSize = blockSize;
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.            
  86.             return maxBlockSize;
  87.         }
  88.        
  89.         return main();
  90.     };
  91.    
  92.     let solution_2 = function(seedBn, rows, cols, colorsNum) {
  93.         const SEED = BigInt(seedBn);
  94.         const ROWS = rows;
  95.         const COLS = cols;
  96.         const COLORS_NUM = colorsNum;
  97.      
  98.         let LCGRand = function() {
  99.             this.next = SEED;
  100.             this.A = BigInt('6364136223846793005');
  101.             this.C = BigInt('1442695040888963407');
  102.             this.M = BigInt('0xFFFFFFFFFFFFFFFF');
  103.             this.SHIFT = BigInt('32');
  104.             return function() {
  105.                 this.next = (((this.next * this.A) & this.M) + this.C) & this.M;
  106.                 return this.next >> this.SHIFT;
  107.             };
  108.         }();
  109.      
  110.         function generateMatrix(size) {
  111.         let matrix = [];
  112.         for (let i = 0; i < size; i++) {
  113.             matrix.push({
  114.                 color: Number(LCGRand()) % COLORS_NUM
  115.                 ,area: {
  116.                     pos: i
  117.                 }
  118.             });
  119.         }
  120.         return matrix;
  121.     }
  122.  
  123.     function matrixAt(matrix, i, j) {
  124.         return matrix[i * COLS + j];
  125.     }
  126.        
  127.     function main()
  128.     {
  129.         var len  = ROWS * COLS;
  130.         let matrix  = generateMatrix(len);
  131.         let startTime = performance.now();
  132.        
  133.         for (var i = 0; i < ROWS; i++) {
  134.             var prev=null;
  135.             for (var j = 0; j < COLS; j++)
  136.             {
  137.                 var curr = matrixAt(matrix, i, j);
  138.                 if (prev && curr.color == prev.color){
  139.                     curr.area = prev.area;
  140.                 }
  141.                 if (i>0){
  142.                     var top = matrixAt(matrix, i-1, j);
  143.                     if (top.color == curr.color){
  144.                         if (top.area.pos!=curr.area.pos)
  145.                             merge(top, curr);
  146.                     }
  147.                 }
  148.                 prev=curr;
  149.             }
  150.         }
  151.  
  152.         var area = new Array(len).fill(0);
  153.         var maxArea = 0;
  154.         for (var i = 0; i < len; i++) {
  155.             var pos=matrix[i].area.pos;
  156.             area[pos]+=1;
  157.             if (area[pos] > maxArea) {
  158.                 maxArea = area[pos];
  159.             }
  160.         }
  161.         return maxArea;
  162.     }
  163.     function merge(a,b)
  164.     {
  165. //     a.area.size += b.area.size;
  166. //     b.area.size =  a.area.size;
  167.         a.area.pos  =  Math.min(a.area.pos, b.area.pos)|0 ;
  168.         b.area.pos  =  a.area.pos;
  169.     }
  170.  
  171.        
  172.         return main();
  173.     };
  174.    
  175.     const BASE_SEED = BigInt('0xDEADBEEF');
  176.     for (let rows = 1; rows < 50; rows++) {
  177.         for (let cols = 1; cols < 100; cols++) {
  178.             for (let seedOffset = BigInt(-5); seedOffset < BigInt(5); seedOffset++) {
  179.                 const seed = BASE_SEED + seedOffset;
  180.                 const result_1 = solution_1(seed, rows, cols);
  181.                 const result_2 = solution_2(seed, rows, cols);
  182.                 if (result_1 != result_2) {
  183.                     console.log('ERROR FOR SEED = ' + seed.toString()
  184.                         + ', ROWS = ' + rows.toString()
  185.                         + ', COLS = ' + cols.toString()
  186.                         + ' (' + result_1.toString() + ' != ' + result_2.toString() + ')');
  187.                     return;
  188.                 }
  189.             }
  190.         }
  191.     }
  192.     console.log('OK');
  193. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement