예제 #1
0
        // == == == == == Puzzle 1 == == == == ==
        public static int Puzzle1(string input)
        {
            var records = new List <Record>();

            // Parse input
            foreach (var rawRecord in Common.ParseStringArray(input))
            {
                var record = new Record(rawRecord);
                records.Add(record);
            }
            records.Sort((x, y) => x.Time.CompareTo(y.Time));
            // Create ShiftInfos
            var        guardStats = new Dictionary <int, GuardStats>();
            GuardStats gs;
            ShiftInfo  si = null;

            foreach (var record in records)
            {
                // Shift Start
                if (record.Type == Record.RecordType.ShiftStart)
                {
                    // Next guard
                    si = new ShiftInfo(record);
                    if (guardStats.TryGetValue(record.ID, out gs))
                    {
                        gs.AddShift(si);
                    }
                    else
                    {
                        gs    = new GuardStats();
                        gs.ID = si.ID;
                        gs.AddShift(si);
                        guardStats.Add(gs.ID, gs);
                    }
                }
                // FallAsleep / WakeUp
                else
                {
                    si.AddRecord(record);
                }
            }
            // Find sleepiest guard
            int maxAsleepMin = 0, maxAsleepID = 0;

            foreach (var guard in guardStats)
            {
                var minSleep = guard.Value.MinutesAsleep;
                if (minSleep > maxAsleepMin)
                {
                    maxAsleepMin = minSleep;
                    maxAsleepID  = guard.Value.ID;
                }
            }

            return(guardStats[maxAsleepID].FindSleepiestMinute(out int noNeed) * maxAsleepID);
        }
예제 #2
0
        // == == == == == Puzzle 2 == == == == ==
        public static int Puzzle2(string input)
        {
            // CopyPaste from Puzzle 1
            var records = new List <Record>();

            // Parse input
            foreach (var rawRecord in Common.ParseStringArray(input))
            {
                var record = new Record(rawRecord);
                records.Add(record);
            }
            records.Sort((x, y) => x.Time.CompareTo(y.Time));
            // Create ShiftInfos
            var        guardStats = new Dictionary <int, GuardStats>();
            GuardStats gs;
            ShiftInfo  si = null;

            foreach (var record in records)
            {
                // Shift Start
                if (record.Type == Record.RecordType.ShiftStart)
                {
                    // Next guard
                    si = new ShiftInfo(record);
                    if (guardStats.TryGetValue(record.ID, out gs))
                    {
                        gs.AddShift(si);
                    }
                    else
                    {
                        gs    = new GuardStats();
                        gs.ID = si.ID;
                        gs.AddShift(si);
                        guardStats.Add(gs.ID, gs);
                    }
                }
                // FallAsleep / WakeUp
                else
                {
                    si.AddRecord(record);
                }
            }

            // Puzzle 2 modifications
            int asleepTimesMax = 0, asleepMinuteMax = 0, asleepIDMax = 0;   // Max values
            int asleepTimes, asleepMinute;

            foreach (var guard in guardStats)
            {
                // Need to get this value to populate array on Shift objects
                // Should be rewritten to calculate all stats on GuardStats class, then access as variables
                var noNeed = guard.Value.MinutesAsleep;

                asleepMinute = guard.Value.FindSleepiestMinute(out asleepTimes);
                if (asleepTimes > asleepTimesMax)
                {
                    asleepTimesMax  = asleepTimes;
                    asleepMinuteMax = asleepMinute;
                    asleepIDMax     = guard.Value.ID;
                }
            }
            return(asleepMinuteMax * asleepIDMax);
        }