Created
December 4, 2018 08:22
-
-
Save jyarbro/b1f364edbd9bcf3d4995c22782b83125 to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Day4_2018 { | |
const string INPUTFILE = @"2018\day4.txt"; | |
public void Part1() { | |
var guardSleepMinutes = LoadGuardSleepMinutes(); | |
var guardSleepTotals = new Dictionary<int, int>(); | |
foreach (var guard in guardSleepMinutes) { | |
if (!guardSleepTotals.ContainsKey(guard.Key)) { | |
guardSleepTotals.Add(guard.Key, 0); | |
} | |
foreach (var minute in guard.Value) { | |
guardSleepTotals[guard.Key] += minute; | |
} | |
} | |
var worstGuard = (from guard in guardSleepTotals | |
orderby guard.Value descending | |
select guard.Key).First(); | |
var worstMinute = -1; | |
for (var i = 0; i < 60; i++) { | |
if (guardSleepMinutes[worstGuard][i] > worstMinute) { | |
worstMinute = i; | |
} | |
} | |
Console.WriteLine($"Guard {worstGuard} * Minute {worstMinute} = {worstGuard * worstMinute}"); | |
} | |
public void Part2() { | |
var guardSleepMinutes = LoadGuardSleepMinutes(); | |
var worstGuard = 0; | |
var worstMinute = 0; | |
var worstMinuteTotal = 0; | |
foreach (var guard in guardSleepMinutes) { | |
for (var i = 0; i < 60; i++) { | |
if (guard.Value[i] > worstMinuteTotal) { | |
worstMinuteTotal = guard.Value[i]; | |
worstMinute = i; | |
worstGuard = guard.Key; | |
} | |
} | |
} | |
Console.WriteLine($"Guard {worstGuard} * Minute {worstMinute} = {worstGuard * worstMinute}"); | |
} | |
Dictionary<int, int[]> LoadGuardSleepMinutes() { | |
var logs = LoadLogs(); | |
var guardSleepMinutes = new Dictionary<int, int[]>(); | |
var currentGuard = 0; | |
var sleepStart = 0; | |
var sleepEnd = 0; | |
foreach (var log in logs) { | |
var match = Regex.Match(log.Event, @"Guard #(\d+)? begins shift"); | |
if (match.Success) { | |
currentGuard = Convert.ToInt32(match.Groups[1].Value); | |
if (!guardSleepMinutes.ContainsKey(currentGuard)) { | |
guardSleepMinutes.Add(currentGuard, new int[60]); | |
} | |
} | |
else if (log.Event == "falls asleep") { | |
sleepStart = log.Timestamp.Minute; | |
} | |
else { | |
sleepEnd = log.Timestamp.Minute; | |
for (var i = sleepStart; i < sleepEnd; i++) { | |
guardSleepMinutes[currentGuard][i]++; | |
} | |
} | |
} | |
return guardSleepMinutes; | |
} | |
List<Log> LoadLogs() { | |
var input = File.ReadAllLines(INPUTFILE); | |
var logs = new List<Log>(); | |
foreach (var line in input) { | |
var match = Regex.Match(line, @"\[(?<datetime>\d{4}-\d{2}-\d{2} \d{2}:\d{2})] (?<event>.+)"); | |
if (!match.Success) { | |
throw new Exception("Your regex is wrong"); | |
} | |
var timestamp = DateTime.ParseExact(match.Groups["datetime"].Value, "yyyy-MM-dd H:mm", CultureInfo.InvariantCulture); | |
logs.Add(new Log(timestamp, match.Groups["event"].Value)); | |
} | |
return logs.OrderBy(l => l.Timestamp).ToList(); | |
} | |
class Log { | |
public DateTime Timestamp { get; set; } | |
public string Event { get; set; } | |
public Log(DateTime timestamp, string eventText) { | |
Timestamp = timestamp; | |
Event = eventText; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment