public static int Strategy1(SleepLog sleepLog) { var biggestSleeper = sleepLog.TotalSleep.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; var mostSleptMinute = sleepLog.MinuteAsleep[biggestSleeper].Aggregate((l, r) => l.Value > r.Value ? l : r).Key; return(biggestSleeper * mostSleptMinute); }
public static SleepLog ReadLogs(IList <string> input) { var logs = ParseInput(input); var sleepLog = new SleepLog(); var currentId = -1; var fellAsleep = -1; foreach (var log in logs) { if (log.Message.Contains("begins shift")) { currentId = Convert.ToInt32(Regex.Match(log.Message, @"#(\d+) ").Groups[1].Value); continue; } if (log.Message.Contains("falls asleep")) { fellAsleep = log.TimeStamp.Minute; continue; } if (log.Message.Contains("wakes up")) { var wokeUp = log.TimeStamp.Minute; if (!sleepLog.MinuteAsleep.ContainsKey(currentId)) { sleepLog.MinuteAsleep.Add(currentId, new Dictionary <int, int>()); } for (var t = fellAsleep; t < wokeUp; t++) { if (!sleepLog.MinuteAsleep[currentId].ContainsKey(t)) { sleepLog.MinuteAsleep[currentId].Add(t, 0); } sleepLog.MinuteAsleep[currentId][t]++; } if (!sleepLog.TotalSleep.ContainsKey(currentId)) { sleepLog.TotalSleep.Add(currentId, 0); } sleepLog.TotalSleep[currentId] += wokeUp - fellAsleep; } } return(sleepLog); }
public static int Strategy2(SleepLog sleepLog) { // (Guard ID : Most frequent minute var guardMax = new Dictionary <int, int>(); foreach (var guard in sleepLog.MinuteAsleep.Keys) { var mostFrequentMinute = sleepLog.MinuteAsleep[guard].Aggregate((l, r) => l.Value > r.Value ? l : r).Value; guardMax.Add(guard, mostFrequentMinute); } var winner = guardMax.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; return(winner * sleepLog.MinuteAsleep[winner].Aggregate((l, r) => l.Value > r.Value ? l : r).Key); }