Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const NOT_OPEN = -1;
  2. const MINE_FOUND = -2;
  3. const NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8];
  4.  
  5. const dataSets = [
  6.     {
  7.         input: [
  8.             [-1, 1, 1, -1],
  9.             [ 1, 1, 1,  1],
  10.             [ 1, 1, 1,  1],
  11.             [-1, 1, 1, -1],
  12.         ],
  13.         expected: [
  14.             [-2, 1, 1, -2],
  15.             [ 1, 1, 1,  1],
  16.             [ 1, 1, 1,  1],
  17.             [-2, 1, 1, -2],
  18.         ],
  19.     },
  20.     {
  21.         input: [
  22.             [-1, -1, -1, -1, -1, -1],
  23.             [-1,  2,  2,  2,  2, -1],
  24.             [-1,  2,  0,  0,  2, -1],
  25.             [-1,  2,  0,  0,  2, -1],
  26.             [-1,  2,  2,  2,  2, -1],
  27.             [-1, -1, -1, -1, -1, -1],
  28.         ],
  29.         expected: [
  30.             [ 0, 1, -2, -2, 1,  0],
  31.             [ 1, 2,  2,  2, 2,  1],
  32.             [-2, 2,  0,  0, 2, -2],
  33.             [-2, 2,  0,  0, 2, -2],
  34.             [ 1, 2,  2,  2, 2,  1],
  35.             [ 0, 1, -2, -2, 1,  0],
  36.         ],
  37.     },
  38. ];
  39.  
  40. function cloneData(data) {
  41.     return data.map(row => {
  42.         return row.slice();
  43.     });
  44. }
  45.  
  46. function solve(data) {
  47.     const height = data.length;
  48.     const width = data[0].length;
  49.  
  50.     for (let i = 0; i < height; i++) {
  51.         for (let j = 0; j < width; j++) {
  52.             assertMine(data, i, j);
  53.             markCells(data, i, j);
  54.         }
  55.     }
  56. }
  57.  
  58. function assertMine(data, i, j) {
  59.     const field = data[i][j];
  60.     if (field !== NOT_OPEN) return;
  61.  
  62.     const newData = cloneData(data);
  63.     newData[i][j] = MINE_FOUND;
  64.  
  65.     if (!validateData(newData)) {
  66.         data[i][j] = 0;
  67.         const aroundCells = getAdjacent(data, i, j, NUMBERS);
  68.         for (const cell of aroundCells) {
  69.             markCells(data, cell[0], cell[1]);
  70.         }
  71.     }
  72. }
  73.  
  74. function markCells(data, i, j) {
  75.     const minesCount = data[i][j];
  76.     if (minesCount <= 0) return;
  77.  
  78.     const mineFound = getAdjacent(data, i, j, [MINE_FOUND]);
  79.     const notOpen = getAdjacent(data, i, j, [NOT_OPEN]);
  80.     const allMinesFound = mineFound.length === minesCount;
  81.     const allMinesCanBeFound = (notOpen.length + mineFound.length) === minesCount;
  82.  
  83.     if (minesCount > mineFound.length + notOpen.length) {
  84.         throw new Error('Invalid');
  85.     }
  86.  
  87.     if (allMinesFound || allMinesCanBeFound) {
  88.         const marked = [];
  89.         if (allMinesFound) {
  90.             for (const cell of notOpen) {
  91.                 data[cell[0]][cell[1]] = 0;
  92.                 marked.push(cell);
  93.             }
  94.         }
  95.  
  96.         if (allMinesCanBeFound) {
  97.             for (const cell of notOpen) {
  98.                 data[cell[0]][cell[1]] = MINE_FOUND;
  99.                 marked.push(cell);
  100.             }
  101.         }
  102.  
  103.         if (marked.length) {
  104.             const foundAroundCells = {};
  105.             for (const cell of marked) {
  106.                 const aroundCells = getAdjacent(data, cell[0], cell[1], NUMBERS);
  107.                 for (const aroundCell of aroundCells) {
  108.                     foundAroundCells[cellKey(aroundCell)] = aroundCell;
  109.                 }
  110.             }
  111.  
  112.             for (const aroundCell of Object.values(foundAroundCells)) {
  113.                 markCells(data, aroundCell[0], aroundCell[1]);
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119. function validateData(data) {
  120.     const height = data.length;
  121.     const width = data[0].length;
  122.  
  123.     for (let i = 0; i < height; i++) {
  124.         for (let j = 0; j < width; j++) {
  125.             try {
  126.                 markCells(data, i, j);
  127.             } catch (e) {
  128.                 return false;
  129.             }
  130.         }
  131.     }
  132.  
  133.     return true;
  134. }
  135.  
  136. function cellKey(cell) {
  137.     return cell[0] + '_' + cell[1];
  138. }
  139.  
  140. function getAdjacent(data, i, j, values) {
  141.     let cells = [];
  142.     for (let x = -1; x <= 1; x++) {
  143.         const i0 = i + x;
  144.         if (!data[i0]) continue;
  145.         for (let y = -1; y <= 1; y++) {
  146.             const j0 = j + y;
  147.             if (!data[i0][j0] || (j0 === j && i0 === i)) continue;
  148.  
  149.             const field = data[i0][j0];
  150.             if (values.indexOf(field) > -1) {
  151.                 cells.push([i0, j0]);
  152.             }
  153.         }
  154.     }
  155.  
  156.     return cells;
  157. }
  158.  
  159. describe("running jasmine in jsfiddle", function(){
  160.     it("should run tests", function() {
  161.         for (const dataset of dataSets) {
  162.             const data = dataset.input;
  163.             solve(data);
  164.             expect(data).toEqual(dataset.expected);
  165.         }
  166.     });
  167. });
  168.  
  169.  
  170. (function() {
  171.     var env = jasmine.getEnv();
  172.     console.log(jasmine);
  173.     env.addReporter(new jasmine.HtmlReporter());
  174.     env.execute();
  175. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement