Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int fieldSize = int.Parse(Console.ReadLine());
- int[] field = new int[fieldSize];
- Array.Fill(field, 0);
- //Populating bugs on the field:
- int[] bugPlaces = Console.ReadLine().Split().Select(int.Parse).ToArray();
- for (int i = 0; i < bugPlaces.Length; i++)
- {
- if (bugPlaces[i] < 0 || bugPlaces[i] > field.Length - 1) { continue; }
- int pos = bugPlaces[i];
- field[pos] = 1;
- }
- string input;
- string[] cmd = new string[3]; //Command holder: [0]-position, [1]-direction, [2]-steps
- while ((input = Console.ReadLine()) != "end")
- {
- cmd = input.Split().ToArray();
- int pos = int.Parse(cmd[0]);
- int steps = int.Parse(cmd[2]);
- int vector = 0;
- if (cmd[1] == "right") { vector = 1; }
- else if (cmd[1] == "left") { vector = -1; }
- //Give order
- if (pos < 0 || pos > field.Length - 1 || field[pos] == 0)
- {
- continue; //Nothing happens
- }
- else
- {
- int afterFlight = pos + vector * steps;
- if (afterFlight > field.Length - 1 || afterFlight < 0)
- {
- field[pos] = 0; //The bug flew away
- continue;
- }
- else
- {
- if (field[afterFlight] == 0)
- {
- field[pos] = 0;
- field[afterFlight] = 1;
- }
- else
- {
- //Flying until there is a place for landing:
- while (field[afterFlight] == 1)
- {
- field[pos] = 0;
- pos = afterFlight;
- afterFlight = pos + vector * steps;
- if (afterFlight < 0 || afterFlight > field.Length - 1)
- {
- break;
- }
- else
- {
- field[afterFlight] = 1;
- break;
- }
- }
- }
- }
- }
- }
- Console.WriteLine(String.Join(" ", field));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement