Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
stackoverflow_25444818
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Globalization; namespace Rextester { static class Extensions { public static string[] ParseColumns(this String line) { return line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } public static DateTime ToDateTime(this String[] line) { const string datetime_format = "dd/MM/yyyy H:mm:ss"; return DateTime.ParseExact( line[2] + " " + line[3], datetime_format, CultureInfo.InvariantCulture ); } } public class Program { public static void Main(string[] args) { //var lines = File // .ReadAllLines("input.txt", Encoding.Default) // .Skip(1); var lines = @" No. ID DATE_EVENT TIME_EVENT EVENT CODE 102995 018159871 07/08/2014 09:01:57 9008 1111 20398 018159871 07/08/2014 09:01:58 1000 1402 105541 018159871 07/08/2014 09:01:58 9210 1111 63492 018253609 07/08/2014 09:54:26 9008 905 37552 018253609 07/08/2014 09:54:45 9008 1111 9627 018253609 07/08/2014 09:54:48 9210 1111 112700 018253609 07/08/2014 09:54:48 1000 1402 50555 018253609 07/08/2014 09:55:56 1000 1401 63634 018253609 07/08/2014 09:55:56 9210 1111 34551 018330948 07/08/2014 09:21:51 9008 905 47252 018330948 07/08/2014 09:22:15 9008 1111 3975 018330948 07/08/2014 09:22:17 1000 1402 24196 018330948 07/08/2014 09:22:17 9210 1111 111150 018342571 07/08/2014 09:40:08 9008 905 17119 018342571 07/08/2014 09:40:19 9008 1111 18658 018342571 07/08/2014 09:40:21 9210 1111 25654 018342571 07/08/2014 09:40:21 1000 1402 " .Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries) .Select(x=>x.Trim()) .Skip(2); var event_tuples = lines .Zip(lines.Skip(1), (start, end) => new { Start = start, End = end }); var entries = event_tuples .Select(x => { var start_data = x.Start.ParseColumns(); var end_data = x.End.ParseColumns(); var duration = end_data.ToDateTime() - start_data.ToDateTime(); return new { No=start_data[0], Id=start_data[1], Duration = duration, Event = start_data[4], Code = start_data[5] }; }) ; var query = entries .Where(x => x.Event == "9008" && new[] { "905", "1111" }.Contains(x.Code)) ; Console.WriteLine("{0} events found",query.Count()); var total_duration = query .Select(x=>x.Duration) .Aggregate((a, b) => a + b); Console.WriteLine("total duration: {0}", total_duration); } } }
run
|
edit
|
history
|
help
0
12
Side 1
dxcsdfcsasxd
database interior 0.2
Working with Strings
shorten file paths in a commandline using regex
2 point gradient (v2)
QuickSort
HawkSearch field naming convention
Better Random number blocks