Skip to content

Instantly share code, notes, and snippets.

@bobixaka
Created September 8, 2013 21:22
Show Gist options
  • Save bobixaka/6488518 to your computer and use it in GitHub Desktop.
Save bobixaka/6488518 to your computer and use it in GitHub Desktop.
You are given a rectangular cuboid of size W (width), H (height) and D (depth) consisting of W * H * D cubes. The top row (layer) of the cube is considered to be with height 0. Each cube can contain one of the following things – slide, teleport, basket or just be an empty cube. A small ball that can fit through a single cube is dropped over the …
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Slides
{
class Slides
{
static void Main()
{
string CubeSize = Console.ReadLine();
string[] Dimensions = CubeSize.Split(' ');
int W = int.Parse(Dimensions[0]);
int H = int.Parse(Dimensions[1]);
int D = int.Parse(Dimensions[2]);
string[,,] Cube = new string[W,H,D];
for (int h = 0; h < H; h++)
{
string InputLine = Console.ReadLine();
InputLine = InputLine.Replace(" | ", "|");
InputLine = InputLine.Replace(")(", ",");
InputLine = InputLine.Replace("(", "");
InputLine = InputLine.Replace(")", "");
string[] DepthLayers = InputLine.Split('|');
for (int d = 0; d < DepthLayers.Length; d++)
{
string[] Boxes = DepthLayers[d].Split(',');
for (int w = 0; w < Boxes.Length; w++)
{
Cube[w, h, d] = Boxes[w];
}
}
}
string BallPosition = Console.ReadLine();
string[] Coordinates = BallPosition.Split(' ');
int ballW = int.Parse(Coordinates[0]);
int ballD = int.Parse(Coordinates[1]);
int ballH = 0;
//Ball position is always in this format (ballW, ballH, ballD)
bool InTheCube = true;
bool BallHasEscaped = false;
int LastBallW = ballW;
int LastBallH = ballH;
int LastBallD = ballD;
while (InTheCube)
{
if (ballW >= 0 && ballW < W && ballH >= 0 && ballH < H && ballD >= 0 && ballD < D)
{
LastBallW = ballW;
LastBallH = ballH;
LastBallD = ballD;
switch (Cube[ballW, ballH, ballD])
{
case "S L": ballW--; ballH++;
break;
case "S R": ballW++; ballH++;
break;
case "S F": ballD--; ballH++;
break;
case "S B": ballD++; ballH++;
break;
case "S FL": ballD--; ballW--; ballH++;
break;
case "S FR": ballD--; ballW++; ballH++;
break;
case "S BL": ballD++; ballW--; ballH++;
break;
case "S BR": ballD++; ballW++; ballH++;
break;
case "E": ballH++;
break;
case "B": InTheCube = false; BallHasEscaped = false;
break;
default:
if (Cube[ballW, ballH, ballD].IndexOf("T") != -1)
{
string[] Teleport = Cube[ballW, ballH, ballD].Split(' ');
ballW = int.Parse(Teleport[1]);
ballD = int.Parse(Teleport[2]);
}
else
{
Console.WriteLine("ERROR! Box: {0}", Cube[ballW, ballH, ballD]);
}
break;
}
}
//In case the ball has escaped from the bottom
else if (ballW >= 0 && ballW < W && ballH >= H && ballD >= 0 && ballD < D)
{
BallHasEscaped = true;
InTheCube = false;
}
//In case the ball is on the final floor and escaped from the walls
else if (ballH >= H-1 && (ballW < 0 || ballW >= W || ballD < 0 || ballD >= D))
{
BallHasEscaped = true;
InTheCube = false;
}
else
{
InTheCube = false;
}
}
if (BallHasEscaped)
{
Console.WriteLine("Yes");
Console.WriteLine("{0} {1} {2}", LastBallW, LastBallH, LastBallD);
}
else
{
Console.WriteLine("No");
Console.WriteLine("{0} {1} {2}", LastBallW, LastBallH, LastBallD);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment