SHARE
TWEET

Untitled

a guest Jul 5th, 2016 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     let moves = {
  3.         'u': {
  4.             row: -1,
  5.             col: 0
  6.         },
  7.         'r': {
  8.             row: 0,
  9.             col: 1
  10.         },
  11.         'd': {
  12.             row: 1,
  13.             col: 0
  14.         },
  15.         'l': {
  16.             row: 0,
  17.             col: -1
  18.         }
  19.     };
  20.  
  21.     let boardSizeArr = args[0].split(' ').map(Number);
  22.     let startingString = args[1].split(';');
  23.  
  24.     let rows = boardSizeArr[0];
  25.     let cols = boardSizeArr[1];
  26.  
  27.     let woobPos = extractStarting(startingString[0]);
  28.     let nbsPos = extractStarting(startingString[1]);
  29.     let princessLPos = extractStarting(startingString[2]);
  30.  
  31.     let layedTraps = {};
  32.     let commads = args.slice(2);
  33.  
  34.     let isWoobTrapped = false;
  35.     let isNbsTrapped = false;
  36.     let move, currCommand;
  37.     for (let i = 0; i < commads.length; i += 1) {
  38.         currCommand = extractCommand(commads[i]);
  39.  
  40.  
  41.         if (currCommand[0] === 'lay') {
  42.             layedTraps[[princessLPos.row, princessLPos.col]] = true;
  43.  
  44.             // escaped from maze or 2 trolls are caught
  45.             if ((princessLPos.col == cols - 1 && princessLPos.row == rows - 1) ||
  46.                 (isWoobTrapped && isNbsTrapped)) {
  47.  
  48.                 console.log("Lsjtujzbo is saved! " + woobPos.row + ' ' + woobPos.col + ' ' + nbsPos.row
  49.                 + ' ' + nbsPos.col);
  50.                 return;
  51.             }
  52.         } else {
  53.             move = moves[currCommand[2]];
  54.         }
  55.  
  56.         if (currCommand[1] === 'Wboup') {
  57.             debugger
  58.             if (insideField(woobPos.row + move.row, woobPos.col + move.col) && !isWoobTrapped) {
  59.                 woobPos.col += move.col;
  60.                 woobPos.row += move.row;
  61.             } else {
  62.                 continue;
  63.             }
  64.             if (layedTraps[[woobPos.row, woobPos.col]] === true) {
  65.                 if (isNbsTrapped) {
  66.                     isNbsTrapped = false;
  67.                     layedTraps[[woobPos.col, woobPos.row]] = false;
  68.                 } else {
  69.                     isWoobTrapped = true;
  70.                 }
  71.             }
  72.  
  73.  
  74.         }  else if (currCommand[1] === 'Nbslbub') {
  75.             if (insideField(nbsPos.row + move.row, nbsPos.col + move.col) && !isNbsTrapped) {
  76.                 nbsPos.col += move.col;
  77.                 nbsPos.row += move.row;
  78.             } else {
  79.                 continue;
  80.             }
  81.             if (layedTraps[[nbsPos.row, nbsPos.col]] === true) {
  82.                 if (isWoobTrapped) {
  83.                     isWoobTrapped = false;
  84.                     layedTraps[[nbsPos.col, nbsPos.row]] = false;
  85.                 } else {
  86.                     isNbsTrapped = true;
  87.                 }
  88.             }
  89.  
  90.         } else if (currCommand[1] === 'Lsjtujzbo') {
  91.             if (insideField(princessLPos.row + move.row, princessLPos.col + move.col))
  92.             princessLPos.col += move.col;
  93.             princessLPos.row += move.row;
  94.         }
  95.  
  96.         // check if queen is trapped
  97.         if (checkTrollOnOneCellWithPrincess(woobPos, nbsPos, princessLPos)) {
  98.  
  99.             console.log('The trolls caught Lsjtujzbo' + ' at ' + princessLPos.row + ' ' +princessLPos.col);
  100.             return;
  101.         }
  102.         if (checkTrollOnNeighborCell(woobPos,nbsPos,princessLPos)){
  103.  
  104.             console.log('The trolls caught Lsjtujzbo' + ' at ' + princessLPos.row + ' ' +princessLPos.col);
  105.             return;
  106.         }
  107.     }
  108.  
  109.     console.log("Lsjtujzbo is saved! " + woobPos.row + ' ' + woobPos.col + ' ' + nbsPos.row + ' ' + nbsPos.col);
  110.  
  111.  
  112.     function checkTrollOnNeighborCell(trollOne, trollTwo, princess) {
  113.         let trollOneToPrincess =
  114.             Math.abs(trollOne.row - princess.row) <= 1 &&
  115.             Math.abs(trollOne.col - princess.col) <= 1;
  116.  
  117.  
  118.         let trollTwoToPrincess =
  119.             Math.abs(trollTwo.row - princess.row) <= 1 &&
  120.             Math.abs(trollTwo.col - princess.col) <= 1;
  121.  
  122.         return trollOneToPrincess || trollTwoToPrincess;
  123.     }
  124.  
  125.     function checkTrollOnOneCellWithPrincess(trollOne, trollTwo, priness) {
  126.         return (trollOne.row === priness.row && trollOne.col === priness.col) ||
  127.             (trollTwo.row === priness.row && trollTwo.col === priness.col);
  128.  
  129.     }
  130.  
  131.     function insideField(r, c) {
  132.         return r >= 0 && r < rows && c >= 0 && c < cols;
  133.     }
  134.  
  135.     function extractCommand(commandAsStr) {
  136.         return commandAsStr.split(' ');
  137.     }
  138.  
  139.     function extractStarting(arrayOfCoords) {
  140.         arrayOfCoords = arrayOfCoords.split(' ').map(Number);
  141.         let r = arrayOfCoords[0];
  142.         let c = arrayOfCoords[1];
  143.  
  144.         return {
  145.             row: r,
  146.             col: c
  147.         };
  148.     }
  149.  
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top