Advertisement
WindFell

Iron Girder

Aug 28th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class IronGirder
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         Dictionary<string, Town> towns = new Dictionary<string, Town>();
  10.         string input;
  11.  
  12.         while ((input = Console.ReadLine()) != "Slide rule")
  13.         {
  14.             string[] commandArgs = input
  15.                 .Split(new string[] { ":", "->" }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             string townName = commandArgs[0];
  18.             int passengers = int.Parse(commandArgs[2]);
  19.  
  20.             if (commandArgs[1] == "ambush")
  21.             {
  22.                 if (towns.ContainsKey(townName))
  23.                 {
  24.                     towns[townName].Ambush(passengers);
  25.                 }
  26.                 continue;
  27.             }
  28.  
  29.             int time = int.Parse(commandArgs[1]);
  30.  
  31.             if (towns.ContainsKey(townName) == false)
  32.             {
  33.                 towns[townName] = new Town(townName, time, passengers);
  34.             }
  35.             else
  36.             {
  37.                 if (towns[townName].Time > time || towns[townName].Time == 0)
  38.                 {
  39.                     towns[townName].ChangeTime(time);
  40.                 }
  41.  
  42.                 towns[townName].AddPassengers(passengers);
  43.             }
  44.         }
  45.  
  46.         towns
  47.             .Select(t => t.Value)
  48.             .Where(t => t.Time != 0 && t.Passengers != 0)
  49.             .OrderBy(t => t.Time)
  50.             .ThenBy(t => t.Name)
  51.             .ToList()
  52.             .ForEach(t => Console.WriteLine(t));
  53.     }
  54. }
  55.  
  56. class Town
  57. {    
  58.     public Town(string name, int time, int passengers)
  59.     {
  60.         this.Name = name;
  61.         this.Time = time;
  62.         this.Passengers = passengers;
  63.     }
  64.  
  65.     public string Name { get; private set; }
  66.  
  67.     public int Time { get; private set; }
  68.  
  69.     public int Passengers { get; private set; }
  70.  
  71.     public void AddPassengers(int passengers)
  72.     {
  73.         this.Passengers += passengers;
  74.     }
  75.  
  76.     public void Ambush(int passengers)
  77.     {
  78.         this.Time = 0;
  79.         this.Passengers -= passengers;
  80.         if (this.Passengers < 0)
  81.         {
  82.             this.Passengers = 0;
  83.         }
  84.     }
  85.  
  86.     public void ChangeTime(int time)
  87.     {
  88.         this.Time = time;
  89.     }
  90.  
  91.     public override string ToString()
  92.     {
  93.         string result = $"{this.Name} -> Time: {this.Time} -> Passemgers: {this.Passengers}";
  94.         return result;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement