Advertisement
Guest User

Winecraft

a guest
Jun 27th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Winecraft
  8. {
  9.     class Winecraft
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> grapes = Console.ReadLine()
  14.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(int.Parse)
  16.                 .ToList();
  17.  
  18.             int growthDays = int.Parse(Console.ReadLine());
  19.  
  20.             bool[] isGreatGrape = new bool[grapes.Count + 1];   // Longer than grapes cnt to avoid out of range exception later
  21.             CheckForGreatGrapes(grapes, isGreatGrape);      // Check all grapes for great grapes
  22.  
  23.             while (grapes.Count >= growthDays)
  24.             {
  25.                 for (int i = 0; i < growthDays; i++)
  26.                 {
  27.                     IncrementGrapes(grapes, isGreatGrape);
  28.                     CheckForGreatGrapes(grapes, isGreatGrape);   // Check again after incrementing for new found great grapes
  29.                 }
  30.  
  31.                 CheckForWeakGrapes(grapes, growthDays);
  32.             }
  33.  
  34.             Console.WriteLine(string.Join(" ", grapes));
  35.         }
  36.  
  37.         static void CheckForWeakGrapes(List<int> grapes, int growthDays)
  38.         {
  39.             int healthyGrapes = grapes.Count;
  40.             for (int i = 0; i < grapes.Count; i++)
  41.             {
  42.                 if (grapes[i] <= growthDays)
  43.                 {
  44.                     healthyGrapes--;
  45.                     grapes[i] = 0;
  46.                 }
  47.             }
  48.  
  49.             if (healthyGrapes < growthDays)
  50.             {
  51.                 LeaveHealthyGrapesOnly(healthyGrapes, growthDays, grapes);
  52.             }
  53.         }
  54.  
  55.         static void LeaveHealthyGrapesOnly(int healthyGrapes, int growthDays, List<int> grapes)
  56.         {
  57.             for (int i = 0; i < grapes.Count; i++)
  58.             {
  59.                 if (grapes[i] < growthDays)
  60.                 {
  61.                     grapes.Remove(grapes[i]);
  62.                     i--;
  63.                 }
  64.             }
  65.         }
  66.  
  67.         static void IncrementGrapes(List<int> grapes, bool[] isGreatGrape)
  68.         {
  69.             for (int i = 0; i < grapes.Count; i++)
  70.             {
  71.                 if (isGreatGrape[i])
  72.                 {
  73.                     grapes[i]++;
  74.                     if (grapes[i - 1] > 0)
  75.                     {
  76.                         grapes[i]++;
  77.                         grapes[i - 1]--;
  78.                     }
  79.  
  80.                     if (grapes[i + 1] > 0)
  81.                     {
  82.                         grapes[i]++;
  83.                         grapes[i + 1]--;
  84.                     }
  85.                 }
  86.                 else
  87.                 {
  88.                     if (i == 0 && !isGreatGrape[i + 1])
  89.                     {
  90.                         grapes[i]++;
  91.                     }
  92.                     else if (i == grapes.Count - 1 && !isGreatGrape[i - 1])
  93.                     {
  94.                         grapes[i]++;
  95.                     }
  96.                     else if (!isGreatGrape[i + 1] && !isGreatGrape[i - 1])
  97.                     {
  98.                         grapes[i]++;
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.  
  104.         static void CheckForGreatGrapes(List<int> grapes, bool[] isGreatGrape)
  105.         {
  106.             for (int i = 1; i < grapes.Count - 1; i++)
  107.             {
  108.                 int currentGrape = grapes[i];
  109.                 int previousGrape = grapes[i - 1];
  110.                 int nextGrape = grapes[i + 1];
  111.                 if (currentGrape > previousGrape && currentGrape > nextGrape)
  112.                 {
  113.                     isGreatGrape[i] = true;
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement