コード例 #1
0
        public List <GuardLog> Parse(IEnumerable <string> inputs)
        {
            var logs = new List <GuardLog>();

            foreach (var input in inputs)
            {
                var log = new GuardLog();
                if (input.EndsWith("shift"))
                {
                    var parts = input.Split(new [] { "[", "] Guard #", " begins shift" }, StringSplitOptions.RemoveEmptyEntries);
                    log.Id        = Int32.Parse(parts[1]);
                    log.Timestamp = DateTime.Parse(parts[0]);
                    log.Event     = EventType.BeginShift;
                }
                else if (input.EndsWith("asleep"))
                {
                    var parts = input.Split(new [] { "[", "] falls asleep" }, StringSplitOptions.RemoveEmptyEntries);
                    log.Timestamp = DateTime.Parse(parts[0]);
                    log.Event     = EventType.FallsAsleep;
                }
                else
                {
                    var parts = input.Split(new [] { "[", "] wakes up" }, StringSplitOptions.RemoveEmptyEntries);
                    log.Timestamp = DateTime.Parse(parts[0]);
                    log.Event     = EventType.WakesUp;
                }
                logs.Add(log);
            }

            logs.Sort((a, b) => a.Timestamp.CompareTo(b.Timestamp));
            return(logs);
        }
コード例 #2
0
        public void Puzzle2_FindGuardAsleepTheSameMinuteTheMost()
        {
            var log = new GuardLog(Input.Day04Parse(Input.Day04));

            var(guard, minute, _) = log.Strategy2();

            (guard * minute).Should().Be(5705);
        }
コード例 #3
0
        public void Puzzle1_FindGuardAsleepTheMost_AndMinuteMostAsleep()
        {
            var log = new GuardLog(Input.Day04Parse(Input.Day04));

            var(guard, minute) = log.Strategy1();

            (guard * minute).Should().Be(14346);
        }
コード例 #4
0
        public void Puzzle2Example_LocateGuardWhoSleptTheMostDuringTheSameMinute()
        {
            var input = Input.Day04Parse(PuzzleExample);
            var log   = new GuardLog(input);

            var(guard, minute, _) = log.Strategy2();

            guard.Should().Be(99);
            minute.Should().Be(45);
        }
コード例 #5
0
        public void Puzzle1Example_LocateGuardWhosAsleepMostMinutesCorrectly()
        {
            var input = Input.Day04Parse(PuzzleExample);
            var log   = new GuardLog(input);

            var(guard, minute) = log.Strategy1();

            guard.Should().Be(10);
            minute.Should().Be(24);
        }
コード例 #6
0
        static void ExecutePartTwo(string[] input)
        {
            //Create helper variables
            int   guardId   = 0;
            int   minAsleep = 0;
            int   minAwake  = 0;
            Match timestampMatches;
            var   guardLog      = new Dictionary <DateTime, GuardLog>();
            var   guardLogEntry = new GuardLog();


            // Parse input and create guardLog
            foreach (string line in input)
            {
                Regex getTimestamp = new Regex(@"\[(.{16})\].*(#([0-9]*)|falls|wakes)");
                timestampMatches = getTimestamp.Match(line);
                guardLogEntry    = new GuardLog
                {
                    matchType = timestampMatches.Groups[2].Value,
                    guardID   = Helpers.StringsAndInts.ConvertStringToInt(timestampMatches.Groups[3].Value)
                };
                guardLog.Add(DateTime.Parse(timestampMatches.Groups[1].Value), guardLogEntry);
            }

            // Order input by datetime
            var orderedGuardLog = new SortedDictionary <DateTime, GuardLog>(guardLog);


            //Create "sleepy" array with guardID, minute he fell asleep and how many times he was asleep in that minute
            var sleepy = new List <SleepyList>();

            //Loop trough input
            var kvp = orderedGuardLog.ToList();

            for (int c = 1; c < kvp.Count; c++)
            {
                //If start of shift place GuardID in var
                if (kvp[c].Value.guardID != 0)
                {
                    guardId = kvp[c].Value.guardID;
                    c++;
                    minAsleep = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                    c++;
                    minAwake = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                }


                //If falls asleep then place starting minute in var
                if (kvp[c].Value.matchType == "falls")
                {
                    minAsleep = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                    c++;
                    minAwake = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                }



                //If both asleep and wakeup var are not NULL then create a entry or add 1 to times asleep
                //in the "sleepy" array for every minute from asleep untill awake (asleep minute counts, awake minute does not count)
                if (guardId != 0 && minAsleep != 0 && minAwake != 0)
                {
                    for (int i = minAsleep; i < minAwake; i++)
                    {
                        // Zoek naar een record voor deze guard op deze minuut
                        var rec = sleepy.FirstOrDefault(x => x.guardId == guardId && x.minuteId == i);

                        // Als record gevonden is update deze dan.
                        if (rec != null)
                        {
                            rec.repeats++;
                        }

                        // Als geen record gevonden is maak deze dan aan
                        if (rec == null)
                        {
                            var newData = new SleepyList
                            {
                                guardId  = guardId,
                                minuteId = i,
                                repeats  = 1
                            };
                            sleepy.Add(newData);
                        }
                    }

                    //Reset vars
                    minAsleep = 0;
                    minAwake  = 0;
                }
            }


            //Get the minute with highest value
            var sleepySorted = sleepy.OrderByDescending(x => x.repeats).First();

            // Return the sum
            Console.WriteLine("Guard " + sleepySorted.guardId + " * Minute " + sleepySorted.minuteId + " = " + sleepySorted.guardId * sleepySorted.minuteId);
        }
