Advertisement
dimipan80

Exams - Parachute

Jan 11th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given an array of strings. The jumper can be anywhere in the array and is denoted by the 'o' symbol.
  2.  * You need to determine the movement of the jumper in iterations. On each iteration the jumper moves
  3.  * one line down, pulled by gravity. Additionally, the jumper moves sideways by the wind on the current line.
  4.  * The '>' symbol means the wind is moving the jumper one position to the right.
  5.  * The '<' symbol means the wind is moving the jumper one position to the left.
  6.  * The total direction of the wind on a single line may stack (e.g. '>>>' moves the jumper 3 positions to
  7.  * the right; '><<' moves the jumper 1 position to the left).
  8.  * The jumper will never fly off the map!
  9.  * The jumper can move only through 'air' (the '-' symbol). When the jumper hits the ground, water or a cliff,
  10.  * the jump is finished!
  11.  * Your task is to determine how the jumper will finish his jump and where he will land exactly, based on
  12.  * the gravity and wind parameters!
  13.  * The symbols in input strings will not separated by anything!
  14.  * The output should consist of two lines. On the first line you must display 1 of 3 possible messages:
  15.  * If you have landed on the ground ('_' symbol), the message is: "Landed on the ground like a boss!";
  16.  *If you have landed in the water ('~' symbol), the message is: "Drowned in the water like a cat!";
  17.  *If you have landed on a cliff ('/', '\' or '|' symbol), the message is: "Got smacked on the rock like a dog!".
  18.  *The second line holds the position (the row and col, space-separated) of your landing. */
  19.  
  20. "use strict";
  21.  
  22. function solve(args) {
  23.     var startRow = 0;
  24.     var col = 0;
  25.     for (var row = 0; row < args.length; row += 1) {
  26.         if (args[row].indexOf('o') > -1) {
  27.             startRow = row;
  28.             col = args[row].indexOf('o');
  29.         }
  30.     }
  31.    
  32.     for (row = startRow + 1; row < args.length; row += 1) {
  33.         var rightMoves = 0;
  34.         var leftMoves = 0;
  35.         for (var j = 0; j < args[row].length; j += 1) {
  36.             if (args[row][j] == '>') {
  37.                 rightMoves += 1;
  38.             } else if (args[row][j] == '<') {
  39.                 leftMoves += 1;
  40.             }
  41.         }
  42.        
  43.         col += (rightMoves - leftMoves);
  44.         switch (args[row][col]) {
  45.             case '/':
  46.             case '\\':
  47.             case '|':
  48.                 console.log("Got smacked on the rock like a dog!\n%d %d", row, col);
  49.                 return;
  50.             case '~':
  51.                 console.log("Drowned in the water like a cat!\n%d %d", row, col);
  52.                 return;
  53.             case '_':
  54.                 console.log("Landed on the ground like a boss!\n%d %d", row, col);
  55.                 return;
  56.         }
  57.     }
  58. }
  59.  
  60. solve(['--o----------------------',
  61.     '>------------------------',
  62.     '>------------------------',
  63.     '>-----------------/\\-----',
  64.     '-----------------/--\\----',
  65.     '>---------/\\----/----\\---',
  66.     '---------/--\\--/------\\--',
  67.     '<-------/----\\/--------\\-',
  68.     '\\------/----------------\\',
  69.     '-\\____/------------------']);
  70.  
  71. solve(['-------------o-<<--------',
  72.     '-------->>>>>------------',
  73.     '---------------->-<---<--',
  74.     '------<<<<<-------/\\--<--',
  75.     '--------------<--/-<\\----',
  76.     '>>--------/\\----/<-<-\\---',
  77.     '---------/<-\\--/------\\--',
  78.     '<-------/----\\/--------\\-',
  79.     '\\------/--------------<-\\',
  80.     '-\\___~/------<-----------']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement