コード例 #1
0
 public void AddHistogram(GuardSleepHistogram histogram)
 {
     for (int i = 0; i < MinutesOnHour; i++)
     {
         SleepOnMunute[i] += histogram.SleepOnMunute[i];
     }
 }
コード例 #2
0
        private static Dictionary <int, GuardSleepHistogram> BuildFullHistorgram(List <GuardEvent> guardEvents)
        {
            Dictionary <int, GuardSleepHistogram> dictFullHistogram = new Dictionary <int, GuardSleepHistogram>();

            foreach (IGrouping <int, GuardEvent> group in guardEvents.GroupBy(guardEvent => guardEvent.Date.DayOfYear))
            {
                Dictionary <int, GuardSleepHistogram> dictDayHistogram = new Dictionary <int, GuardSleepHistogram>();
                foreach (GuardEvent guardEvent in group)
                {
                    if (guardEvent.ID == null)
                    {
                        continue;
                    }
                    GuardSleepHistogram dayGuardHistogram = null;
                    if (dictDayHistogram.ContainsKey((int)guardEvent.ID))
                    {
                        dayGuardHistogram = dictDayHistogram[(int)guardEvent.ID];
                    }
                    else
                    {
                        dayGuardHistogram = new GuardSleepHistogram {
                            ID = (int)guardEvent.ID
                        };
                        dictDayHistogram.Add(dayGuardHistogram.ID, dayGuardHistogram);
                    }
                    if (guardEvent.Type == GuardEventType.FallSleep)
                    {
                        dayGuardHistogram.FallSleep(guardEvent.Date.Minute);
                    }
                    if (guardEvent.Type == GuardEventType.WakeUp)
                    {
                        dayGuardHistogram.WakeUp(guardEvent.Date.Minute);
                    }
                }

                foreach (GuardSleepHistogram dayGuardHistogram in dictDayHistogram.Values)
                {
                    GuardSleepHistogram guardHistogram = null;
                    if (dictFullHistogram.ContainsKey(dayGuardHistogram.ID))
                    {
                        guardHistogram = dictFullHistogram[dayGuardHistogram.ID];
                        guardHistogram.AddHistogram(dayGuardHistogram);
                    }
                    else
                    {
                        dictFullHistogram.Add(dayGuardHistogram.ID, dayGuardHistogram);
                    }
                }
            }

            return(dictFullHistogram);
        }
コード例 #3
0
        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());
        }