Add colors and stuff

Because I think it's neat. Ref: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
This commit is contained in:
2021-12-13 09:46:32 -06:00
parent 884e685c62
commit fe2d276115
17 changed files with 70 additions and 72 deletions

View File

@ -4,12 +4,9 @@ internal class Day01 : Day
{
internal override void Go()
{
Logger.Log("Day 1");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/01.txt");
Part1(lines);
Part2(lines);
Logger.Log("");
}
private static void Part1(IEnumerable<string> lines)
@ -30,7 +27,7 @@ internal class Day01 : Day
lastDepth = depth;
}
Logger.Log($"part1: {numIncreased}");
Logger.Log($"part1: <blue>{numIncreased}<r>");
}
private static void Part2(IEnumerable<string> lines)
@ -64,6 +61,6 @@ internal class Day01 : Day
lastTotal = total;
}
Logger.Log($"part2: {numIncreased}");
Logger.Log($"part2: <blue>{numIncreased}<r>");
}
}

View File

@ -4,8 +4,6 @@ internal class Day02 : Day
{
internal override void Go()
{
Logger.Log("Day 2");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/02.txt");
var instructions = new List<Instruction>();
foreach (var instruction in lines)
@ -20,7 +18,6 @@ internal class Day02 : Day
Part1(instructions);
Part2(instructions);
Logger.Log("");
}
struct Instruction
@ -57,7 +54,7 @@ internal class Day02 : Day
}
}
Logger.Log($"part1: h: {pos.h}, d: {pos.d}, result: {pos.h * pos.d}");
Logger.Log($"part1: h: {pos.h}, d: {pos.d}, result: <blue>{pos.h * pos.d}<r>");
}
private static void Part2(IEnumerable<Instruction> instructions)
@ -84,6 +81,6 @@ internal class Day02 : Day
}
}
Logger.Log($"part2: h: {pos.h}, d: {pos.d}, result: {pos.h * pos.d}");
Logger.Log($"part2: h: {pos.h}, d: {pos.d}, result: <blue>{pos.h * pos.d}<r>");
}
}

View File

@ -4,12 +4,9 @@ internal class Day03 : Day
{
internal override void Go()
{
Logger.Log("Day 3");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/03.txt");
Part1(lines);
Part2(lines);
Logger.Log("");
}
private static void Part1(IEnumerable<string> lines)
@ -44,7 +41,7 @@ internal class Day03 : Day
}
}
Logger.Log($"part1: gamma rate: {gammaRate}, epsilon rate: {epsilonRate}, mult: {gammaRate * epsilonRate}");
Logger.Log($"part1: gamma rate: {gammaRate}, epsilon rate: {epsilonRate}, mult: <blue>{gammaRate * epsilonRate}<r>");
}
private static void Part2(IEnumerable<string> lines)
@ -126,6 +123,6 @@ internal class Day03 : Day
}
}
Logger.Log($"part2: o2*co2 = {o2} * {co2} = {o2 * co2}");
Logger.Log($"part2: o2*co2 = {o2} * {co2} = <blue>{o2 * co2}<r>");
}
}

View File

@ -43,8 +43,6 @@ internal class Day05 : Day
internal override void Go()
{
Logger.Log("Day 5");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/05.txt");
List<Line> segments = new();
foreach (var line in lines)
@ -68,21 +66,20 @@ internal class Day05 : Day
Part1(segments);
Part2(segments);
Logger.Log("");
}
private static void Part1(IEnumerable<Line> lines)
{
using var t = new Timer();
int numPointsGreater = Solve(lines, (line) => !(line.Start.X == line.End.X || line.Start.Y == line.End.Y));
Logger.Log($"part1: {numPointsGreater}");
Logger.Log($"part1: <blue>{numPointsGreater}<r>");
}
private static void Part2(IEnumerable<Line> lines)
{
using var t = new Timer();
int numPointsGreater = Solve(lines, (line) => false);
Logger.Log($"part2: {numPointsGreater}");
Logger.Log($"part2: <blue>{numPointsGreater}<r>");
}
private static int Solve(IEnumerable<Line> lines, Func<Line, bool> filter)

View File

@ -12,8 +12,6 @@ internal class Day06 : Day
internal override void Go()
{
Logger.Log("Day 6");
Logger.Log("-----");
var input = File.ReadAllText("inputs/06.txt");
List<Fish> fish = new();
foreach (var state in input.Split(','))
@ -22,7 +20,6 @@ internal class Day06 : Day
}
Part1(fish);
Part2(fish);
Logger.Log("");
}
private static void Part1(IEnumerable<Fish> fish)
@ -48,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
Logger.Log($"part1: #fish={list.LongCount()}");
Logger.Log($"part1: #fish=<blue>{list.LongCount()}<r>");
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
}
@ -75,6 +72,6 @@ internal class Day06 : Day
fishAtState[8] = adders;
}
Logger.Log($"part2: #fish={fishAtState.Values.Sum()}");
Logger.Log($"part2: #fish=<blue>{fishAtState.Values.Sum()}<r>");
}
}

View File

@ -4,12 +4,9 @@ internal class Day07 : Day
{
internal override void Go()
{
Logger.Log("Day 7");
Logger.Log("-----");
var nums = File.ReadAllText("inputs/07.txt").Split(',').Select(int.Parse);
Part1(nums);
Part2(nums);
Logger.Log("");
}
private static (long, int) Solve(IEnumerable<int> nums, Func<long, long> formula)
@ -42,7 +39,7 @@ internal class Day07 : Day
var (minDist, minNum) = Solve(nums, (d) => d);
Logger.Log($"part1: position: {minNum}, fuel cost: {minDist}");
Logger.Log($"part1: position: {minNum}, fuel cost: <blue>{minDist}<r>");
}
private static void Part2(IEnumerable<int> nums)
@ -53,6 +50,6 @@ 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);
Logger.Log($"part2: position: {minNum}, fuel cost: {minDist}");
Logger.Log($"part2: position: {minNum}, fuel cost: <blue>{minDist}<r>");
}
}

View File

@ -4,8 +4,6 @@ internal class Day08 : Day
{
internal override void Go()
{
Logger.Log("Day 8");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/08.txt");
List<(List<string>, List<string>)> puzzle = new();
foreach (var line in lines)
@ -15,7 +13,6 @@ internal class Day08 : Day
}
Part1(puzzle);
Part2(puzzle);
Logger.Log("");
}
private static void Part1(List<(List<string>, List<string>)> lines)
@ -39,7 +36,7 @@ internal class Day08 : Day
}
}
Logger.Log($"part1: {count}");
Logger.Log($"part1: <blue>{count}<r>");
}
private static void Part2(List<(List<string>, List<string>)> lines)
@ -103,7 +100,7 @@ internal class Day08 : Day
sum += num;
}
Logger.Log($"part2: {sum}");
Logger.Log($"part2: <blue>{sum}<r>");
}
private static int FindNum(char[] segments, string num)

View File

@ -4,8 +4,6 @@ internal class Day09 : Day
{
internal override void Go()
{
Logger.Log("Day 9");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/09.txt");
byte[,] grid = new byte[lines.Length, lines[0].Length];
for (int i = 0; i < lines.Length; i++)
@ -17,7 +15,6 @@ internal class Day09 : Day
}
Part1(grid);
Part2(grid);
Logger.Log("");
}
private static void Part1(byte[,] grid)
@ -27,7 +24,7 @@ internal class Day09 : Day
var lowPoints = GetLowPoints(grid);
var totalRisk = lowPoints.Sum(x => grid[x.Item1, x.Item2] + 1);
Logger.Log($"part1: {totalRisk}");
Logger.Log($"part1: <blue>{totalRisk}<r>");
}
private static List<(int, int)> GetLowPoints(byte[,] grid)
@ -75,7 +72,7 @@ internal class Day09 : Day
}
var top3Mult = basins.OrderByDescending(x => x).Take(3).Aggregate(1, (x, y) => x * y);
Logger.Log($"part2: {top3Mult}");
Logger.Log($"part2: <blue>{top3Mult}<r>");
}
private static List<(int, int)> GetBasinSize(byte[,] grid, int i, int j)

View File

@ -4,12 +4,9 @@ internal class Day10 : Day
{
internal override void Go()
{
Logger.Log("Day 10");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/10.txt");
Part1(lines);
Part2(lines);
Logger.Log("");
}
private static readonly List<char> Openers = new()
@ -52,7 +49,7 @@ internal class Day10 : Day
}
}
Logger.Log($"part1: {score}");
Logger.Log($"part1: <blue>{score}<r>");
}
private static (bool, char) IsCorrupted(string line)
@ -136,6 +133,6 @@ internal class Day10 : Day
var final = scores.OrderBy(x => x).Skip(scores.Count / 2).First();
Logger.Log($"part2: {final}");
Logger.Log($"part2: <blue>{final}<r>");
}
}

View File

@ -4,9 +4,6 @@ internal class Day11 : Day
{
internal override void Go()
{
Logger.Log("Day 11");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/11.txt");
var grid = new byte[lines.Length, lines[0].Length];
for (int i = 0; i < lines.Length; i++)
@ -24,8 +21,6 @@ internal class Day11 : Day
Part1(grid);
Part2(grid);
Logger.Log("");
}
private static IEnumerable<(int, int)> Adjacent(byte[,] grid, int i, int j)
@ -104,7 +99,7 @@ internal class Day11 : Day
}
}
Logger.Log($"part1: {numFlashes}");
Logger.Log($"part1: <blue>{numFlashes}<r>");
}
private static void Part2(byte[,] grid)
@ -137,6 +132,6 @@ internal class Day11 : Day
}
}
Logger.Log($"part2: {step}");
Logger.Log($"part2: <blue>{step}<r>");
}
}

View File

@ -4,8 +4,6 @@ internal class Day12 : Day
{
internal override void Go()
{
Logger.Log("Day 12");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/12.txt");
var paths = new Dictionary<string, List<string>>();
foreach (var line in lines)
@ -40,7 +38,6 @@ internal class Day12 : Day
}
Part1(paths);
Part2(paths);
Logger.Log("");
}
private static void Part1(Dictionary<string, List<string>> paths)
@ -50,8 +47,8 @@ internal class Day12 : Day
var validPaths = new List<List<string>>();
FindPaths(paths, validPaths, new List<string>(){ "START" }, false);
Logger.Log($"part1: {validPaths.Count}");
//validPaths.ForEach(path => Logger.Log($" {string.Join(',', path)}"));
Logger.Log($"part1: <blue>{validPaths.Count}<r>");
//validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x));
}
private static void FindPaths(Dictionary<string, List<string>> paths, List<List<string>> routes, List<string> currRoute, bool canVisitSmallCaveTwice, bool hasDoubledCave = false)
@ -105,7 +102,7 @@ internal class Day12 : Day
var validPaths = new List<List<string>>();
FindPaths(paths, validPaths, new List<string>() { "START" }, true);
Logger.Log($"part2: {validPaths.Count}");
//validPaths.ForEach(path => Logger.Log($" {string.Join(',', path)}"));
Logger.Log($"part2: <blue>{validPaths.Count}<r>");
//validPaths.Select(x => string.Join(',', x)).OrderBy(x => x).ForEach(x => Logger.Log(x));
}
}

View File

@ -4,8 +4,6 @@ internal class Day13 : Day
{
internal override void Go()
{
Logger.Log("Day 13");
Logger.Log("-----");
var lines = File.ReadAllLines("inputs/13.txt");
int phase = 0;
var points = new HashSet<(int, int)>();
@ -31,7 +29,6 @@ internal class Day13 : Day
}
Part1(points, folds);
Part2(points, folds);
Logger.Log("");
}
private static void Fold(ICollection<(int x, int y)> points, char axis, int line)
@ -60,7 +57,7 @@ internal class Day13 : Day
Fold(grid, folds[0].axis, folds[0].line);
Logger.Log($"part1: {grid.Count}");
Logger.Log($"part1: <blue>{grid.Count}<r>");
}
private static void Part2(ICollection<(int x, int y)> grid, IList<(char axis, int line)> folds)
@ -90,7 +87,7 @@ internal class Day13 : Day
sb.Append('\n');
}
}
Logger.Log(sb.ToString());
Logger.Log($"<blue>{sb}<r>");
Logger.Log($"part2: {grid.Count}");
}
}

View File

@ -1,6 +1,17 @@
namespace aoc2021;
internal abstract class Day
internal abstract class Day : IDisposable
{
public Day()
{
Logger.Log($"\u001b[47m\u001b[30m{GetType().Name}\u001b[0m");
//Logger.Log("\u001b[47m\u001b[30m-----\u001b[0m");
}
public void Dispose()
{
Logger.Log("");
}
internal abstract void Go();
}

View File

@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace aoc2021
{
@ -6,8 +7,23 @@ namespace aoc2021
{
public static void Log(string msg)
{
Console.WriteLine(msg);
Debug.WriteLine(msg);
Console.WriteLine(InsertColorCodes(msg));
Debug.WriteLine(StripColorCodes(msg));
}
private static string InsertColorCodes(string msg)
{
return msg.Replace("<blue>", "\u001b[36;1m")
.Replace("<r>", "\u001b[0m");
}
private static readonly Regex colorCodes = new Regex(@"(\u001b\[(?:\d+;)?(?:\d+;)?\d+m)", RegexOptions.Compiled);
private static string StripColorCodes(string msg)
{
var ret = msg;
var matches = colorCodes.Matches(msg);
matches?.ForEach(match => ret = ret.Replace(match.Groups[1].Value, ""));
return ret;
}
}
}

View File

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

View File

@ -9,7 +9,16 @@ namespace aoc2021
{
stopwatch.Stop();
var (elapsed, unit) = ConvertElapsedToHumanReadable();
Logger.Log($"Took {elapsed:N1}{unit}");
var color = "[31m";
if (unit == "us" || (unit == "ms" && elapsed < 10))
{
color = "[32m";
}
else if (unit == "ms" && elapsed < 250)
{
color = "[33m";
}
Logger.Log($"Took \u001b{color}{elapsed:N1}{unit}\u001b[0m");
}
public (double elapsed, string unit) ConvertElapsedToHumanReadable()

View File

@ -11,13 +11,13 @@ if (arg == "all")
foreach (var type in types)
{
var day = (Day)Activator.CreateInstance(type)!;
using var day = (Day)Activator.CreateInstance(type)!;
day.Go();
}
}
else
{
Day day = arg switch
using Day day = arg switch
{
"1" => new Day01(),
"2" => new Day02(),