Beispiel #1
0
        int IComparable.CompareTo(object obj)
        {
            GuardLog g = (GuardLog)obj;

            return(date.CompareTo(g.date));
        }
Beispiel #2
0
        public void ParseInput(string path)
        {
            if (parsed)
            {
                return;
            }

            StreamReader reader = new StreamReader(path);
            string       line;

            while ((line = reader.ReadLine()) != null)
            {
                string[] stringSplit = line.Split(']');
                DateTime dt;
                DateTime.TryParse(stringSplit[0].Trim('['), out dt);

                GuardLog log = new GuardLog
                {
                    date  = dt,
                    entry = stringSplit[1].Trim()
                };

                logs.Add(log);
            }

            reader.Close();

            var sortedLogs = logs.OrderBy(logDate => logDate.date);

            int id        = -1;
            int minAsleep = -1;
            int minAwake  = -1;

            foreach (GuardLog log in sortedLogs)
            {
                if (log.entry.Contains('#'))
                {
                    id = int.Parse(log.entry.Split('#')[1].Split(' ')[0]);

                    if (profiles.All(p => p.id != id))
                    {
                        GuardProfile guard = new GuardProfile
                        {
                            id          = id,
                            rangeAsleep = new List <int>()
                        };

                        profiles.Add(guard);
                    }
                }
                else if (log.entry.Contains("asleep"))
                {
                    minAsleep = log.date.Minute;
                }
                else if (log.entry.Contains("wakes"))
                {
                    if (id == -1 || minAsleep == -1)
                    {
                        throw new ArgumentException("Order of logs is wrong");
                    }

                    minAwake = log.date.Minute;

                    GuardProfile guard = profiles.First(g => g.id == id);

                    int sleepRange = (minAwake - minAsleep);
                    guard.minutesSlept += sleepRange;
                    guard.rangeAsleep.AddRange(Enumerable.Range(minAsleep, sleepRange));

                    minAsleep = -1;
                    minAwake  = -1;
                }
            }
            parsed = true;
        }