| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace D4._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) return;
- if (File.Exists(args[0]) == false) return;
- var dic = new Dictionary<DateTime, string>();
- var guard = new Dictionary<string, Dictionary<int, int>>();
- var file = File.OpenText(args[0]);
- var regex = new Regex(@"\[(.*)\]");
- do
- {
- var line = file.ReadLine();
- if (line == null) break;
- var dateStr = line.Substring(0, 18).Trim('[', ']');
- var date = DateTime.Parse(dateStr);
- dic.Add(date, line.Substring(19, line.Length - 19));
- } while (true);
- var ordered = dic.OrderBy(kvp => kvp.Key);
- string curGuard = string.Empty;
- DateTime asleep = DateTime.Now;
- foreach (var kvp in ordered)
- {
- var date = kvp.Key;
- var str = kvp.Value;
- if (str.StartsWith("Guard")) curGuard = str.Split(' ')[1];
- {
- if (guard.ContainsKey(curGuard) == false)
- {
- var d = new Dictionary<int, int>();
- for (var i = 0; i < 60; i++) d.Add(i, 0);
- guard.Add(curGuard, d);
- }
- }
- if (str == "falls asleep")
- {
- asleep = date;
- }
- if (str == "wakes up")
- {
- for (int m = asleep.Minute; m < date.Minute; m++)
- guard[curGuard][m] += 1;
- }
- }
- KeyValuePair<int, int> highestMinute = new KeyValuePair<int, int>(0, 0);
- string sleeper = "";
- foreach (var g in guard)
- {
- var minute = g.Value.OrderByDescending(v => v.Value).FirstOrDefault();
- if (minute.Value > highestMinute.Value)
- {
- highestMinute = minute;
- sleeper = g.Key;
- }
- }
-
- Console.WriteLine($"Guard {sleeper} sleeps {highestMinute.Value} times on {highestMinute.Key}th minute");
- var id = int.Parse(sleeper.TrimStart('#'));
- Console.WriteLine($"Answer : {id * highestMinute.Key}");
- }
- }
- }
|