Exemplo n.º 1
0
        public MostSleptMinute GetMostSleptMinute()
        {
            MostSleptMinute retVal = new MostSleptMinute(0, 0);

            foreach (KeyValuePair <int, int> kvp in MinuteData)
            {
                if (kvp.Value > retVal.Duration)
                {
                    retVal.Minute   = kvp.Key;
                    retVal.Duration = kvp.Value;
                }
            }
            return(retVal);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string[] inputs = System.IO.File.ReadAllLines(@"..\..\..\input.txt");

            List <LogEntry>         sortedlog   = new List <LogEntry>();
            Dictionary <int, Guard> GuardLookup = new Dictionary <int, Guard>();

            foreach (string input in inputs)
            {
                sortedlog.Add(new LogEntry(input));
            }
            sortedlog.Sort(delegate(LogEntry l1, LogEntry l2) { return(l1.TimeStamp.CompareTo(l2.TimeStamp)); });

            int previousID = 0;

            foreach (LogEntry le in sortedlog)
            {
                if (le.Message.Contains("wakes up"))
                {
                    GuardLookup[previousID].WakesUp(le.TimeStamp);
                }
                else if (le.Message.Contains("falls asleep"))
                {
                    GuardLookup[previousID].FallsAsleep(le.TimeStamp);
                }
                else
                {
                    Regex           findID = new Regex(@".*#(\d*).*");
                    MatchCollection mc     = findID.Matches(le.Message);
                    foreach (Match m in mc)
                    {
                        GroupCollection g = m.Groups;
                        previousID = int.Parse(g[1].Value);
                        if (!GuardLookup.ContainsKey(previousID))
                        {
                            GuardLookup.Add(previousID, new Guard(previousID));
                        }
                    }
                }
            }
            Guard           SleepsTheMost  = null;
            Guard           MostConsistent = null;
            MostSleptMinute msm            = new MostSleptMinute(0, 0);

            foreach (KeyValuePair <int, Guard> kvp in GuardLookup)
            {
                if (SleepsTheMost == null)
                {
                    SleepsTheMost = kvp.Value;
                }
                else if (SleepsTheMost.TotalMinutesSlept < kvp.Value.TotalMinutesSlept)
                {
                    SleepsTheMost = kvp.Value;
                }
                if (MostConsistent == null)
                {
                    MostConsistent = kvp.Value;
                }
                else if (kvp.Value.GetMostSleptMinute().Duration > msm.Duration)
                {
                    msm            = kvp.Value.GetMostSleptMinute();
                    MostConsistent = kvp.Value;
                }
            }
            KeyValuePair <int, int> MostSleptMinute = new KeyValuePair <int, int>(0, 0);

            foreach (KeyValuePair <int, int> kvp in SleepsTheMost.MinuteData)
            {
                if (kvp.Value > MostSleptMinute.Value)
                {
                    MostSleptMinute = kvp;
                }
            }
            Console.WriteLine(string.Format("Part1:{0}", (MostSleptMinute.Key * SleepsTheMost.ID)));
            Console.WriteLine(string.Format("Part2:{0}", (MostConsistent.ID * msm.Minute)));
        }