daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Dec 6th, 2018 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import std.stdio;
  2. import std.datetime;
  3. import std.file;
  4. import std.format;
  5. import std.algorithm;
  6. import std.string;
  7. import std.range;
  8.  
  9. void main(string[] args)
  10. {
  11.     auto logLines = readText("data.txt").splitLines().sort();
  12.     int year, month, day, hour, min, g, startMinute;
  13.     int[][int] guards;
  14.     string log;
  15.  
  16.     foreach(line; logLines) {
  17.         line.formattedRead!"[%d-%d-%d %d:%d] %s"(year, month, day, hour, min, log);
  18.         auto date = DateTime(year, month, day, hour, min);
  19.  
  20.         final switch(log[0]) {
  21.             case 'G':
  22.                 log.formattedRead!"Guard #%d begins shift"(g);
  23.                 break;
  24.  
  25.             case 'f':
  26.                 startMinute = date.minute;
  27.                 break;
  28.  
  29.             case 'w':
  30.                 guards[g] ~= iota(startMinute, date.minute, 1).array;
  31.                 break;
  32.         }
  33.     }
  34.  
  35.     auto minuteSum = guards.values.map!(a => a.length);
  36.     auto id = assocArray(zip(minuteSum, guards.keys))[minuteSum.maxElement];
  37.     auto minutesCount = guards[id].sort.group.assocArray;
  38.     auto maxCount = minutesCount.values.reduce!max;
  39.     auto minute = assocArray(zip(minutesCount.values, minutesCount.keys))[maxCount];
  40.  
  41.     writeln(id * minute);
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top