Esempio n. 1
0
        private void BuildGuardRecords(ref Dictionary <int, GuardRecord> records, ref TimedAction[] actions)
        {
            TimedAction lastAction  = new TimedAction();
            int         activeGuard = 0;

            foreach (TimedAction action in actions)
            {
                switch (action.action)
                {
                case GuardAction.FallsAsleep: break;

                case GuardAction.WakesUp:
                    UpdateGuardRecord(ref records, activeGuard, lastAction.time.Minute, action.time.Minute);
                    break;

                case GuardAction.StartShift: activeGuard = action.guardNumber; break;
                }

                lastAction = action;
            }

            // Take a copy of the keys so we can update the dictionary for each one
            List <int> keys = new List <int>(records.Keys);

            foreach (int key in keys)
            {
                GuardRecord record = records[key];
                record.sleepiestMinuteAndCount = GetCount(ref records, key);
                records[key] = record;
            }
        }
Esempio n. 2
0
        private void UpdateGuardRecord(ref Dictionary <int, GuardRecord> records, int guardNum, int startTime, int endTime)
        {
            GuardRecord record = new GuardRecord();

            if (records.ContainsKey(guardNum))
            {
                record = records[guardNum];
            }
            record.totalMinutesAsleep += endTime - startTime;

            for (int minute = startTime; minute < endTime; ++minute)
            {
                record.minutesAsleep.Add(minute);
            }

            records[guardNum] = record;
        }