Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Point
- {
- public Point(int x, int y) { X = x; Y = y; }
- public int X, Y;
- }
- class OrbitException : Exception { }
- class CrushException : OrbitException { }
- List<Point> planets = new List<Point>();
- List<string> history = new List<string>();
- Point rocket, velocity, universeSize;
- public void Main()
- {
- var i = 0;
- for (var line = Console.ReadLine(); line != ""; line = Console.ReadLine())
- {
- universeSize.X = line.Select((c, j) => CreateObject(c, i, j)).Count();
- i++;
- }
- universeSize.Y = i;
- var x = new Action<string>(Console.WriteLine);
- try
- {
- while (true)
- {
- ApplyGravity();
- Move();
- }
- }
- catch (CrushException) { x("crush"); }
- catch (OrbitException) { x("orbit"); }
- catch { x("escape"); }
- }
- Point CreateObject(char c, int i, int j)
- {
- if (c == '@')
- planets.Add(new Point(j, i));
- else if (c != '.')
- {
- rocket = new Point(j, i);
- velocity = (c == 'v' ? new Point(0, 1) : c == '<' ? new Point(-1, 0) : c == '^' ? new Point(0, -1) : new Point(1, 0));
- }
- return universeSize;
- }
- private void ApplyGravity()
- {
- planets.ForEach(planet =>
- {
- var dx = planet.X - rocket.X;
- var dy = planet.Y - rocket.Y;
- if (dx < 2 && dx > -2 && dy < 2 && dy > -2)
- {
- velocity.X += dx; velocity.Y += dy;
- }
- });
- }
- private void Move()
- {
- // check if orbit; save current context
- var context = string.Join(".", rocket.X, rocket.Y, velocity.X, velocity.Y);
- if (history.Contains(context))
- throw new OrbitException();
- history.Add(context);
- // colision check
- var position = new Point(rocket.X, rocket.Y);
- var k = new[] { velocity.X, velocity.Y };
- var min = k.Min();
- var max = k.Max();
- for (int i = 1; i <= max; i++)
- {
- if ((i * min) % max == 0)
- {
- var p1 = new Point(
- position.X + (velocity.X == max ? i : i * min / max),
- position.Y + (velocity.Y == max ? i : i * min / max));
- if (planets.Contains(p1))
- throw new CrushException();
- }
- }
- // apply move
- rocket.X += velocity.X;
- rocket.Y += velocity.Y;
- // bounds check
- if (rocket.X < 0 || rocket.X >= universeSize.X || rocket.Y < 0 || rocket.Y >= universeSize.Y)
- throw new Exception(); // using plain Exception for 'escape' condition
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement