Advertisement
social1986

Untitled

Jan 11th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.Snowmen
  6. {
  7.     public class Startup
  8.     {
  9.         public static void Main()
  10.         {
  11.             var snowmen = Console.ReadLine()
  12.                 .Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.  
  16.             var lostSnowmen = new List<int>();
  17.  
  18.             while (snowmen.Count > 1)
  19.             {
  20.  
  21.                 for (int i = 0; i < snowmen.Count - 1; i++)
  22.                 {                    
  23.                     if (lostSnowmen.Contains(i))
  24.                     {
  25.                         continue;
  26.                     }
  27.                     var targerIndex = CheckValue(snowmen[i], snowmen.Count);
  28.                     var atacker = i;
  29.  
  30.                     if (targerIndex == atacker)
  31.                     {
  32.                         Console.WriteLine($"{atacker} performed harakiri");
  33.                         lostSnowmen.Add(atacker);
  34.                     }
  35.                     else
  36.                     {
  37.                         var winner = Winner(targerIndex, atacker);
  38.                         var loser = Loser(targerIndex, atacker);
  39.                         lostSnowmen.Add(loser);
  40.                         Console.WriteLine($"{atacker} x {targerIndex} -> {winner} wins");
  41.                     }
  42.  
  43.                     if (snowmen.Count - lostSnowmen.Count == 1)
  44.                     {
  45.                         return;
  46.                     }
  47.                 }
  48.  
  49.                 var snowmenToBeremoved = lostSnowmen.OrderByDescending(p => p);
  50.  
  51.                 foreach (var loser in snowmenToBeremoved)
  52.                 {
  53.                     snowmen.RemoveAt(loser);
  54.                 }
  55.                 lostSnowmen.Clear();
  56.             }
  57.         }
  58.  
  59.         public static int CheckValue(int value, int length)
  60.         {
  61.             if (value > length - 1)
  62.             {
  63.                 value %= length;
  64.             }
  65.             return value;
  66.         }
  67.  
  68.         public static int Winner(int targetIndex, int atacker)
  69.         {
  70.             if (Math.Abs(targetIndex - atacker) % 2 != 0)
  71.             {
  72.                 return targetIndex;
  73.             }
  74.             return atacker;
  75.         }
  76.  
  77.         public static int Loser(int targetIndex, int atacker)
  78.         {
  79.             if (Math.Abs(targetIndex - atacker) % 2 != 0)
  80.             {
  81.                 return atacker;
  82.             }
  83.             return targetIndex;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement