Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class SoftUniDemosC
  6. {
  7.     public static void Main(string[] args)
  8.     {
  9.         var jarvis = new Jarvis();
  10.  
  11.         var energy = long.Parse(Console.ReadLine());
  12.  
  13.         var command = Console.ReadLine().Split();
  14.  
  15.         while (command[0] != "Assemble!")
  16.         {
  17.             jarvis.Apply(PartFactory.Get(command));
  18.             command = Console.ReadLine().Split();
  19.         }
  20.  
  21.         switch (jarvis.Assemble(energy))
  22.         {
  23.             case 1:
  24.                 Console.WriteLine("We need more parts!");
  25.                 break;
  26.             case 2:
  27.                 Console.WriteLine("We need more power!");
  28.                 break;
  29.             default:
  30.                 Console.WriteLine(jarvis);
  31.                 break;
  32.         }
  33.     }
  34. }
  35.  
  36. internal class Jarvis
  37. {
  38.     private long energy = 0;
  39.     private Head head;
  40.     private Torso torso;
  41.     private readonly List<Arm> arms = new List<Arm>();
  42.     private readonly List<Leg> legs = new List<Leg>();
  43.  
  44.     public bool Apply(Part p)
  45.     {
  46.         if (p == null) return false;
  47.  
  48.         switch (p.GetType().Name)
  49.         {
  50.             case "Head":
  51.                 var h = (Head)p;
  52.                 if (head != null)
  53.                 {
  54.                     if (h.Energy >= head.Energy) return false;
  55.                     energy -= head.Energy - h.Energy;
  56.                     head = h;
  57.                 }
  58.                 else
  59.                 {
  60.                     head = h;
  61.                     energy += h.Energy;
  62.                 }
  63.                 return true;
  64.             case "Torso":
  65.                 var t = (Torso)p;
  66.                 if (torso != null)
  67.                 {
  68.                     if (t.Energy >= torso.Energy) return false;
  69.                     energy -= torso.Energy - t.Energy;
  70.                     torso = t;
  71.                 }
  72.                 else
  73.                 {
  74.                     torso = t;
  75.                     energy += t.Energy;
  76.                 }
  77.                 return true;
  78.             case "Arm":
  79.                 var a = (Arm)p;
  80.                 if (arms.Count > 1)
  81.                 {
  82.                     var idx = arms[0].Energy > arms[1].Energy ? 0 : 1;
  83.                     if (a.Energy >= arms[idx].Energy) return false;
  84.                     energy -= arms[idx].Energy - a.Energy;
  85.                     arms.RemoveAt(idx);
  86.                     arms.Add(a);
  87.                 }
  88.                 else
  89.                 {
  90.                     arms.Add(a);
  91.                     energy += a.Energy;
  92.                 }
  93.                 return true;
  94.             case "Leg":
  95.                 var l = (Leg)p;
  96.                 if (legs.Count > 1)
  97.                 {
  98.                     var idx = legs[0].Energy > legs[1].Energy ? 0 : 1;
  99.                     if (l.Energy >= legs[idx].Energy) return false;
  100.                     energy -= legs[idx].Energy - l.Energy;
  101.                     legs.RemoveAt(idx);
  102.                     legs.Add(l);
  103.                 }
  104.                 else
  105.                 {
  106.                     legs.Add(l);
  107.                     energy += l.Energy;
  108.                 }
  109.                 return true;
  110.             default:
  111.                 return false;
  112.         }
  113.     }
  114.  
  115.     public int Assemble(long energy)
  116.     {
  117.         if (head == null || torso == null || legs.Count != 2 || arms.Count != 2)
  118.         {
  119.             return 1;
  120.         }
  121.         if (energy < this.energy)
  122.         {
  123.             return 2;
  124.         }
  125.         return 0;
  126.     }
  127.  
  128.     public override string ToString()
  129.     {
  130.         return $"{GetType().Name}:\n{head}\n{torso}\n{string.Join("\n", arms.OrderBy(a => a.Energy))}\n{string.Join("\n", legs.OrderBy(l => l.Energy))}";
  131.     }
  132. }
  133.  
  134. internal abstract class Part { }
  135.  
  136. internal class Head : Part
  137. {
  138.     public int Energy { get; }
  139.     public int Iq { get; }
  140.     public string Material { get; }
  141.  
  142.     public Head() { }
  143.  
  144.     public Head(int e, int i, string m)
  145.     {
  146.         Energy = e;
  147.         Iq = i;
  148.         Material = m;
  149.     }
  150.  
  151.     public override string ToString()
  152.     {
  153.         return $"#{GetType().Name}:\n###Energy consumption: {Energy}\n###IQ: {Iq}\n###Skin material: {Material}";
  154.     }
  155. }
  156.  
  157. internal class Torso : Part
  158. {
  159.     public int Energy { get; }
  160.     public double Size { get; }
  161.     public string Material { get; }
  162.  
  163.     public Torso() { }
  164.  
  165.     public Torso(int e, double d, string m)
  166.     {
  167.         Energy = e;
  168.         Size = d;
  169.         Material = m;
  170.     }
  171.  
  172.     public override string ToString()
  173.     {
  174.         return $"#{GetType().Name}:\n###Energy consumption: {Energy}\n###Processor size: {Size:0.0}\n###Corpus material: {Material}";
  175.     }
  176. }
  177.  
  178. internal class Arm : Part
  179. {
  180.     public int Energy { get; }
  181.     public int Reach { get; }
  182.     public int Fingers { get; }
  183.  
  184.     public Arm() { }
  185.  
  186.     public Arm(int e, int r, int f)
  187.     {
  188.         Energy = e;
  189.         Reach = r;
  190.         Fingers = f;
  191.     }
  192.  
  193.     public override string ToString()
  194.     {
  195.         return $"#{GetType().Name}:\n###Energy consumption: {Energy}\n###Reach: {Reach}\n###Fingers: {Fingers}";
  196.     }
  197. }
  198.  
  199. internal class Leg : Part
  200. {
  201.     public int Energy { get; }
  202.     public int Strength { get; }
  203.     public int Speed { get; }
  204.  
  205.     public Leg() { }
  206.  
  207.     public Leg(int e, int s, int sp)
  208.     {
  209.         Energy = e;
  210.         Strength = s;
  211.         Speed = sp;
  212.     }
  213.  
  214.     public override string ToString()
  215.     {
  216.         return $"#{GetType().Name}:\n###Energy consumption: {Energy}\n###Strength: {Strength}\n###Speed: {Speed}";
  217.     }
  218. }
  219.  
  220. internal static class PartFactory
  221. {
  222.     public static Part Get(string[] input)
  223.     {
  224.         if (input.Length != 4) return null;
  225.         try
  226.         {
  227.  
  228.             switch (input[0])
  229.             {
  230.                 case "Head":
  231.                     return new Head(int.Parse(input[1]), int.Parse(input[2]), input[3]);
  232.                 case "Torso":
  233.                     return new Torso(int.Parse(input[1]), double.Parse(input[2]), input[3]);
  234.                 case "Arm":
  235.                     return new Arm(int.Parse(input[1]), int.Parse(input[2]), int.Parse(input[3]));
  236.                 case "Leg":
  237.                     return new Leg(int.Parse(input[1]), int.Parse(input[2]), int.Parse(input[3]));
  238.                 default:
  239.                     return null;
  240.             }
  241.         }
  242.         catch
  243.         {
  244.             return null;
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement