Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- 'use strict';
- function readRow(row) {
- return row.split('|').map(function (stupidStr) {
- return stupidStr.split('(').map(function (str) {
- return str.replace(')', '').trim();
- }).filter(function (str) {
- return str.trim(' ');
- });
- });
- }
- var deltas = {
- 'L': { x: -1, y: 0 },
- 'R': { x: 1, y: 0 },
- 'F': { x: 0, y: -1 },
- 'B': { x: 0, y: 1 }
- };
- var sizes = args[0].split(' ').map(Number),
- W = sizes[0],
- H = sizes[1],
- D = sizes[2];
- function isInside(row, col) {
- return (0 <= row) && (row < D) && (0 <= col) && (col < W);
- }
- var hate = 1;
- var currentLevel = readRow(args[hate]);
- var coords = args[args.length - 1].split(' ').map(Number);
- var ball = {
- x: coords[0],
- y: coords[1],
- z: H
- };
- //console.log(ball);
- while (true) {
- //console.log(currentLevel);
- if (currentLevel[ball.y][ball.x][0] === 'E') {
- //console.log('falling');
- ball.z -= 1;
- if (ball.z < 1) {
- console.log('Yes');
- return console.log(ball.x, H - 1 - ball.z, ball.y);
- }
- } else if (currentLevel[ball.y][ball.x][0] === 'B') {
- console.log('No');
- return console.log(ball.x, H - ball.z, ball.y);
- } else if (currentLevel[ball.y][ball.x][0] === 'T') {
- //console.log('tewepowting');
- var location = currentLevel[ball.y][ball.x].split(' ').map(Number);
- ball.x = location[1];
- ball.y = location[2];
- //console.log(ball);
- continue;
- } else if (currentLevel[ball.y][ball.x][0] === 'S') {
- var dir = currentLevel[ball.y][ball.x].split(' ')[1];
- //console.log('going ' + currentLevel[ball.y][ball.x]);
- //console.log(ball);
- ball.z -= 1;
- if (ball.z === 0) {
- console.log('Yes');
- return console.log(ball.x, H - 1, ball.y);
- }
- ball.x += deltas[dir[0]].x;
- ball.y += deltas[dir[0]].y;
- if (dir[1]) {
- ball.x += deltas[dir[1]].x;
- ball.y += deltas[dir[1]].y;
- }
- if (!isInside(ball.y, ball.x)) {
- ball.x -= deltas[dir[0]].x;
- ball.y -= deltas[dir[0]].y;
- if (dir[1]) {
- ball.x -= deltas[dir[1]].x;
- ball.y -= deltas[dir[1]].y;
- }
- console.log('No');
- return console.log(ball.x, H - 1 - ball.z, ball.y);
- }
- } else {
- throw new Error('Your program sucks');
- }
- hate += 1;
- currentLevel = readRow(args[hate]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement