diff --git a/AdventOfCode2018.Tests/Day04_Tests.cs b/AdventOfCode2018.Tests/Day04_Tests.cs index adde5da..a5efcd2 100644 --- a/AdventOfCode2018.Tests/Day04_Tests.cs +++ b/AdventOfCode2018.Tests/Day04_Tests.cs @@ -60,5 +60,62 @@ namespace AdventOfCode2018.Tests Assert.AreEqual("240", result); } + + + [TestMethod()] + public void ResolvePart2__BaseStatement() + { + Day04 day04 = new Day04(); + + string result = day04.ResolvePart2(new string[] { + "[1518-11-01 00:00] Guard #10 begins shift", + "[1518-11-01 00:05] falls asleep", + "[1518-11-01 00:25] wakes up", + "[1518-11-01 00:30] falls asleep", + "[1518-11-01 00:55] wakes up", + "[1518-11-01 23:58] Guard #99 begins shift", + "[1518-11-02 00:40] falls asleep", + "[1518-11-02 00:50] wakes up", + "[1518-11-03 00:05] Guard #10 begins shift", + "[1518-11-03 00:24] falls asleep", + "[1518-11-03 00:29] wakes up", + "[1518-11-04 00:02] Guard #99 begins shift", + "[1518-11-04 00:36] falls asleep", + "[1518-11-04 00:46] wakes up", + "[1518-11-05 00:03] Guard #99 begins shift", + "[1518-11-05 00:45] falls asleep", + "[1518-11-05 00:55] wakes up", + }); + + Assert.AreEqual("4455", result); + } + + [TestMethod()] + public void ResolvePart2__BaseStatementUnsorted() + { + Day04 day04 = new Day04(); + + string result = day04.ResolvePart2(new string[] { + "[1518-11-04 00:36] falls asleep", + "[1518-11-04 00:46] wakes up", + "[1518-11-05 00:03] Guard #99 begins shift", + "[1518-11-01 00:05] falls asleep", + "[1518-11-01 00:25] wakes up", + "[1518-11-02 00:40] falls asleep", + "[1518-11-01 00:30] falls asleep", + "[1518-11-03 00:29] wakes up", + "[1518-11-02 00:50] wakes up", + "[1518-11-03 00:05] Guard #10 begins shift", + "[1518-11-03 00:24] falls asleep", + "[1518-11-01 00:00] Guard #10 begins shift", + "[1518-11-04 00:02] Guard #99 begins shift", + "[1518-11-05 00:45] falls asleep", + "[1518-11-01 00:55] wakes up", + "[1518-11-01 23:58] Guard #99 begins shift", + "[1518-11-05 00:55] wakes up", + }); + + Assert.AreEqual("4455", result); + } } } \ No newline at end of file diff --git a/AdventOfCode2018/Day04.cs b/AdventOfCode2018/Day04.cs index c132faf..565a405 100644 --- a/AdventOfCode2018/Day04.cs +++ b/AdventOfCode2018/Day04.cs @@ -58,6 +58,14 @@ namespace AdventOfCode2018 What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 10 * 24 = 240.) + --- Part Two --- + + Strategy 2: Of all guards, which guard is most frequently asleep on the same minute? + + In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.) + + What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.) + */ public class Day04 { @@ -98,7 +106,27 @@ namespace AdventOfCode2018 public string ResolvePart2(string[] inputs) { - return null; + List guardEvents = GuardEvent.FromStringArray(inputs); + Dictionary dictFullHistogram = BuildFullHistorgram(guardEvents); + + int selectedGuardID = int.MinValue; + int selectedMinute = int.MinValue; + int maxSleepMinuteValue = int.MinValue; + for (int i = 0; i < GuardSleepHistogram.MinutesOnHour; i++) + { + foreach (GuardSleepHistogram guardHistogram in dictFullHistogram.Values) + { + if (guardHistogram.SleepOnMunute[i] > maxSleepMinuteValue) + { + maxSleepMinuteValue = guardHistogram.SleepOnMunute[i]; + selectedGuardID = guardHistogram.ID; + selectedMinute = i; + } + } + } + + int result = selectedGuardID * selectedMinute; + return result.ToString(); } private static Dictionary BuildFullHistorgram(List guardEvents)