public static GuardEvent FromString(string strEvent) { GuardEvent guardEvent = new GuardEvent(); string[] parts = strEvent.Split(new string[] { "[", "-", " ", ":", "]", "#", }, StringSplitOptions.RemoveEmptyEntries); guardEvent.Date = new DateTime( Convert.ToInt32(parts[0]), Convert.ToInt32(parts[1]), Convert.ToInt32(parts[2]), Convert.ToInt32(parts[3]), Convert.ToInt32(parts[4]), 0 ); if (parts[5] == "Guard") { guardEvent.ID = Convert.ToInt32(parts[6]); guardEvent.Type = GuardEventType.ShiftBegin; } if (parts[5] == "falls") { guardEvent.Type = GuardEventType.FallSleep; } if (parts[5] == "wakes") { guardEvent.Type = GuardEventType.WakeUp; } return(guardEvent); }
public string ResolvePart2(string[] inputs) { List <GuardEvent> guardEvents = GuardEvent.FromStringArray(inputs); Dictionary <int, GuardSleepHistogram> dictFullHistogram = BuildFullHistorgram(guardEvents); int selectedGuardID = int.MinValue; int selectedMinute = int.MinValue; int maxSleepMinuteValue = int.MinValue; for (int i = 0; i < GuardSleepHistogram.MinutesOnHour; i++) { foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values) { if (guardHistogram.SleepOnMunute[i] > maxSleepMinuteValue) { maxSleepMinuteValue = guardHistogram.SleepOnMunute[i]; selectedGuardID = guardHistogram.ID; selectedMinute = i; } } } int result = selectedGuardID * selectedMinute; return(result.ToString()); }
public string ResolvePart1(string[] inputs) { List <GuardEvent> guardEvents = GuardEvent.FromStringArray(inputs); Dictionary <int, GuardSleepHistogram> dictFullHistogram = BuildFullHistorgram(guardEvents); // Find sleepier guard GuardSleepHistogram highestSleeperHistogram = null; long highestTotalSleep = long.MinValue; foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values) { int totalSleep = guardHistogram.SleepOnMunute.Sum(); if (totalSleep > highestTotalSleep) { highestSleeperHistogram = guardHistogram; highestTotalSleep = totalSleep; } } // Find sleepier minute int maxSleepMinute = int.MinValue; int maxSleepMinuteValue = int.MinValue; for (int i = 0; i < GuardSleepHistogram.MinutesOnHour; i++) { if (highestSleeperHistogram.SleepOnMunute[i] > maxSleepMinuteValue) { maxSleepMinute = i; maxSleepMinuteValue = highestSleeperHistogram.SleepOnMunute[i]; } } int result = highestSleeperHistogram.ID * maxSleepMinute; return(result.ToString()); }