From 59637e4eff004d31370d5971aed99073400af00d Mon Sep 17 00:00:00 2001 From: Parnic Date: Sun, 10 Dec 2023 12:21:38 -0600 Subject: [PATCH] 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 :) --- src/10.cs | 60 +++++++++++++++++++++++++++++-------------- src/Day.cs | 8 +++--- src/Logger.cs | 8 +++++- src/Timer.cs | 2 +- src/Util/Constants.cs | 6 +++++ src/Util/Testing.cs | 8 +++--- src/main.cs | 2 +- 7 files changed, 64 insertions(+), 30 deletions(-) diff --git a/src/10.cs b/src/10.cs index 91e9cff..0f223b8 100644 --- a/src/10.cs +++ b/src/10.cs @@ -17,6 +17,17 @@ internal class Day10 : Day start, } + private static readonly Dictionary 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 grid = []; private readonly Dictionary pipeLocations = []; private ivec2 startLoc; @@ -184,24 +195,33 @@ internal class Day10 : Day } } + void RenderGrid(List? 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($"{drawChars[grid[p]]}"); + } + else if (interiors?.Contains(p) == true) + { + Logger.Log($"{drawChars[grid[p]]}"); + } + 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 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}"; } } diff --git a/src/Day.cs b/src/Day.cs index 04b8512..03f9885 100644 --- a/src/Day.cs +++ b/src/Day.cs @@ -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($"{GetType().Name}"); + Logger.LogLine($"{GetType().Name}"); using (new Timer("Parsing")) { @@ -23,7 +23,7 @@ internal abstract class Day : IDisposable stopwatch.Stop(); if (!string.IsNullOrEmpty(response)) { - Logger.Log($"<+black>> part1: {response}"); + Logger.LogLine($"<+black>> part1: {response}"); } else { @@ -38,7 +38,7 @@ internal abstract class Day : IDisposable stopwatch.Stop(); if (!string.IsNullOrEmpty(response)) { - Logger.Log($"<+black>> part2: {response}"); + Logger.LogLine($"<+black>> part2: {response}"); } else { diff --git a/src/Logger.cs b/src/Logger.cs index 7c696b7..c9da419 100644 --- a/src/Logger.cs +++ b/src/Logger.cs @@ -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) diff --git a/src/Timer.cs b/src/Timer.cs index abd1d20..9236f07 100644 --- a/src/Timer.cs +++ b/src/Timer.cs @@ -43,6 +43,6 @@ internal class Timer : IDisposable { color = ""; } - Logger.Log($"{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}"); + Logger.LogLine($"{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}"); } } diff --git a/src/Util/Constants.cs b/src/Util/Constants.cs index 094f884..8c9b923 100644 --- a/src/Util/Constants.cs +++ b/src/Util/Constants.cs @@ -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 = '╭'; } diff --git a/src/Util/Testing.cs b/src/Util/Testing.cs index 4831311..0855a50 100644 --- a/src/Util/Testing.cs +++ b/src/Util/Testing.cs @@ -6,12 +6,12 @@ public static class Testing { internal static void StartTestSet(string name) { - Logger.Log($"test: {name}"); + Logger.LogLine($"test: {name}"); } internal static void StartTest(string label) { - Logger.Log($"{label}"); + Logger.LogLine($"{label}"); } internal static void TestCondition(Func a, bool printResult = true) @@ -21,14 +21,14 @@ public static class Testing Debug.Assert(false); if (printResult) { - Logger.Log("x"); + Logger.LogLine("x"); } } else { if (printResult) { - Logger.Log(""); + Logger.LogLine(""); } } } diff --git a/src/main.cs b/src/main.cs index 35c2ee3..67ab4ed 100644 --- a/src/main.cs +++ b/src/main.cs @@ -64,7 +64,7 @@ else var type = types.FirstOrDefault(x => x.Name == $"Day{desiredDay.PadLeft(2, '0')}"); if (type == null) { - Logger.Log($"Unknown day {desiredDay}"); + Logger.LogLine($"Unknown day {desiredDay}"); } else {