Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _02.Snowmen
- {
- public class Startup
- {
- public static void Main()
- {
- var snowmen = Console.ReadLine()
- .Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToList();
- var lostSnowmen = new List<int>();
- while (snowmen.Count > 1)
- {
- for (int i = 0; i < snowmen.Count - 1; i++)
- {
- if (lostSnowmen.Contains(i))
- {
- continue;
- }
- var targerIndex = CheckValue(snowmen[i], snowmen.Count);
- var atacker = i;
- if (targerIndex == atacker)
- {
- Console.WriteLine($"{atacker} performed harakiri");
- lostSnowmen.Add(atacker);
- }
- else
- {
- var winner = Winner(targerIndex, atacker);
- var loser = Loser(targerIndex, atacker);
- lostSnowmen.Add(loser);
- Console.WriteLine($"{atacker} x {targerIndex} -> {winner} wins");
- }
- if (snowmen.Count - lostSnowmen.Count == 1)
- {
- return;
- }
- }
- var snowmenToBeremoved = lostSnowmen.OrderByDescending(p => p);
- foreach (var loser in snowmenToBeremoved)
- {
- snowmen.RemoveAt(loser);
- }
- lostSnowmen.Clear();
- }
- }
- public static int CheckValue(int value, int length)
- {
- if (value > length - 1)
- {
- value %= length;
- }
- return value;
- }
- public static int Winner(int targetIndex, int atacker)
- {
- if (Math.Abs(targetIndex - atacker) % 2 != 0)
- {
- return targetIndex;
- }
- return atacker;
- }
- public static int Loser(int targetIndex, int atacker)
- {
- if (Math.Abs(targetIndex - atacker) % 2 != 0)
- {
- return atacker;
- }
- return targetIndex;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement