Advertisement
yanchevilian

1. Bombs/ C# Advanced Exam - 28 June 2020

Dec 7th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Channels;
  7.  
  8. namespace _01._Bombs
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //Input
  15.             //    •   On the first line, you will receive the integers representing the bomb effects, separated by ", ".
  16.             //    •   On the second line, you will receive the integers representing the bomb casing, separated by ", ".
  17.  
  18.             int[] bombEffects = ReadInput();
  19.             int[] bombCasing = ReadInput();
  20.  
  21.             Queue<int> bombEffectInQueue = new Queue<int>(bombEffects);
  22.             Stack<int> bombCasingStack = new Stack<int>(bombCasing);
  23.  
  24.             Dictionary<string, int> bombPouch = new Dictionary<string, int>()
  25.             {
  26.                 {"Datura Bombs", 0},
  27.                 {"Cherry Bombs", 0},
  28.                 {"Smoke Decoy Bombs", 0}
  29.             };
  30.             bool isFilledBombPouch = false;
  31.  
  32.             //You need to stop combining when you have no more bomb effects or bomb casings, or you successfully filled the bomb pouch
  33.             while (bombEffectInQueue.Count > 0 && bombCasingStack.Count > 0)
  34.             {
  35.                 int currBombEffect = bombEffectInQueue.Peek();
  36.                 int currBombCase = bombCasingStack.Peek();
  37.                 CheckBombCoincidence(currBombEffect, currBombCase, bombPouch, bombCasingStack, bombEffectInQueue);
  38.  
  39.                 if (bombPouch.Count == 3)
  40.                 {
  41.                     if (IsFilledBombPouch(bombPouch))
  42.                     {
  43.                         isFilledBombPouch = true;
  44.                         break;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             Console.WriteLine(isFilledBombPouch ? "Bene! You have successfully filled the bomb pouch!" :
  50.                                                         "You don't have enough materials to fill the bomb pouch.");
  51.  
  52.             Console.WriteLine(bombEffectInQueue.Count > 0
  53.                                          ? $"Bomb Effects: {string.Join(", ", bombEffectInQueue)}"
  54.                                          : "Bomb Effects: empty");
  55.  
  56.             Console.WriteLine(bombCasingStack.Count > 0 ? $"Bomb Casings: {string.Join(", ", bombCasingStack)}" :
  57.                                                             "Bomb Casings: empty");
  58.            
  59.             foreach (var currentBomb in bombPouch.OrderBy(n => n.Key))
  60.             {
  61.                 Console.WriteLine($"{currentBomb.Key}: {currentBomb.Value}");
  62.             }
  63.         }
  64.  
  65.         private static void CheckBombCoincidence(int currBombEffect, int currBombCase, Dictionary<string, int> bombPouch,
  66.             Stack<int> bombCasingStack, Queue<int> bombEffectInQueue)
  67.         {
  68.             int sumBombs = currBombEffect + currBombCase;
  69.             switch (sumBombs)
  70.             {
  71.                 case 40:
  72.                     bombPouch["Datura Bombs"] += 1;
  73.                     bombCasingStack.Pop();
  74.                     bombEffectInQueue.Dequeue();
  75.                     break;
  76.                 case 60:
  77.                     bombPouch["Cherry Bombs"] += 1;
  78.                     bombCasingStack.Pop();
  79.                     bombEffectInQueue.Dequeue();
  80.                     break;
  81.                 case 120:
  82.                     bombPouch["Smoke Decoy Bombs"] += 1;
  83.                     bombCasingStack.Pop();
  84.                     bombEffectInQueue.Dequeue();
  85.                     break;
  86.                 default:
  87.                     bombCasingStack.Pop();
  88.                     bombCasingStack.Push(currBombCase - 5);
  89.                     break;
  90.             }
  91.         }
  92.  
  93.         public static int[] ReadInput()
  94.         {
  95.             int[] input = Console.ReadLine()
  96.                 .Split(", ", StringSplitOptions.RemoveEmptyEntries)
  97.                 .Select(int.Parse)
  98.                 .ToArray();
  99.  
  100.             return input;
  101.         }
  102.  
  103.         private static bool IsFilledBombPouch(Dictionary<string, int> bombPouch)
  104.         {
  105.             bool isFilled = false;
  106.             foreach (var currentBomb in bombPouch)
  107.             {
  108.                 if (currentBomb.Value >= 3)
  109.                 {
  110.                     isFilled = true;
  111.                 }
  112.                 else
  113.                 {
  114.                     isFilled = false;
  115.                     break;
  116.                 }
  117.             }
  118.             return isFilled;
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement