social1986

Untitled

Jan 11th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 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 HashSet<int>();
  17.  
  18.             while (snowmen.Count > 1)
  19.             {                  
  20.  
  21.                 for (int i = 0; i < snowmen.Count; i++)
  22.                 {
  23.                     if (snowmen.Count - lostSnowmen.Count == 1)
  24.                     {
  25.                         return;
  26.                     }
  27.  
  28.                     if (lostSnowmen.Contains(i))
  29.                     {
  30.                         continue;
  31.                     }
  32.                     var targetIndex = CheckValue(snowmen[i], snowmen.Count);
  33.                     var atacker = i;
  34.  
  35.                     if (targetIndex == atacker)
  36.                     {
  37.                         Console.WriteLine($"{atacker} performed harakiri");
  38.                         lostSnowmen.Add(atacker);
  39.                     }
  40.                     else
  41.                     {
  42.                         var winner = Winner(targetIndex, atacker);
  43.                         var loser = Loser(targetIndex, atacker);
  44.                         lostSnowmen.Add(loser);
  45.                         Console.WriteLine($"{atacker} x {targetIndex} -> {winner} wins");
  46.                     }                    
  47.                 }
  48.  
  49.                 foreach (var loser in lostSnowmen.OrderByDescending(p=>p))
  50.                 {
  51.                     snowmen.RemoveAt(loser);
  52.                 }
  53.                 lostSnowmen.Clear();
  54.             }
  55.         }
  56.  
  57.         public static int CheckValue(int value, int length)
  58.         {
  59.             if (value > length - 1)
  60.             {
  61.                 value %= length;
  62.             }
  63.             return value;
  64.         }
  65.  
  66.         public static int Winner(int targetIndex, int atacker)
  67.         {
  68.             if (Math.Abs(targetIndex - atacker) % 2 != 0)
  69.             {
  70.                 return targetIndex;
  71.             }
  72.             return atacker;
  73.         }
  74.  
  75.         public static int Loser(int targetIndex, int atacker)
  76.         {
  77.             if (Math.Abs(targetIndex - atacker) % 2 != 0)
  78.             {
  79.                 return atacker;
  80.             }
  81.             return targetIndex;
  82.         }
  83.     }
  84. }
Add Comment
Please, Sign In to add comment