Expand color code support

Because @tocchan had such a better implementation than me.
This commit is contained in:
2021-12-16 16:06:27 -06:00
parent 542df5bd8c
commit 13cc275842
20 changed files with 93 additions and 49 deletions

View File

@ -27,7 +27,7 @@ internal class Day01 : Day
lastDepth = depth; lastDepth = depth;
} }
Logger.Log($"part1: <blue>{numIncreased}<r>"); Logger.Log($"<+black>> part1: <+white>{numIncreased}<r>");
} }
private static void Part2(IEnumerable<string> lines) private static void Part2(IEnumerable<string> lines)
@ -61,6 +61,6 @@ internal class Day01 : Day
lastTotal = total; lastTotal = total;
} }
Logger.Log($"part2: <blue>{numIncreased}<r>"); Logger.Log($"<+black>> part2: <+white>{numIncreased}<r>");
} }
} }

View File

@ -39,7 +39,7 @@ func part1(depths []int) {
lastDepth = depth lastDepth = depth
} }
fmt.Println("part1: increased:", numIncreased) fmt.Println("<+black>> part1: increased:", numIncreased)
} }
func part2(depths []int) { func part2(depths []int) {
@ -65,5 +65,5 @@ func part2(depths []int) {
lastTotal = total lastTotal = total
} }
fmt.Println("part2: increased:", numIncreased) fmt.Println("<+black>> part2: increased:", numIncreased)
} }

View File

@ -54,7 +54,7 @@ internal class Day02 : Day
} }
} }
Logger.Log($"part1: h: {pos.h}, d: {pos.d}, result: <blue>{pos.h * pos.d}<r>"); Logger.Log($"<+black>> part1: h: {pos.h}, d: {pos.d}, result: <+white>{pos.h * pos.d}<r>");
} }
private static void Part2(IEnumerable<Instruction> instructions) private static void Part2(IEnumerable<Instruction> instructions)
@ -81,6 +81,6 @@ internal class Day02 : Day
} }
} }
Logger.Log($"part2: h: {pos.h}, d: {pos.d}, result: <blue>{pos.h * pos.d}<r>"); Logger.Log($"<+black>> part2: h: {pos.h}, d: {pos.d}, result: <+white>{pos.h * pos.d}<r>");
} }
} }

View File

@ -41,7 +41,7 @@ internal class Day03 : Day
} }
} }
Logger.Log($"part1: gamma rate: {gammaRate}, epsilon rate: {epsilonRate}, mult: <blue>{gammaRate * epsilonRate}<r>"); Logger.Log($"<+black>> part1: gamma rate: {gammaRate}, epsilon rate: {epsilonRate}, mult: <+white>{gammaRate * epsilonRate}<r>");
} }
private static void Part2(IEnumerable<string> lines) private static void Part2(IEnumerable<string> lines)
@ -123,6 +123,6 @@ internal class Day03 : Day
} }
} }
Logger.Log($"part2: o2*co2 = {o2} * {co2} = <blue>{o2 * co2}<r>"); Logger.Log($"<+black>> part2: o2*co2 = {o2} * {co2} = <+white>{o2 * co2}<r>");
} }
} }

View File

@ -72,14 +72,14 @@ internal class Day05 : Day
{ {
using var t = new Timer(); using var t = new Timer();
int numPointsGreater = Solve(lines, (line) => !(line.Start.X == line.End.X || line.Start.Y == line.End.Y)); int numPointsGreater = Solve(lines, (line) => !(line.Start.X == line.End.X || line.Start.Y == line.End.Y));
Logger.Log($"part1: <blue>{numPointsGreater}<r>"); Logger.Log($"<+black>> part1: <+white>{numPointsGreater}<r>");
} }
private static void Part2(IEnumerable<Line> lines) private static void Part2(IEnumerable<Line> lines)
{ {
using var t = new Timer(); using var t = new Timer();
int numPointsGreater = Solve(lines, (line) => false); int numPointsGreater = Solve(lines, (line) => false);
Logger.Log($"part2: <blue>{numPointsGreater}<r>"); Logger.Log($"<+black>> part2: <+white>{numPointsGreater}<r>");
} }
private static int Solve(IEnumerable<Line> lines, Func<Line, bool> filter) private static int Solve(IEnumerable<Line> lines, Func<Line, bool> filter)

View File

@ -45,7 +45,7 @@ internal class Day06 : Day
} }
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available - Count is of type int, list might be longer than that #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($"part1: #fish=<blue>{list.LongCount()}<r>"); Logger.Log($"<+black>> part1: #fish=<+white>{list.LongCount()}<r>");
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available #pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
} }
@ -72,6 +72,6 @@ internal class Day06 : Day
fishAtState[8] = adders; fishAtState[8] = adders;
} }
Logger.Log($"part2: #fish=<blue>{fishAtState.Values.Sum()}<r>"); Logger.Log($"<+black>> part2: #fish=<+white>{fishAtState.Values.Sum()}<r>");
} }
} }

