Advertisement
j0k3r

core2 #2

Jan 13th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApplication4
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var freights = new List<FreightModel>();
  13.  
  14.             if (File.Exists("tavok.txt"))
  15.             {
  16.                 using (var sr = new StreamReader("tavok.txt"))
  17.                 {
  18.                     while (!sr.EndOfStream)
  19.                     {
  20.                         var line = sr.ReadLine();
  21.  
  22.                         if (string.IsNullOrWhiteSpace(line))
  23.                         {
  24.                             throw new Exception("Empty line");
  25.                         }
  26.  
  27.                         var parts = line.Split(' ');
  28.  
  29.                         freights.Add(new FreightModel
  30.                         {
  31.                             Day = int.Parse(parts[0]),
  32.                             Freight = int.Parse(parts[1]),
  33.                             Distance = int.Parse(parts[2])
  34.                         });
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             freights = freights.GroupBy(g => g.Day).OrderBy(g => g.Key).SelectMany(g => g.OrderBy(x => x.Freight)).ToList();
  40.  
  41.             //foreach (var item in freights)
  42.             //{
  43.             //    Console.WriteLine("{0} {1} {2}", item.Day, item.Freight, item.Distance);
  44.             //}
  45.  
  46.             var first = freights.FirstOrDefault();
  47.  
  48.             if (first != null)
  49.             {
  50.                 Console.WriteLine("First freight: {0} {1} {2}", first.Day, first.Freight, first.Distance);
  51.             }
  52.  
  53.             var last = freights.LastOrDefault(x => x.Day == 7);
  54.  
  55.             if (last != null)
  56.             {
  57.                 Console.WriteLine("Last freight at day 7: {0} {1} {2}", last.Day, last.Freight, last.Distance);
  58.             }
  59.  
  60.             Console.ReadLine();
  61.         }
  62.     }
  63.  
  64.     class FreightModel
  65.     {
  66.         public int Day { get; set; }
  67.         public int Freight { get; set; }
  68.         public int Distance { get; set; }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement