Report more accurate timings
The string colorizer is slow (but apparently only the first time it runs, for some reason?), printing can be slow, who knows what else the framework is doing, etc., so manually inserting Stop()s exactly where we want the timings to stop is the most reliable way to handle this. I like the elegance of the disposable solution, but my goal here is to measure the actual algorithm, not anything else. A slightly restructuring would make it so I don't have to do this; if main.cs was receiving a string from Part1 and Part2 instead of having each day individually print its own results, we could scope timings to exclude any console writing. But whatever. I also went ahead and made 17 only run once since I was using the same result to answer part 1 and 2. I've since discovered that part 1 is far simpler and can be found without solving the entire problem space, but whatever.
This commit is contained in:
@ -27,6 +27,7 @@ internal class Day01 : Day
|
||||
lastDepth = depth;
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{numIncreased}<r>");
|
||||
}
|
||||
|
||||
@ -61,6 +62,7 @@ internal class Day01 : Day
|
||||
lastTotal = total;
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{numIncreased}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -54,6 +54,7 @@ internal class Day02 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: h: {pos.h}, d: {pos.d}, result: <+white>{pos.h * pos.d}<r>");
|
||||
}
|
||||
|
||||
@ -81,6 +82,7 @@ internal class Day02 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: h: {pos.h}, d: {pos.d}, result: <+white>{pos.h * pos.d}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ internal class Day03 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: gamma rate: {gammaRate}, epsilon rate: {epsilonRate}, mult: <+white>{gammaRate * epsilonRate}<r>");
|
||||
}
|
||||
|
||||
@ -123,6 +124,7 @@ internal class Day03 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: o2*co2 = {o2} * {co2} = <+white>{o2 * co2}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -72,6 +72,7 @@ internal class Day05 : Day
|
||||
{
|
||||
using var t = new Timer();
|
||||
int numPointsGreater = Solve(lines, (line) => !(line.Start.X == line.End.X || line.Start.Y == line.End.Y));
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{numPointsGreater}<r>");
|
||||
}
|
||||
|
||||
@ -79,6 +80,7 @@ internal class Day05 : Day
|
||||
{
|
||||
using var t = new Timer();
|
||||
int numPointsGreater = Solve(lines, (line) => false);
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{numPointsGreater}<r>");
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,7 @@ internal class Day06 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available - Count is of type int, list might be longer than that
|
||||
Logger.Log($"<+black>> part1: #fish=<+white>{list.LongCount()}<r>");
|
||||
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
|
||||
@ -72,6 +73,7 @@ internal class Day06 : Day
|
||||
fishAtState[8] = adders;
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: #fish=<+white>{fishAtState.Values.Sum()}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ internal class Day07 : Day
|
||||
|
||||
var (minDist, minNum) = Solve(nums, (d) => d);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: position: {minNum}, fuel cost: <+white>{minDist}<r>");
|
||||
}
|
||||
|
||||
@ -50,6 +51,7 @@ internal class Day07 : Day
|
||||
// found by searching "factorial but with addition" because i'm smart like that.
|
||||
var (minDist, minNum) = Solve(nums, (d) => ((d * d) + d) / 2);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: position: {minNum}, fuel cost: <+white>{minDist}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ internal class Day08 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{count}<r>");
|
||||
}
|
||||
|
||||
@ -100,6 +101,7 @@ internal class Day08 : Day
|
||||
sum += num;
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{sum}<r>");
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ internal class Day09 : Day
|
||||
var lowPoints = GetLowPoints(grid);
|
||||
var totalRisk = lowPoints.Sum(x => grid[x.Item1, x.Item2] + 1);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{totalRisk}<r>");
|
||||
}
|
||||
|
||||
@ -72,6 +73,7 @@ internal class Day09 : Day
|
||||
}
|
||||
var top3Mult = basins.OrderByDescending(x => x).Take(3).Aggregate(1, (x, y) => x * y);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{top3Mult}<r>");
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,7 @@ internal class Day10 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{score}<r>");
|
||||
}
|
||||
|
||||
@ -133,6 +134,7 @@ internal class Day10 : Day
|
||||
|
||||
var final = scores.OrderBy(x => x).Skip(scores.Count / 2).First();
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{final}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,7 @@ internal class Day11 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{numFlashes}<r>");
|
||||
}
|
||||
|
||||
@ -132,6 +133,7 @@ internal class Day11 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{step}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ internal class Day12 : Day
|
||||
var validPaths = new List<List<string>>();
|
||||
FindPaths(paths, validPaths, new List<string>(){ "START" }, false);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{validPaths.Count}<r>");
|
||||
//validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x));
|
||||
}
|
||||
@ -102,6 +103,7 @@ internal class Day12 : Day
|
||||
var validPaths = new List<List<string>>();
|
||||
FindPaths(paths, validPaths, new List<string>() { "START" }, true);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{validPaths.Count}<r>");
|
||||
//validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x));
|
||||
}
|
||||
|
@ -57,6 +57,7 @@ internal class Day13 : Day
|
||||
|
||||
Fold(grid, folds[0].axis, folds[0].line);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{grid.Count}<r>");
|
||||
}
|
||||
|
||||
@ -87,6 +88,8 @@ internal class Day13 : Day
|
||||
sb.Append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+white>{sb}<r>");
|
||||
Logger.Log($"<+black>> part2: {grid.Count}<r>");
|
||||
}
|
||||
|
@ -59,6 +59,7 @@ internal class Day14 : Day
|
||||
var least = frequencies.Min(x => x.Value);
|
||||
var most = frequencies.Max(x => x.Value);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{most - least}<r>");
|
||||
}
|
||||
|
||||
@ -110,6 +111,7 @@ internal class Day14 : Day
|
||||
}
|
||||
}
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{most - least}<r>");
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ internal class Day15 : Day
|
||||
|
||||
var risk = Solve(grid);
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white>{risk}<r>");
|
||||
}
|
||||
|
||||
@ -107,6 +108,7 @@ internal class Day15 : Day
|
||||
|
||||
var risk = Solve(ScaleUp(grid));
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white>{risk}<r>");
|
||||
}
|
||||
}
|
||||
|
38
src/17.cs
38
src/17.cs
@ -35,8 +35,12 @@ internal class Day17 : Day
|
||||
bounds.minY = Convert.ToInt32(ys[0][(ys[0].IndexOf('=') + 1)..]);
|
||||
bounds.maxY = Convert.ToInt32(ys[1]);
|
||||
|
||||
Part1(bounds);
|
||||
Part2(bounds);
|
||||
using var t = new Timer();
|
||||
var successes = GetSuccessfulVelocities(bounds);
|
||||
t.Stop();
|
||||
|
||||
Part1(successes);
|
||||
Part2(successes);
|
||||
}
|
||||
|
||||
private static (int x, int y) Step((int x, int y) pt, ref (int x, int y) velocity)
|
||||
@ -56,23 +60,16 @@ internal class Day17 : Day
|
||||
return (pt.x, pt.y);
|
||||
}
|
||||
|
||||
private static void Part1(Rectangle bounds)
|
||||
private static void Part1(Dictionary<(int x, int y), int> successes)
|
||||
{
|
||||
using var t = new Timer();
|
||||
|
||||
var successes = GetSuccessfulVelocities(bounds);
|
||||
var max = successes.MaxBy(vel => vel.Value);
|
||||
var numOthers = successes.Count(vel => vel.Value == max.Value);
|
||||
|
||||
Logger.Log($"<+black>> part1: highest Y was at velocity {max.Key} (and {numOthers} other{(numOthers == 1 ? "" : "s")}): <+white>{max.Value}<r>");
|
||||
}
|
||||
|
||||
private static void Part2(Rectangle bounds)
|
||||
private static void Part2(Dictionary<(int x, int y), int> successes)
|
||||
{
|
||||
using var t = new Timer();
|
||||
|
||||
var successes = GetSuccessfulVelocities(bounds);
|
||||
|
||||
Logger.Log($"<+black>> part2: #successful velocities: <+white>{successes.Count}<r>");
|
||||
}
|
||||
|
||||
@ -141,21 +138,16 @@ internal class Day17 : Day
|
||||
private static (int min, int max) GetYVelocityRange(Rectangle bounds)
|
||||
{
|
||||
int maxSuccessYVel = int.MinValue;
|
||||
int maxVelY = Math.Abs(bounds.minY) - 1;
|
||||
int guess = bounds.minY;
|
||||
bool foundYVals = false;
|
||||
while (!foundYVals)
|
||||
while (guess <= maxVelY)
|
||||
{
|
||||
var guesspt = (0, 0);
|
||||
var testVel = (0, guess);
|
||||
bool lastWasAbove = false;
|
||||
while (true)
|
||||
{
|
||||
guesspt = Step(guesspt, ref testVel);
|
||||
if (bounds.IsHigh(guesspt))
|
||||
{
|
||||
lastWasAbove = true;
|
||||
}
|
||||
else if (!bounds.IsLow(guesspt))
|
||||
if (!bounds.IsHigh(guesspt) && !bounds.IsLow(guesspt))
|
||||
{
|
||||
if (guess > maxSuccessYVel)
|
||||
{
|
||||
@ -163,14 +155,8 @@ internal class Day17 : Day
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
else if (bounds.IsLow(guesspt))
|
||||
{
|
||||
// need to find a better way to choose a reasonable guess max...
|
||||
if (lastWasAbove && guess > bounds.minY * -4)
|
||||
{
|
||||
foundYVals = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,9 @@ internal class DayTemplate : Day
|
||||
{
|
||||
using var t = new Timer();
|
||||
|
||||
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part1: <+white><r>");
|
||||
}
|
||||
|
||||
@ -20,6 +23,9 @@ internal class DayTemplate : Day
|
||||
{
|
||||
using var t = new Timer();
|
||||
|
||||
|
||||
|
||||
t.Stop();
|
||||
Logger.Log($"<+black>> part2: <+white><r>");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user