View File

@ -39,7 +39,7 @@ internal class Day07 : Day
var (minDist, minNum) = Solve(nums, (d) => d); var (minDist, minNum) = Solve(nums, (d) => d);
Logger.Log($"part1: position: {minNum}, fuel cost: <blue>{minDist}<r>"); Logger.Log($"<+black>> part1: position: {minNum}, fuel cost: <+white>{minDist}<r>");
} }
private static void Part2(IEnumerable<int> nums) private static void Part2(IEnumerable<int> nums)
@ -50,6 +50,6 @@ internal class Day07 : Day
// found by searching "factorial but with addition" because i'm smart like that. // found by searching "factorial but with addition" because i'm smart like that.
var (minDist, minNum) = Solve(nums, (d) => ((d * d) + d) / 2); var (minDist, minNum) = Solve(nums, (d) => ((d * d) + d) / 2);
Logger.Log($"part2: position: {minNum}, fuel cost: <blue>{minDist}<r>"); Logger.Log($"<+black>> part2: position: {minNum}, fuel cost: <+white>{minDist}<r>");
} }
} }

View File

@ -36,7 +36,7 @@ internal class Day08 : Day
} }
} }
Logger.Log($"part1: <blue>{count}<r>"); Logger.Log($"<+black>> part1: <+white>{count}<r>");
} }
private static void Part2(List<(List<string>, List<string>)> lines) private static void Part2(List<(List<string>, List<string>)> lines)
@ -100,7 +100,7 @@ internal class Day08 : Day
sum += num; sum += num;
} }
Logger.Log($"part2: <blue>{sum}<r>"); Logger.Log($"<+black>> part2: <+white>{sum}<r>");
} }
private static int FindNum(char[] segments, string num) private static int FindNum(char[] segments, string num)

View File

@ -24,7 +24,7 @@ internal class Day09 : Day
var lowPoints = GetLowPoints(grid); var lowPoints = GetLowPoints(grid);
var totalRisk = lowPoints.Sum(x => grid[x.Item1, x.Item2] + 1); var totalRisk = lowPoints.Sum(x => grid[x.Item1, x.Item2] + 1);
Logger.Log($"part1: <blue>{totalRisk}<r>"); Logger.Log($"<+black>> part1: <+white>{totalRisk}<r>");
} }
private static List<(int, int)> GetLowPoints(byte[,] grid) private static List<(int, int)> GetLowPoints(byte[,] grid)
@ -72,7 +72,7 @@ internal class Day09 : Day
} }
var top3Mult = basins.OrderByDescending(x => x).Take(3).Aggregate(1, (x, y) => x * y); var top3Mult = basins.OrderByDescending(x => x).Take(3).Aggregate(1, (x, y) => x * y);
Logger.Log($"part2: <blue>{top3Mult}<r>"); Logger.Log($"<+black>> part2: <+white>{top3Mult}<r>");
} }
private static List<(int, int)> GetBasinSize(byte[,] grid, int i, int j) private static List<(int, int)> GetBasinSize(byte[,] grid, int i, int j)

View File

@ -49,7 +49,7 @@ internal class Day10 : Day
} }
} }
Logger.Log($"part1: <blue>{score}<r>"); Logger.Log($"<+black>> part1: <+white>{score}<r>");
} }
private static (bool, char) IsCorrupted(string line) private static (bool, char) IsCorrupted(string line)
@ -133,6 +133,6 @@ internal class Day10 : Day
var final = scores.OrderBy(x => x).Skip(scores.Count / 2).First(); var final = scores.OrderBy(x => x).Skip(scores.Count / 2).First();
Logger.Log($"part2: <blue>{final}<r>"); Logger.Log($"<+black>> part2: <+white>{final}<r>");
} }
} }

View File

@ -99,7 +99,7 @@ internal class Day11 : Day
} }
} }
Logger.Log($"part1: <blue>{numFlashes}<r>"); Logger.Log($"<+black>> part1: <+white>{numFlashes}<r>");
} }
private static void Part2(byte[,] grid) private static void Part2(byte[,] grid)
@ -132,6 +132,6 @@ internal class Day11 : Day
} }
} }
Logger.Log($"part2: <blue>{step}<r>"); Logger.Log($"<+black>> part2: <+white>{step}<r>");
} }
} }

View File

@ -47,7 +47,7 @@ internal class Day12 : Day
var validPaths = new List<List<string>>(); var validPaths = new List<List<string>>();
FindPaths(paths, validPaths, new List<string>(){ "START" }, false); FindPaths(paths, validPaths, new List<string>(){ "START" }, false);
Logger.Log($"part1: <blue>{validPaths.Count}<r>"); Logger.Log($"<+black>> part1: <+white>{validPaths.Count}<r>");
//validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x)); //validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x));
} }
@ -102,7 +102,7 @@ internal class Day12 : Day
var validPaths = new List<List<string>>(); var validPaths = new List<List<string>>();
FindPaths(paths, validPaths, new List<string>() { "START" }, true); FindPaths(paths, validPaths, new List<string>() { "START" }, true);
Logger.Log($"part2: <blue>{validPaths.Count}<r>"); Logger.Log($"<+black>> part2: <+white>{validPaths.Count}<r>");
//validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x)); //validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x));
} }
} }

View File

@ -57,7 +57,7 @@ internal class Day13 : Day
Fold(grid, folds[0].axis, folds[0].line); Fold(grid, folds[0].axis, folds[0].line);
Logger.Log($"part1: <blue>{grid.Count}<r>"); Logger.Log($"<+black>> part1: <+white>{grid.Count}<r>");
} }
private static void Part2(ICollection<(int x, int y)> grid, IList<(char axis, int line)> folds) private static void Part2(ICollection<(int x, int y)> grid, IList<(char axis, int line)> folds)
@ -87,7 +87,7 @@ internal class Day13 : Day
sb.Append('\n'); sb.Append('\n');
} }
} }
Logger.Log($"<blue>{sb}<r>"); Logger.Log($"<+white>{sb}<r>");
Logger.Log($"part2: {grid.Count}"); Logger.Log($"<+black>> part2: {grid.Count}<r>");
} }
} }

View File

@ -59,7 +59,7 @@ internal class Day14 : Day
var least = frequencies.Min(x => x.Value); var least = frequencies.Min(x => x.Value);
var most = frequencies.Max(x => x.Value); var most = frequencies.Max(x => x.Value);
Logger.Log($"part1: <blue>{most - least}<r>"); Logger.Log($"<+black>> part1: <+white>{most - least}<r>");
} }
private static void Part2(string template, Dictionary<string, char> rules) private static void Part2(string template, Dictionary<string, char> rules)
@ -110,6 +110,6 @@ internal class Day14 : Day
} }
} }
Logger.Log($"part2: <blue>{most - least}<r>"); Logger.Log($"<+black>> part2: <+white>{most - least}<r>");
} }
} }

View File

@ -75,7 +75,7 @@ internal class Day15 : Day
var risk = Solve(grid); var risk = Solve(grid);
Logger.Log($"part1: <blue>{risk}<r>"); Logger.Log($"<+black>> part1: <+white>{risk}<r>");
} }
private static byte[,] ScaleUp(byte[,] map) private static byte[,] ScaleUp(byte[,] map)
@ -107,6 +107,6 @@ internal class Day15 : Day
var risk = Solve(ScaleUp(grid)); var risk = Solve(ScaleUp(grid));
Logger.Log($"part2: <blue>{risk}<r>"); Logger.Log($"<+black>> part2: <+white>{risk}<r>");
} }
} }

View File

@ -23,12 +23,12 @@ internal class Day16 : Day
private static void Part1(long versionTotal) private static void Part1(long versionTotal)
{ {
Logger.Log($"part1: version total: <blue>{versionTotal}<r>"); Logger.Log($"<+black>> part1: version total: <+white>{versionTotal}<r>");
} }
private static void Part2(long result) private static void Part2(long result)
{ {
Logger.Log($"part2: operator result: <blue>{result}<r>"); Logger.Log($"<+black>> part2: operator result: <+white>{result}<r>");
} }
private static (long versionTotal, long result) DecodePacket(string binary, ref int idx) private static (long versionTotal, long result) DecodePacket(string binary, ref int idx)

View File

