Better day 10 renderer

This adds constants for box-type-drawing characters and expands the logger's support for printing colored chunks without needing a newline. The result is quite nice! Credit to r/adventofcode for the idea :)
This commit is contained in:
2023-12-10 12:21:38 -06:00
parent 4cac4bd164
commit 59637e4eff
7 changed files with 64 additions and 30 deletions

View File

@ -17,6 +17,17 @@ internal class Day10 : Day
start, start,
} }
private static readonly Dictionary<tiletype, char> drawChars = new()
{
{tiletype.ns, Constants.BoxVert},
{tiletype.ew, Constants.BoxHorz},
{tiletype.ne, Constants.BoxCurveNE},
{tiletype.nw, Constants.BoxCurveNW},
{tiletype.sw, Constants.BoxCurveSW},
{tiletype.se, Constants.BoxCurveSE},
{tiletype.ground, ' '},
};
private readonly Dictionary<ivec2, tiletype> grid = []; private readonly Dictionary<ivec2, tiletype> grid = [];
private readonly Dictionary<ivec2, int> pipeLocations = []; private readonly Dictionary<ivec2, int> pipeLocations = [];
private ivec2 startLoc; private ivec2 startLoc;
@ -184,24 +195,33 @@ internal class Day10 : Day
} }
} }
void RenderGrid(List<ivec2>? interiors = null)
{
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
var p = new ivec2(col, row);
if (pipeLocations.ContainsKey(p))
{
Logger.Log($"<bgred>{drawChars[grid[p]]}<r>");
}
else if (interiors?.Contains(p) == true)
{
Logger.Log($"<bgblue>{drawChars[grid[p]]}<r>");
}
else
{
Console.Write(drawChars[grid[p]]);
}
}
Logger.LogLine("");
}
}
internal override string Part1() internal override string Part1()
{ {
// render your map! // RenderGrid();
// for (int row = 0; row < height; row++)
// {
// for (int col = 0; col < width; col++)
// {
// if (visited.ContainsKey(new ivec2(col, row)))
// {
// Console.Write(Constants.SolidBlock);
// }
// else
// {
// Console.Write('.');
// }
// }
// Console.WriteLine();
// }
int furthestLocation = pipeLocations.Max(v => v.Value); int furthestLocation = pipeLocations.Max(v => v.Value);
return $"Furthest point from start requires #steps: <+white>{furthestLocation}"; return $"Furthest point from start requires #steps: <+white>{furthestLocation}";
@ -233,7 +253,7 @@ internal class Day10 : Day
} }
} }
int interior = 0; List<ivec2> interior = new();
for (int row = 0; row < height; row++) for (int row = 0; row < height; row++)
{ {
bool bInside = false; bool bInside = false;
@ -258,11 +278,13 @@ internal class Day10 : Day
if (bInside && !pipeLocations.ContainsKey(pt)) if (bInside && !pipeLocations.ContainsKey(pt))
{ {
interior++; interior.Add(pt);
} }
} }
} }
return $"Number of spaces interior to the pipeline: <+white>{interior}"; // RenderGrid(interior);
return $"Number of spaces interior to the pipeline: <+white>{interior.Count}";
} }
} }

View File

@ -4,12 +4,12 @@ internal abstract class Day : IDisposable
{ {
public void Dispose() public void Dispose()
{ {
Logger.Log(""); Logger.LogLine("");
} }
internal void Go(bool runPart1, bool runPart2) internal void Go(bool runPart1, bool runPart2)
{ {
Logger.Log($"<reverse>{GetType().Name}<r>"); Logger.LogLine($"<reverse>{GetType().Name}<r>");
using (new Timer("Parsing")) using (new Timer("Parsing"))
{ {
@ -23,7 +23,7 @@ internal abstract class Day : IDisposable
stopwatch.Stop(); stopwatch.Stop();
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
Logger.Log($"<+black>> part1: {response}<r>"); Logger.LogLine($"<+black>> part1: {response}<r>");
} }
else else
{ {
@ -38,7 +38,7 @@ internal abstract class Day : IDisposable
stopwatch.Stop(); stopwatch.Stop();
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
Logger.Log($"<+black>> part2: {response}<r>"); Logger.LogLine($"<+black>> part2: {response}<r>");
} }
else else
{ {

View File

@ -44,12 +44,18 @@ internal class Logger
{ "reverse", "\u001b[7m" }, { "reverse", "\u001b[7m" },
}; };
public static void Log(string msg) public static void LogLine(string msg)
{ {
Console.WriteLine(InsertColorCodes(msg)); Console.WriteLine(InsertColorCodes(msg));
Debug.WriteLine(StripColorCodes(msg)); Debug.WriteLine(StripColorCodes(msg));
} }
public static void Log(string msg)
{
Console.Write(InsertColorCodes(msg));
Debug.Write(StripColorCodes(msg));
}
private static string InsertColorCodes(string msg) private static string InsertColorCodes(string msg)
{ {
foreach (var code in colorCodes) foreach (var code in colorCodes)

View File

@ -43,6 +43,6 @@ internal class Timer : IDisposable
{ {
color = "<yellow>"; color = "<yellow>";
} }
Logger.Log($"<cyan>{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}<r>"); Logger.LogLine($"<cyan>{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}<r>");
} }
} }

View File

@ -3,4 +3,10 @@
public static class Constants public static class Constants
{ {
public const char SolidBlock = '█'; public const char SolidBlock = '█';
public const char BoxVert = '│';
public const char BoxHorz = '─';
public const char BoxCurveNE = '╰';
public const char BoxCurveNW = '╯';
public const char BoxCurveSW = '╮';
public const char BoxCurveSE = '╭';
} }

View File

@ -6,12 +6,12 @@ public static class Testing
{ {
internal static void StartTestSet(string name) internal static void StartTestSet(string name)
{ {
Logger.Log($"<underline>test: {name}<r>"); Logger.LogLine($"<underline>test: {name}<r>");
} }
internal static void StartTest(string label) internal static void StartTest(string label)
{ {
Logger.Log($"<magenta>{label}<r>"); Logger.LogLine($"<magenta>{label}<r>");
} }
internal static void TestCondition(Func<bool> a, bool printResult = true) internal static void TestCondition(Func<bool> a, bool printResult = true)
@ -21,14 +21,14 @@ public static class Testing
Debug.Assert(false); Debug.Assert(false);
if (printResult) if (printResult)
{ {
Logger.Log("<red>x<r>"); Logger.LogLine("<red>x<r>");
} }
} }
else else
{ {
if (printResult) if (printResult)
{ {
Logger.Log("<green>✓<r>"); Logger.LogLine("<green>✓<r>");
} }
} }
} }

View File

@ -64,7 +64,7 @@ else
var type = types.FirstOrDefault(x => x.Name == $"Day{desiredDay.PadLeft(2, '0')}"); var type = types.FirstOrDefault(x => x.Name == $"Day{desiredDay.PadLeft(2, '0')}");
if (type == null) if (type == null)
{ {
Logger.Log($"Unknown day <cyan>{desiredDay}<r>"); Logger.LogLine($"Unknown day <cyan>{desiredDay}<r>");
} }
else else
{ {