Code for Day04 part 2

This commit is contained in:
2018-12-04 10:35:24 +01:00
parent 834b057437
commit a61446686f
2 changed files with 86 additions and 1 deletions

View File

@@ -60,5 +60,62 @@ namespace AdventOfCode2018.Tests
Assert.AreEqual("240", result); 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);
}
} }
} }

View File

@@ -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.) 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 public class Day04
{ {
@@ -98,7 +106,27 @@ namespace AdventOfCode2018
public string ResolvePart2(string[] inputs) public string ResolvePart2(string[] inputs)
{ {
return null; List<GuardEvent> guardEvents = GuardEvent.FromStringArray(inputs);
Dictionary<int, GuardSleepHistogram> 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<int, GuardSleepHistogram> BuildFullHistorgram(List<GuardEvent> guardEvents) private static Dictionary<int, GuardSleepHistogram> BuildFullHistorgram(List<GuardEvent> guardEvents)