From aaf31ed7b12f15526b1040141520243c039c1c1b Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 14 Dec 2021 12:51:50 -0600 Subject: [PATCH] Shave a few ms off day 14 This is a small low-hanging fruit pass. I was really just curious where the time was going since I didn't feel like the algorithm was actually expensive. Turns out it's not, really...the larger cost is setting up the frequencies and pairs collections, and computing min/max was 9x more expensive than it needed to be (I was expecting 2x since I was iterating over the collection twice, but 9x is...unreasonable, so I just unrolled it myself). It also turns out that MinBy() is marginally slower than Min() with no gain (for this use case), so that was an easy win on the brute force solution. --- src/14.cs | 54 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/14.cs b/src/14.cs index 6820f6d..c2acaed 100644 --- a/src/14.cs +++ b/src/14.cs @@ -33,6 +33,13 @@ internal class Day14 : Day string curr = template; + Dictionary frequencies = new(); + foreach (var pair in rules) + { + frequencies[pair.Key[0]] = 0; + frequencies[pair.Key[1]] = 0; + } + for (int i = 0; i < 10; i++) { StringBuilder sb = new(); @@ -41,24 +48,18 @@ internal class Day14 : Day sb.Append(curr[j]); sb.Append(rules[curr[j..(j + 2)]]); } - sb.Append(curr[curr.Length - 1]); + sb.Append(curr[^1]); curr = sb.ToString(); } - Dictionary frequency = new(); foreach (var c in curr) { - if (!frequency.ContainsKey(c)) - { - frequency.Add(c, 0); - } - - frequency[c]++; + frequencies[c]++; } - var least = frequency.MinBy(x => x.Value); - var most = frequency.MaxBy(x => x.Value); + var least = frequencies.Min(x => x.Value); + var most = frequencies.Max(x => x.Value); - Logger.Log($"part1: {most.Value - least.Value}"); + Logger.Log($"part1: {most - least}"); } private static void Part2(string template, Dictionary rules) @@ -69,15 +70,9 @@ internal class Day14 : Day Dictionary frequencies = new(); foreach (var pair in rules) { - pairs.Add(pair.Key, 0); - if (!frequencies.ContainsKey(pair.Key[0])) - { - frequencies.Add(pair.Key[0], 0); - } - if (!frequencies.ContainsKey(pair.Key[1])) - { - frequencies.Add(pair.Key[1], 0); - } + pairs[pair.Key] = 0; + frequencies[pair.Key[0]] = 0; + frequencies[pair.Key[1]] = 0; } for (int i = 0; i < template.Length - 1; i++) { @@ -89,7 +84,7 @@ internal class Day14 : Day for (int round = 0; round < 40; round++) { - foreach (var pair in pairs.Where(x => x.Value > 0).ToList()) + foreach (var pair in pairs.ToList()) { pairs[pair.Key] -= pair.Value; frequencies[rules[pair.Key]] += pair.Value; @@ -101,9 +96,20 @@ internal class Day14 : Day } } - var least = frequencies.MinBy(x => x.Value); - var most = frequencies.MaxBy(x => x.Value); + long least = long.MaxValue; + long most = long.MinValue; + foreach (var pair in frequencies) + { + if (pair.Value < least) + { + least = pair.Value; + } + if (pair.Value > most) + { + most = pair.Value; + } + } - Logger.Log($"part2: {most.Value - least.Value}"); + Logger.Log($"part2: {most - least}"); } }