@ -4,8 +4,8 @@ internal abstract class Day : IDisposable
{ {
public Day() public Day()
{ {
Logger.Log($"\u001b[47m\u001b[30m{GetType().Name}\u001b[0m"); Logger.Log($"<reverse>{GetType().Name}<r>");
//Logger.Log("\u001b[47m\u001b[30m-----\u001b[0m"); //Logger.Log("<reverse>-----<r>");
} }
public void Dispose() public void Dispose()

View File

@ -1,10 +1,49 @@
using System.Diagnostics; using System.Diagnostics;
using System.Text.RegularExpressions;
namespace aoc2021 namespace aoc2021
{ {
internal class Logger internal class Logger
{ {
private static readonly Dictionary<string, string> colorCodes = new()
{
{ "r", "\u001b[0m" },
{ "black", "\u001b[30m" },
{ "red", "\u001b[31m" },
{ "green", "\u001b[32m" },
{ "yellow", "\u001b[33m" },
{ "blue", "\u001b[34m" },
{ "magenta", "\u001b[35m" },
{ "cyan", "\u001b[36m" },
{ "white", "\u001b[37m" },
{ "+black", "\u001b[30;1m" },
{ "+red", "\u001b[31;1m" },
{ "+green", "\u001b[32;1m" },
{ "+yellow", "\u001b[33;1m" },
{ "+blue", "\u001b[34;1m" },
{ "+magenta", "\u001b[35;1m" },
{ "+cyan", "\u001b[36;1m" },
{ "+white", "\u001b[37;1m" },
{ "bgBlack", "\u001b[40m" },
{ "bgRed", "\u001b[41m" },
{ "bgGreen", "\u001b[42m" },
{ "bgYellow", "\u001b[43m" },
{ "bgBlue", "\u001b[44m" },
{ "bgMagenta", "\u001b[45m" },
{ "bgCyan", "\u001b[46m" },
{ "bgWhite", "\u001b[47m" },
{ "+bgBlack", "\u001b[40;1m" },
{ "+bgRed", "\u001b[41;1m" },
{ "+bgGreen", "\u001b[42;1m" },
{ "+bgYellow", "\u001b[43;1m" },
{ "+bgBlue", "\u001b[44;1m" },
{ "+bgMagenta", "\u001b[45;1m" },
{ "+bgCyan", "\u001b[46;1m" },
{ "+bgWhite", "\u001b[47;1m" },
{ "bold", "\u001b[1m" },
{ "underline", "\u001b[4m" },
{ "reverse", "\u001b[7m" },
};
public static void Log(string msg) public static void Log(string msg)
{ {
Console.WriteLine(InsertColorCodes(msg)); Console.WriteLine(InsertColorCodes(msg));
@ -13,17 +52,22 @@ namespace aoc2021
private static string InsertColorCodes(string msg) private static string InsertColorCodes(string msg)
{ {
return msg.Replace("<blue>", "\u001b[36;1m") foreach (var code in colorCodes)
.Replace("<r>", "\u001b[0m"); {
msg = msg.Replace($"<{code.Key}>", code.Value, StringComparison.CurrentCultureIgnoreCase);
}
return msg;
} }
private static readonly Regex colorCodes = new(@"(\u001b\[(?:\d+;)?(?:\d+;)?\d+m)", RegexOptions.Compiled);
private static string StripColorCodes(string msg) private static string StripColorCodes(string msg)
{ {
var ret = msg; foreach (var code in colorCodes)
var matches = colorCodes.Matches(msg); {
matches?.ForEach(match => ret = ret.Replace(match.Groups[1].Value, "")); msg = msg.Replace($"<{code.Key}>", string.Empty, StringComparison.CurrentCultureIgnoreCase);
return ret; }
return msg;
} }
} }
} }

View File

@ -13,13 +13,13 @@ internal class DayTemplate : Day
{ {
using var t = new Timer(); using var t = new Timer();
Logger.Log($"part1: <blue><r>"); Logger.Log($"<+black>> part1: <+white><r>");
} }
private static void Part2(IEnumerable<string> lines) private static void Part2(IEnumerable<string> lines)
{ {
using var t = new Timer(); using var t = new Timer();
Logger.Log($"part2: <blue><r>"); Logger.Log($"<+black>> part2: <+white><r>");
} }
} }

View File

@ -28,16 +28,16 @@ namespace aoc2021
{ {
Stop(); Stop();
var (elapsed, unit) = ConvertElapsedToHumanReadable(); var (elapsed, unit) = ConvertElapsedToHumanReadable();
var color = "[31m"; var color = "<red>";
if (unit == "us" || (unit == "ms" && elapsed < 10)) if (unit == "us" || (unit == "ms" && elapsed < 10))
{ {
color = "[32m"; color = "<green>";
} }
else if (unit == "ms" && elapsed < 250) else if (unit == "ms" && elapsed < 250)
{ {
color = "[33m"; color = "<yellow>";
} }
Logger.Log($"{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook \u001b{color}{elapsed:N1}{unit}\u001b[0m"); Logger.Log($"<cyan>{name}{(!string.IsNullOrEmpty(name) ? " t" : "T")}ook {color}{elapsed:N1}{unit}<r>");
} }
public (double elapsed, string unit) ConvertElapsedToHumanReadable() public (double elapsed, string unit) ConvertElapsedToHumanReadable()