Advertisement
bullit3189

Santa's Gifts - TF-MidExam 10Jan19

Jan 26th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _02Santa_sGifts
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int n = int.Parse(Console.ReadLine());
  12.  
  13. List<int> houses = Console.ReadLine().Split().Select(int.Parse).ToList();
  14. int santaCurrPos = 0;
  15.  
  16. for (int i = 0; i < n; i++)
  17. {
  18. string command = Console.ReadLine();
  19.  
  20. string[] tokens = command.Split();
  21.  
  22. string action = tokens[0];
  23.  
  24. if (action == "Forward")
  25. {
  26. int steps = int.Parse(tokens[1]);
  27.  
  28.  
  29. if (santaCurrPos + steps < 0 || santaCurrPos + steps > houses.Count - 1)
  30. {
  31. continue;
  32. }
  33. santaCurrPos += steps;
  34. houses.RemoveAt(santaCurrPos);
  35. }
  36. else if (action == "Back")
  37. {
  38. int steps = int.Parse(tokens[1]);
  39.  
  40.  
  41. if (santaCurrPos-steps < 0 || santaCurrPos-steps > houses.Count - 1)
  42. {
  43. continue;
  44. }
  45. santaCurrPos -= steps;
  46.  
  47. houses.RemoveAt(santaCurrPos);
  48. }
  49. else if (action == "Gift")
  50. {
  51. int index = int.Parse(tokens[1]);
  52.  
  53. if (index<0 || index>houses.Count-1)
  54. {
  55. continue;
  56. }
  57. int houseNum = int.Parse(tokens[2]);
  58. houses.Insert(index, houseNum);
  59. santaCurrPos = index;
  60. }
  61. else if (action == "Swap")
  62. {
  63. int firstHouseNum = int.Parse(tokens[1]);
  64.  
  65. if (!houses.Contains(firstHouseNum))
  66. {
  67. continue;
  68. }
  69. int secondHouseNum = int.Parse(tokens[2]);
  70.  
  71. if (!houses.Contains(secondHouseNum))
  72. {
  73. continue;
  74. }
  75.  
  76. int firstIndex = houses.IndexOf(firstHouseNum);
  77. int secondIndex = houses.IndexOf(secondHouseNum);
  78.  
  79. houses.Insert(secondIndex, firstHouseNum);
  80. houses.RemoveAt(secondIndex + 1);
  81.  
  82. houses.Insert(firstIndex, secondHouseNum);
  83. houses.RemoveAt(firstIndex + 1);
  84. }
  85. }
  86.  
  87. Console.WriteLine($"Position: {santaCurrPos}");
  88. Console.WriteLine(string.Join(", ",houses));
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement