mirror of
https://github.com/parnic/advent-of-code-2023.git
synced 2025-06-16 16:50:14 -05:00
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:
60
src/10.cs
60
src/10.cs
@ -17,6 +17,17 @@ internal class Day10 : Day
|
||||
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, int> pipeLocations = [];
|
||||
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()
|
||||
{
|
||||
// render your map!
|
||||
// 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();
|
||||
// }
|
||||
// RenderGrid();
|
||||
|
||||
int furthestLocation = pipeLocations.Max(v => v.Value);
|
||||
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++)
|
||||
{
|
||||
bool bInside = false;
|
||||
@ -258,11 +278,13 @@ internal class Day10 : Day
|
||||
|
||||
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}";
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,12 @@ internal abstract class Day : IDisposable
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
Logger.Log("");
|
||||
Logger.LogLine("");
|
||||
}
|
||||
|
||||
internal void Go(bool runPart1, bool runPart2)
|
||||
{
|
||||
Logger.Log($"<reverse>{GetType().Name}<r>");
|
||||
Logger.LogLine($"<reverse>{GetType().Name}<r>");
|
||||
|
||||
using (new Timer("Parsing"))
|
||||
{
|
||||
@ -23,7 +23,7 @@ internal abstract class Day : IDisposable
|
||||
stopwatch.Stop();
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
Logger.Log($"<+black>> part1: {response}<r>");
|
||||
Logger.LogLine($"<+black>> part1: {response}<r>");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -38,7 +38,7 @@ internal abstract class Day : IDisposable
|
||||
stopwatch.Stop();
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
Logger.Log($"<+black>> part2: {response}<r>");
|
||||
Logger.LogLine($"<+black>> part2: {response}<r>");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -44,12 +44,18 @@ internal class Logger
|
||||
{ "reverse", "\u001b[7m" },
|
||||
};
|
||||
|
||||
public static void Log(string msg)
|
||||
public static void LogLine(string msg)
|
||||
{
|
||||
Console.WriteLine(InsertColorCodes(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)
|
||||
{
|
||||
foreach (var code in colorCodes)
|
||||
|
@ -43,6 +43,6 @@ internal class Timer : IDisposable
|
||||
{
|
||||
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>");
|
||||
}
|
||||
}
|
||||
|
@ -3,4 +3,10 @@
|
||||
public static class Constants
|
||||
{
|
||||
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 = '╭';
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ public static class Testing
|
||||
{
|
||||
internal static void StartTestSet(string name)
|
||||
{
|
||||
Logger.Log($"<underline>test: {name}<r>");
|
||||
Logger.LogLine($"<underline>test: {name}<r>");
|
||||
}
|
||||
|
||||
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)
|
||||
@ -21,14 +21,14 @@ public static class Testing
|
||||
Debug.Assert(false);
|
||||
if (printResult)
|
||||
{
|
||||
Logger.Log("<red>x<r>");
|
||||
Logger.LogLine("<red>x<r>");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (printResult)
|
||||
{
|
||||
Logger.Log("<green>✓<r>");
|
||||
Logger.LogLine("<green>✓<r>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ else
|
||||
var type = types.FirstOrDefault(x => x.Name == $"Day{desiredDay.PadLeft(2, '0')}");
|
||||
if (type == null)
|
||||
{
|
||||
Logger.Log($"Unknown day <cyan>{desiredDay}<r>");
|
||||
Logger.LogLine($"Unknown day <cyan>{desiredDay}<r>");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Reference in New Issue
Block a user