Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     'use strict';
  3.  
  4.     function readRow(row) {
  5.         return row.split('|').map(function (stupidStr) {
  6.             return stupidStr.split('(').map(function (str) {
  7.                 return str.replace(')', '').trim();
  8.             }).filter(function (str) {
  9.                 return str.trim(' ');
  10.             });
  11.         });
  12.     }
  13.  
  14.     var deltas = {
  15.         'L': { x: -1, y: 0 },
  16.         'R': { x: 1, y: 0 },
  17.         'F': { x: 0, y: -1 },
  18.         'B': { x: 0, y: 1 }
  19.     };
  20.  
  21.     var sizes = args[0].split(' ').map(Number),
  22.         W = sizes[0],
  23.         H = sizes[1],
  24.         D = sizes[2];
  25.  
  26.     function isInside(row, col) {
  27.         return (0 <= row) && (row < D) && (0 <= col) && (col < W);
  28.     }
  29.  
  30.     var hate = 1;
  31.     var currentLevel = readRow(args[hate]);
  32.  
  33.  
  34.     var coords = args[args.length - 1].split(' ').map(Number);
  35.     var ball = {
  36.         x: coords[0],
  37.         y: coords[1],
  38.         z: H
  39.     };
  40.  
  41.     //console.log(ball);
  42.     while (true) {
  43.  
  44.         //console.log(currentLevel);
  45.         if (currentLevel[ball.y][ball.x][0] === 'E') {
  46.             //console.log('falling');
  47.             ball.z -= 1;
  48.             if (ball.z < 1) {
  49.                 console.log('Yes');
  50.                 return console.log(ball.x, H - 1 - ball.z, ball.y);
  51.             }
  52.         } else if (currentLevel[ball.y][ball.x][0] === 'B') {
  53.             console.log('No');
  54.             return console.log(ball.x, H - ball.z, ball.y);
  55.         } else if (currentLevel[ball.y][ball.x][0] === 'T') {
  56.             //console.log('tewepowting');
  57.             var location = currentLevel[ball.y][ball.x].split(' ').map(Number);
  58.  
  59.             ball.x = location[1];
  60.             ball.y = location[2];
  61.             //console.log(ball);
  62.             continue;
  63.         } else if (currentLevel[ball.y][ball.x][0] === 'S') {
  64.             var dir = currentLevel[ball.y][ball.x].split(' ')[1];
  65.             //console.log('going ' + currentLevel[ball.y][ball.x]);
  66.             //console.log(ball);
  67.             ball.z -= 1;
  68.  
  69.             if (ball.z === 0) {
  70.                 console.log('Yes');
  71.                 return console.log(ball.x, H - 1, ball.y);
  72.             }
  73.  
  74.             ball.x += deltas[dir[0]].x;
  75.             ball.y += deltas[dir[0]].y;
  76.  
  77.             if (dir[1]) {
  78.                 ball.x += deltas[dir[1]].x;
  79.                 ball.y += deltas[dir[1]].y;
  80.             }
  81.  
  82.             if (!isInside(ball.y, ball.x)) {
  83.  
  84.                 ball.x -= deltas[dir[0]].x;
  85.                 ball.y -= deltas[dir[0]].y;
  86.  
  87.                 if (dir[1]) {
  88.                     ball.x -= deltas[dir[1]].x;
  89.                     ball.y -= deltas[dir[1]].y;
  90.                 }
  91.  
  92.                 console.log('No');
  93.                 return console.log(ball.x, H - 1 - ball.z, ball.y);
  94.             }
  95.  
  96.  
  97.  
  98.         } else {
  99.             throw new Error('Your program sucks');
  100.         }
  101.         hate += 1;
  102.         currentLevel = readRow(args[hate]);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement