private static KeyValuePair <int, int> CalculateMaximumSleepMinute(IEnumerable <ActivityRecord> guard) { var sleepingMinutes = new Dictionary <int, int>(); foreach (ActivityRecord sleepRecord in guard) { for (var i = 0; i < 60; i++) { for (var j = 0; j < sleepRecord.FallsAsleep.Count; j++) { if (i >= sleepRecord.FallsAsleep[j].Minute && i < sleepRecord.WakesUp[j].Minute) { if (sleepingMinutes.ContainsKey(i)) { sleepingMinutes[i]++; } else { sleepingMinutes.Add(i, 1); } } } } } var maxBy = MaxByExtension.MaxBy(sleepingMinutes, m => m.Value); return(maxBy.FirstOrDefault()); }
public string SolvePart1() { IGrouping <int, ActivityRecord>?sleepiestGuard = MaxByExtension.MaxBy(_guardSleepRecords, g => g.ToList().Sum(s => s.TimeAsleep)).FirstOrDefault(); KeyValuePair <int, int> maxMinutes = CalculateMaximumSleepMinute(sleepiestGuard); return($"Guard #{sleepiestGuard.Key}: Most asleep at 00:{maxMinutes.Key}."); }
public string SolvePart1() { var allLocations = new List <Location>(); foreach (var i in Input) { string[] split = i.Split(','); allLocations.Add(new Location(int.Parse(split[0]), int.Parse(split[1]))); } int xBoundary = allLocations.Max(l => l.X); int yBoundary = allLocations.Max(l => l.Y); var currentLocation = new Location(0, 0); while (currentLocation.X <= xBoundary && currentLocation.Y <= yBoundary) { Dictionary <Location, int> distances = allLocations.ToDictionary(location => location, location => CalculateManhattanDistance(currentLocation, location)); var sortedDistances = MinByExtension.MinBy(distances, d => d.Value); KeyValuePair <Location, int> shortestDistance = sortedDistances.First(); if (sortedDistances.Count() == 1 || sortedDistances.Count(s => s.Value == shortestDistance.Value) == 1) { // Only add if the current location is closest to a single point. var newLocation = new Location(currentLocation.X, currentLocation.Y) { IsInfinite = currentLocation.X == 0 || currentLocation.Y == 0 || currentLocation.X == xBoundary || currentLocation.Y == yBoundary }; allLocations.SingleOrDefault(l => l == shortestDistance.Key)?.ClosestLocations.Add(newLocation); } // Move to the next location if (currentLocation.Y == yBoundary) { currentLocation.X++; currentLocation.Y = 0; } else { currentLocation.Y++; } } List <Location> nonInfiniteLocations = allLocations.Where(l => l.ClosestLocations.All(c => !c.IsInfinite)).ToList(); Location? largestNonInfinite = MaxByExtension.MaxBy(nonInfiniteLocations, l => l.ClosestLocations.Count).First(); return($"The largest non-infinite area is {largestNonInfinite.ClosestLocations.Count}"); }
public string SolvePart2() { var mostAsleep = new Dictionary <int, KeyValuePair <int, int> >(); foreach (var sleepRecord in _guardSleepRecords) { KeyValuePair <int, int> maxSleepMinute = CalculateMaximumSleepMinute(sleepRecord); mostAsleep.Add(sleepRecord.Key, maxSleepMinute); } (int guardId, KeyValuePair <int, int> asleepTime) = MaxByExtension.MaxBy(mostAsleep, v => v.Value.Value).SingleOrDefault(); return($"Guard #{guardId}: Most asleep at 00:{asleepTime.Key}."); }
public string SolvePart2() { IEnumerable <char> distinctUnitTypes = Input.ToUpper().ToCharArray().ToList().Distinct(); var polymerOptions = new Dictionary <string, int>(); // Key: Removed Unit Type, Value: Collapsed Length foreach (char unitType in distinctUnitTypes) { var unit = unitType.ToString(); List <char> improvedPolymer = Input.Replace(unit.ToUpper(), string.Empty).Replace(unit.ToLower(), string.Empty).ToCharArray().ToList(); IList <char> collapsedPolymer = ReactPolymer(improvedPolymer); polymerOptions.Add(unit, collapsedPolymer.Count); } KeyValuePair <string, int> optimalPolymer = MaxByExtension.MaxBy(polymerOptions, o => o.Value).FirstOrDefault(); return($"Part 2: {optimalPolymer.Value}"); }