コード例 #7
0
        static void ExecutePartOne(string[] input)
        {
            //Create helper variables
            int   guardId    = 0;
            int   minAsleep  = 0;
            int   minAwake   = 0;
            bool  actionDone = false;
            Match timestampMatches;
            var   guardLog      = new Dictionary <DateTime, GuardLog>();
            var   guardLogEntry = new GuardLog();


            // Parse input and create guardLog
            foreach (string line in input)
            {
                Regex getTimestamp = new Regex(@"\[(.{16})\].*(#([0-9]*)|falls|wakes)");
                timestampMatches = getTimestamp.Match(line);
                guardLogEntry    = new GuardLog
                {
                    matchType = timestampMatches.Groups[2].Value,
                    guardID   = Helpers.StringsAndInts.ConvertStringToInt(timestampMatches.Groups[3].Value)
                };
                guardLog.Add(DateTime.Parse(timestampMatches.Groups[1].Value), guardLogEntry);
            }

            // Order input by datetime
            var orderedGuardLog = new SortedDictionary <DateTime, GuardLog>(guardLog);


            //Create "sleepy" array with guardID, minute he fell asleep and how many times he was asleep in that hour
            var sleepy = new Dictionary <int, Dictionary <int, int> >();
            // GuardId and number of minutes slept
            var guards = new Dictionary <int, int>();


            //Loop trough input
            var kvp = orderedGuardLog.ToList();

            for (int c = 1; c < kvp.Count; c++)
            {
                //If start of shift place GuardID in var
                if (kvp[c].Value.guardID != 0)
                {
                    guardId = kvp[c].Value.guardID;
                    c++;
                    minAsleep = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                    c++;
                    minAwake = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                }


                //If falls asleep then place starting minute in var
                if (kvp[c].Value.matchType == "falls")
                {
                    minAsleep = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                    c++;
                    minAwake = Helpers.StringsAndInts.ConvertStringToInt(kvp[c].Key.ToString("mm"));
                }



                //If both asleep and wakeup var are not NULL then create a entry or add 1 to times asleep
                //in the "sleepy" array for every minute from asleep untill awake (asleep minute counts, awake minute does not count)
                if (guardId != 0 && minAsleep != 0 && minAwake != 0)
                {
                    // if entry for this guard does not exist create one
                    if (!sleepy.ContainsKey(guardId))
                    {
                        sleepy.Add(guardId, new Dictionary <int, int>());
                    }

                    // if entry for this guard does not exist create one
                    if (!guards.ContainsKey(guardId))
                    {
                        guards.Add(guardId, 0);
                    }

                    for (int i = minAsleep; i < minAwake; i++)
                    {
                        // if entry for this minute exists add one to value
                        if (sleepy[guardId].ContainsKey(i))
                        {
                            sleepy[guardId][i]++;
                        }
                        else // create entry
                        {
                            sleepy[guardId].Add(i, 1);
                        }

                        // Add a minute slept to the guard
                        guards[guardId]++;
                    }

                    //Reset vars
                    minAsleep = 0;
                    minAwake  = 0;
                }
            }

            //After loop completed get total minute slept per guard and select guard with highest amount
            var guardsordered = guards.OrderByDescending(x => x.Value).First();

            //Get the minute with highest value for this guard and return this value
            var sleepySorted = sleepy[guardsordered.Key].OrderByDescending(x => x.Value).First();

            // Return the sum
            Console.WriteLine("Guard " + guardsordered.Key + " * Minute " + sleepySorted.Key + " = " + guardsordered.Key * sleepySorted.Key);
        }
コード例 #8
0
ファイル: Guard.cs プロジェクト: akrisiun/git-dot-aspx
 static Guard()
 {
     Log = new GuardLog();
 }