From 8c97fff459ec826f58a69125a8353550d57e77ab Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 17 Dec 2021 11:49:03 -0600 Subject: [PATCH] Cut day 17 run time in half You'd think I'd have learned my lesson about Dictionaries by now. They're great, but only for sparse input sets or where lookup needs to be as fast as possible and other access types are less important. --- src/17.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/17.cs b/src/17.cs index 7aa8a95..156c167 100644 --- a/src/17.cs +++ b/src/17.cs @@ -64,26 +64,26 @@ internal class Day17 : Day return (pt.x, pt.y); } - private static void Part1(Dictionary<(int x, int y), int> successes) + private static void Part1(List<((int x, int y) pt, int height)> successes) { - var max = successes.MaxBy(vel => vel.Value); - var numOthers = successes.Count(vel => vel.Value == max.Value); + var max = successes.MaxBy(vel => vel.height); + var numOthers = successes.Count(vel => vel.height == max.height); - Logger.Log($"<+black>> part1: highest Y was at velocity {max.Key} (and {numOthers} other{(numOthers == 1 ? "" : "s")}): <+white>{max.Value}"); + Logger.Log($"<+black>> part1: highest Y was at velocity {max.pt} (and {numOthers} other{(numOthers == 1 ? "" : "s")}): <+white>{max.height}"); } - private static void Part2(Dictionary<(int x, int y), int> successes) + private static void Part2(List<((int x, int y) pt, int height)> successes) { Logger.Log($"<+black>> part2: #successful velocities: <+white>{successes.Count}"); } - private static Dictionary<(int x, int y), int> GetSuccessfulVelocities(Rectangle bounds) + private static List<((int x, int y) pt, int height)> GetSuccessfulVelocities(Rectangle bounds) { var (minX, maxX) = GetXVelocityRange(bounds); var (minY, maxY) = GetYVelocityRange(bounds); (int x, int y) pt; - Dictionary<(int x, int y), int> successes = new(); + List<((int x, int y) pt, int height)> successes = new(); for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) @@ -103,7 +103,7 @@ internal class Day17 : Day if (bounds.Contains(pt)) { - successes.Add(startingVelocity, currHighestY); + successes.Add((startingVelocity, currHighestY)); } } }