Advertisement
simonradev

PowerPlants

Apr 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. namespace PowerPlants
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             int[] powerPlants = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  11.  
  12.             int currentDay = -1;
  13.             int currentSeason = 0;
  14.  
  15.             bool thereIsPlantAlive = false;
  16.             while (true)
  17.             {
  18.                 currentDay++;
  19.                 int currIndex = currentDay % powerPlants.Length;
  20.                 thereIsPlantAlive = ManipulateThePlants(powerPlants, currIndex, -1);
  21.  
  22.                 if (!thereIsPlantAlive)
  23.                 {
  24.                     break;
  25.                 }
  26.  
  27.                 if (currIndex + 1 == powerPlants.Length)
  28.                 {
  29.                     currentSeason++;
  30.  
  31.                     ManipulateThePlants(powerPlants, int.MinValue, +1);
  32.                 }
  33.  
  34.             }
  35.  
  36.             Console.WriteLine($"survived {currentDay + 1} days ({currentSeason} seasons)");
  37.         }
  38.  
  39.         private static bool ManipulateThePlants(int[] powerPlants, int currentDay, int bloomPower)
  40.         {
  41.             bool thereIsPlantAlive = false;
  42.             for (int currPlant = 0; currPlant < powerPlants.Length; currPlant++)
  43.             {
  44.                 if (powerPlants[currPlant] == 0)
  45.                 {
  46.                     continue;
  47.                 }
  48.  
  49.                 if (currPlant == currentDay)
  50.                 {
  51.                     thereIsPlantAlive = true;
  52.  
  53.                     continue;
  54.                 }
  55.  
  56.                 powerPlants[currPlant] += bloomPower;
  57.  
  58.                 if (powerPlants[currPlant] > 0)
  59.                 {
  60.                     thereIsPlantAlive = true;
  61.                 }
  62.             }
  63.  
  64.             return thereIsPlantAlive;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement