Compare commits
2 Commits
aa833569da
...
63c866f5a0
| Author | SHA1 | Date | |
|---|---|---|---|
| 63c866f5a0 | |||
| 147c348ee1 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ riderModule.iml
|
|||||||
/_ReSharper.Caches/
|
/_ReSharper.Caches/
|
||||||
.idea/.idea.AdventOfCode/.idea
|
.idea/.idea.AdventOfCode/.idea
|
||||||
AdventOfCode.sln.DotSettings.user
|
AdventOfCode.sln.DotSettings.user
|
||||||
|
BenchmarkDotNet.Artifacts
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
|
||||||
<PackageReference Include="Spectre.Console" Version="0.45.0" />
|
<PackageReference Include="Spectre.Console" Version="0.45.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
23
DayBenchmarker.cs
Normal file
23
DayBenchmarker.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using AdventOfCode.Days;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
|
||||||
|
namespace AdventOfCode;
|
||||||
|
|
||||||
|
[ShortRunJob]
|
||||||
|
[MemoryDiagnoser(false)]
|
||||||
|
public class DayBenchmark
|
||||||
|
{
|
||||||
|
private Day Day => new Day15();
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void Part1()
|
||||||
|
{
|
||||||
|
Day.RunPart1(display: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void Part2()
|
||||||
|
{
|
||||||
|
Day.RunPart2(display: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,8 +5,8 @@ public abstract class Day
|
|||||||
public abstract int Number { get; }
|
public abstract int Number { get; }
|
||||||
public abstract string Name { get; }
|
public abstract string Name { get; }
|
||||||
|
|
||||||
public abstract void RunPart1();
|
public abstract void RunPart1(bool display = true);
|
||||||
public abstract void RunPart2();
|
public abstract void RunPart2(bool display = true);
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
|||||||
15
Days/Day1.cs
15
Days/Day1.cs
@@ -7,7 +7,7 @@ public class Day1 : Day
|
|||||||
public override int Number => 1;
|
public override int Number => 1;
|
||||||
public override string Name => "Calorie Counting";
|
public override string Name => "Calorie Counting";
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
int currentCalories = 0;
|
int currentCalories = 0;
|
||||||
int maxCalories = 0;
|
int maxCalories = 0;
|
||||||
@@ -28,10 +28,13 @@ public class Day1 : Day
|
|||||||
currentCalories += int.Parse(line);
|
currentCalories += int.Parse(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Max calories is: [yellow]{maxCalories}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Max calories is: [yellow]{maxCalories}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
var elfCalories = new List<int>();
|
var elfCalories = new List<int>();
|
||||||
|
|
||||||
@@ -51,7 +54,11 @@ public class Day1 : Day
|
|||||||
}
|
}
|
||||||
|
|
||||||
var totalCalories = elfCalories.OrderByDescending(c => c).Take(3).Sum();
|
var totalCalories = elfCalories.OrderByDescending(c => c).Take(3).Sum();
|
||||||
AnsiConsole.MarkupLine($"[green]Total calories by top 3 elf is: [yellow]{totalCalories}[/][/]");
|
|
||||||
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Total calories by top 3 elf is: [yellow]{totalCalories}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Input
|
#region Input
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ public class Day10 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 10;
|
public override int Number => 10;
|
||||||
public override string Name => "Cathode-Ray Tube";
|
public override string Name => "Cathode-Ray Tube";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
int cycle = 1;
|
int cycle = 1;
|
||||||
int x = 1;
|
int x = 1;
|
||||||
@@ -47,10 +47,13 @@ public class Day10 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Sum of signal strengths: [yellow]{sum}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Sum of signal strengths: [yellow]{sum}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
// Setup the CRT
|
// Setup the CRT
|
||||||
const char litPixel = '#';
|
const char litPixel = '#';
|
||||||
@@ -122,7 +125,10 @@ public class Day10 : Day
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print the CRT
|
// Print the CRT
|
||||||
AnsiConsole.Write(crtCanvas);
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.Write(crtCanvas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Input
|
#region Input
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ public class Day11 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 11;
|
public override int Number => 11;
|
||||||
public override string Name => "Monkey in the Middle";
|
public override string Name => "Monkey in the Middle";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
const int roundsCount = 20;
|
const int roundsCount = 20;
|
||||||
|
|
||||||
@@ -95,10 +95,13 @@ public class Day11 : Day
|
|||||||
|
|
||||||
var topMonkeys = monkeys.OrderByDescending(m => m.InspectionCount).Take(2).ToList();
|
var topMonkeys = monkeys.OrderByDescending(m => m.InspectionCount).Take(2).ToList();
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Monkey business: [yellow]{topMonkeys[0].InspectionCount * topMonkeys[1].InspectionCount}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Monkey business: [yellow]{topMonkeys[0].InspectionCount * topMonkeys[1].InspectionCount}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
const int roundsCount = 10_000;
|
const int roundsCount = 10_000;
|
||||||
|
|
||||||
@@ -116,7 +119,10 @@ public class Day11 : Day
|
|||||||
|
|
||||||
var topMonkeys = monkeys.OrderByDescending(m => m.InspectionCount).Take(2).ToList();
|
var topMonkeys = monkeys.OrderByDescending(m => m.InspectionCount).Take(2).ToList();
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Monkey business: [yellow]{topMonkeys[0].InspectionCount * topMonkeys[1].InspectionCount}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Monkey business: [yellow]{topMonkeys[0].InspectionCount * topMonkeys[1].InspectionCount}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IImmutableList<Monkey> ParseMonkeys()
|
private static IImmutableList<Monkey> ParseMonkeys()
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class Day12 : Day
|
|||||||
private (int x, int y) _end;
|
private (int x, int y) _end;
|
||||||
private (int x, int y) _start;
|
private (int x, int y) _start;
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
var map = ParseMap();
|
var map = ParseMap();
|
||||||
var canvas = DrawMap(map);
|
var canvas = DrawMap(map);
|
||||||
@@ -57,13 +57,16 @@ public class Day12 : Day
|
|||||||
canvas.SetPixel(x, y, Color.White);
|
canvas.SetPixel(x, y, Color.White);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.Write(canvas);
|
if (display)
|
||||||
AnsiConsole.WriteLine();
|
{
|
||||||
|
AnsiConsole.Write(canvas);
|
||||||
|
AnsiConsole.WriteLine();
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Minimum path length: [yellow]{minimumPathLength.length}[/][/]");
|
AnsiConsole.MarkupLine($"[green]Minimum path length: [yellow]{minimumPathLength.length}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
var map = ParseMap();
|
var map = ParseMap();
|
||||||
var canvas = DrawMap(map);
|
var canvas = DrawMap(map);
|
||||||
@@ -77,10 +80,13 @@ public class Day12 : Day
|
|||||||
canvas.SetPixel(x, y, Color.White);
|
canvas.SetPixel(x, y, Color.White);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.Write(canvas);
|
if (display)
|
||||||
AnsiConsole.WriteLine();
|
{
|
||||||
|
AnsiConsole.Write(canvas);
|
||||||
|
AnsiConsole.WriteLine();
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Minimum path length: [yellow]{minimumPathLength.length}[/][/]");
|
AnsiConsole.MarkupLine($"[green]Minimum path length: [yellow]{minimumPathLength.length}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private (int length, List<(int x, int y)> path) ExplorePath(byte[,] map)
|
private (int length, List<(int x, int y)> path) ExplorePath(byte[,] map)
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ public class Day13 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 13;
|
public override int Number => 13;
|
||||||
public override string Name => "Distress Signal";
|
public override string Name => "Distress Signal";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
var pairs = ParsePacketPairs();
|
var pairs = ParsePacketPairs();
|
||||||
|
|
||||||
@@ -169,10 +169,13 @@ public class Day13 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Right order pair indices sum: [yellow]{sum}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Right order pair indices sum: [yellow]{sum}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
var packets = ParsePackets();
|
var packets = ParsePackets();
|
||||||
|
|
||||||
@@ -186,7 +189,11 @@ public class Day13 : Day
|
|||||||
|
|
||||||
var firstDividerIndex = orderedPackets.IndexOf(firstDivider) + 1;
|
var firstDividerIndex = orderedPackets.IndexOf(firstDivider) + 1;
|
||||||
var lastDividerIndex = orderedPackets.IndexOf(lastDivider) + 1;
|
var lastDividerIndex = orderedPackets.IndexOf(lastDivider) + 1;
|
||||||
AnsiConsole.MarkupLine($"[green]Decoder key is: [yellow]{firstDividerIndex * lastDividerIndex}[/][/]");
|
|
||||||
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Decoder key is: [yellow]{firstDividerIndex * lastDividerIndex}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private static PacketElement ReadElement(ref ReadOnlySpan<char> packet)
|
private static PacketElement ReadElement(ref ReadOnlySpan<char> packet)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ public class Day14 : Day
|
|||||||
private readonly Point _sandSource = new(500, 0);
|
private readonly Point _sandSource = new(500, 0);
|
||||||
private Point _offsetSandSource;
|
private Point _offsetSandSource;
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
var map = GenerateMap();
|
var map = GenerateMap();
|
||||||
|
|
||||||
@@ -136,12 +136,15 @@ public class Day14 : Day
|
|||||||
sandBlocksCount++;
|
sandBlocksCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.Write(canvas);
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.Write(canvas);
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Total sand blocks: [yellow]{sandBlocksCount}[/][/]");
|
AnsiConsole.MarkupLine($"[green]Total sand blocks: [yellow]{sandBlocksCount}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
var map = GenerateDictionaryMap();
|
var map = GenerateDictionaryMap();
|
||||||
|
|
||||||
@@ -156,9 +159,13 @@ public class Day14 : Day
|
|||||||
sandBlocksCount++;
|
sandBlocksCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.Write(GenerateCanvas(map, _sandSource));
|
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Total sand blocks: [yellow]{sandBlocksCount}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.Write(GenerateCanvas(map, _sandSource));
|
||||||
|
|
||||||
|
AnsiConsole.MarkupLine($"[green]Total sand blocks: [yellow]{sandBlocksCount}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool GenerateSand(CellType[,] map, Canvas canvas, in Point sandSource)
|
private static bool GenerateSand(CellType[,] map, Canvas canvas, in Point sandSource)
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class Day15 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 15;
|
public override int Number => 15;
|
||||||
public override string Name => "Beacon Exclusion Zone";
|
public override string Name => "Beacon Exclusion Zone";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
const int targetY = 2_000_000;
|
const int targetY = 2_000_000;
|
||||||
|
|
||||||
@@ -129,10 +129,13 @@ public class Day15 : Day
|
|||||||
totalCovered -= beaconsOnTargetY.Count;
|
totalCovered -= beaconsOnTargetY.Count;
|
||||||
|
|
||||||
// Print total covered columns on line Y
|
// Print total covered columns on line Y
|
||||||
AnsiConsole.MarkupLine($"[green]Number of positions that cannot contain a beacon: [yellow]{totalCovered}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Number of positions that cannot contain a beacon: [yellow]{totalCovered}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
const int xMin = 0;
|
const int xMin = 0;
|
||||||
const int xMax = 4_000_000;
|
const int xMax = 4_000_000;
|
||||||
@@ -213,7 +216,10 @@ public class Day15 : Day
|
|||||||
|
|
||||||
var tuningFrequency = finalPoint.X * (long) 4_000_000 + finalPoint.Y;
|
var tuningFrequency = finalPoint.X * (long) 4_000_000 + finalPoint.Y;
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Tuning frequency: [yellow]{tuningFrequency}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Tuning frequency: [yellow]{tuningFrequency}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IList<SensorBeaconPair> ParsePairs()
|
private static IList<SensorBeaconPair> ParsePairs()
|
||||||
|
|||||||
18
Days/Day2.cs
18
Days/Day2.cs
@@ -18,10 +18,10 @@ public enum Outcome : long
|
|||||||
|
|
||||||
public class Day2 : Day
|
public class Day2 : Day
|
||||||
{
|
{
|
||||||
public override int Number { get; } = 2;
|
public override int Number => 2;
|
||||||
public override string Name { get; } = "Rock Paper Scissors";
|
public override string Name => "Rock Paper Scissors";
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
long score = 0;
|
long score = 0;
|
||||||
|
|
||||||
@@ -34,10 +34,13 @@ public class Day2 : Day
|
|||||||
score += (long) ComputeOutcome(adversaryChoice, selfChoice);
|
score += (long) ComputeOutcome(adversaryChoice, selfChoice);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Total score: [yellow]{score}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Total score: [yellow]{score}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
long score = 0;
|
long score = 0;
|
||||||
|
|
||||||
@@ -51,7 +54,10 @@ public class Day2 : Day
|
|||||||
score += (long) desiredOutcome;
|
score += (long) desiredOutcome;
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Total score: [yellow]{score}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Total score: [yellow]{score}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Outcome ComputeOutcome(Choice adversary, Choice self) => (adversary, self) switch
|
private Outcome ComputeOutcome(Choice adversary, Choice self) => (adversary, self) switch
|
||||||
|
|||||||
16
Days/Day3.cs
16
Days/Day3.cs
@@ -7,7 +7,7 @@ public class Day3 : Day
|
|||||||
public override int Number => 3;
|
public override int Number => 3;
|
||||||
public override string Name => "Rucksack Reorganization";
|
public override string Name => "Rucksack Reorganization";
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
long sum = 0;
|
long sum = 0;
|
||||||
|
|
||||||
@@ -22,10 +22,13 @@ public class Day3 : Day
|
|||||||
sum += CharToPriority(common);
|
sum += CharToPriority(common);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Sum of priorities is: [yellow]{sum}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Sum of priorities is: [yellow]{sum}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
long sum = 0;
|
long sum = 0;
|
||||||
|
|
||||||
@@ -40,10 +43,13 @@ public class Day3 : Day
|
|||||||
sum += CharToPriority(common);
|
sum += CharToPriority(common);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Sum of priorities is: [yellow]{sum}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Sum of priorities is: [yellow]{sum}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int CharToPriority(char c)
|
private static int CharToPriority(char c)
|
||||||
{
|
{
|
||||||
if (char.IsLower(c))
|
if (char.IsLower(c))
|
||||||
{
|
{
|
||||||
|
|||||||
14
Days/Day4.cs
14
Days/Day4.cs
@@ -6,7 +6,7 @@ public class Day4 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 4;
|
public override int Number => 4;
|
||||||
public override string Name => "Camp Cleanup";
|
public override string Name => "Camp Cleanup";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
int overlaps = 0;
|
int overlaps = 0;
|
||||||
foreach (var line in Input.ReadAllLines())
|
foreach (var line in Input.ReadAllLines())
|
||||||
@@ -31,10 +31,13 @@ public class Day4 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Overlapping sections: [yellow]{overlaps}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Overlapping sections: [yellow]{overlaps}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
int overlaps = 0;
|
int overlaps = 0;
|
||||||
foreach (var line in Input.ReadAllLines())
|
foreach (var line in Input.ReadAllLines())
|
||||||
@@ -54,7 +57,10 @@ public class Day4 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Overlapping sections: [yellow]{overlaps}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Overlapping sections: [yellow]{overlaps}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Input
|
#region Input
|
||||||
|
|||||||
22
Days/Day5.cs
22
Days/Day5.cs
@@ -9,7 +9,7 @@ public class Day5 : Day
|
|||||||
|
|
||||||
private IDictionary<int, Stack<char>>? _stacks;
|
private IDictionary<int, Stack<char>>? _stacks;
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
_stacks = InitStacks();
|
_stacks = InitStacks();
|
||||||
|
|
||||||
@@ -26,14 +26,17 @@ public class Day5 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i <= 9; i++)
|
if (display)
|
||||||
{
|
{
|
||||||
AnsiConsole.Markup($"[{(i % 2 == 0 ? "yellow" : "green")}]{_stacks[i].Pop()}[/]");
|
for (int i = 1; i <= 9; i++)
|
||||||
|
{
|
||||||
|
AnsiConsole.Markup($"[{(i % 2 == 0 ? "yellow" : "green")}]{_stacks[i].Pop()}[/]");
|
||||||
|
}
|
||||||
|
AnsiConsole.WriteLine();
|
||||||
}
|
}
|
||||||
AnsiConsole.WriteLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
_stacks = InitStacks();
|
_stacks = InitStacks();
|
||||||
|
|
||||||
@@ -53,11 +56,14 @@ public class Day5 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i <= 9; i++)
|
if (display)
|
||||||
{
|
{
|
||||||
AnsiConsole.Markup($"[{(i % 2 == 0 ? "yellow" : "green")}]{_stacks[i].Pop()}[/]");
|
for (int i = 1; i <= 9; i++)
|
||||||
|
{
|
||||||
|
AnsiConsole.Markup($"[{(i % 2 == 0 ? "yellow" : "green")}]{_stacks[i].Pop()}[/]");
|
||||||
|
}
|
||||||
|
AnsiConsole.WriteLine();
|
||||||
}
|
}
|
||||||
AnsiConsole.WriteLine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Input
|
#region Input
|
||||||
|
|||||||
14
Days/Day6.cs
14
Days/Day6.cs
@@ -6,7 +6,7 @@ public class Day6 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 6;
|
public override int Number => 6;
|
||||||
public override string Name => "Tuning Trouble";
|
public override string Name => "Tuning Trouble";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
int position = 0;
|
int position = 0;
|
||||||
|
|
||||||
@@ -30,10 +30,13 @@ public class Day6 : Day
|
|||||||
charStream.Dequeue();
|
charStream.Dequeue();
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Position of start-of-packet marker is: [yellow]{position}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Position of start-of-packet marker is: [yellow]{position}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
const int length = 14;
|
const int length = 14;
|
||||||
int position = 0;
|
int position = 0;
|
||||||
@@ -59,7 +62,10 @@ public class Day6 : Day
|
|||||||
charStream.Dequeue();
|
charStream.Dequeue();
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Position of start-of-message marker is: [yellow]{position}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Position of start-of-message marker is: [yellow]{position}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Input
|
#region Input
|
||||||
|
|||||||
14
Days/Day7.cs
14
Days/Day7.cs
@@ -32,7 +32,7 @@ public class Day7 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 7;
|
public override int Number => 7;
|
||||||
public override string Name => "No Space Left On Device";
|
public override string Name => "No Space Left On Device";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
var fileSystem = ParseFileSystem();
|
var fileSystem = ParseFileSystem();
|
||||||
|
|
||||||
@@ -42,10 +42,13 @@ public class Day7 : Day
|
|||||||
.Where(d => d.Size < 100_000)
|
.Where(d => d.Size < 100_000)
|
||||||
.Sum(d => d.Size);
|
.Sum(d => d.Size);
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Total size: [yellow]{totalSize}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Total size: [yellow]{totalSize}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
const long fileSystemSpace = 70_000_000;
|
const long fileSystemSpace = 70_000_000;
|
||||||
const long requiredSpace = 30_000_000;
|
const long requiredSpace = 30_000_000;
|
||||||
@@ -63,7 +66,10 @@ public class Day7 : Day
|
|||||||
.Where(d => d.Size > missingSpace)
|
.Where(d => d.Size > missingSpace)
|
||||||
.MinBy(d => d.Size);
|
.MinBy(d => d.Size);
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Directory to remove size: [yellow]{directoryToRemove!.Size}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Directory to remove size: [yellow]{directoryToRemove!.Size}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DirectoryEntry ParseFileSystem()
|
private static DirectoryEntry ParseFileSystem()
|
||||||
|
|||||||
15
Days/Day8.cs
15
Days/Day8.cs
@@ -6,7 +6,7 @@ public class Day8 : Day
|
|||||||
{
|
{
|
||||||
public override int Number => 8;
|
public override int Number => 8;
|
||||||
public override string Name => "Treetop Tree House";
|
public override string Name => "Treetop Tree House";
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
var grid = CreateGrid();
|
var grid = CreateGrid();
|
||||||
|
|
||||||
@@ -92,10 +92,14 @@ public class Day8 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Total viewable trees: [yellow]{valid.Count}[/][/]");
|
|
||||||
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Total viewable trees: [yellow]{valid.Count}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
var grid = CreateGrid();
|
var grid = CreateGrid();
|
||||||
|
|
||||||
@@ -116,7 +120,10 @@ public class Day8 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]Max scenic score: [yellow]{maxScenicScore}[/][/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]Max scenic score: [yellow]{maxScenicScore}[/][/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int ComputeScenicScore(int[,] grid, int i, int j, int size)
|
private int ComputeScenicScore(int[,] grid, int i, int j, int size)
|
||||||
|
|||||||
14
Days/Day9.cs
14
Days/Day9.cs
@@ -7,7 +7,7 @@ public class Day9 : Day
|
|||||||
public override int Number => 9;
|
public override int Number => 9;
|
||||||
public override string Name => "Rope Bridge";
|
public override string Name => "Rope Bridge";
|
||||||
|
|
||||||
public override void RunPart1()
|
public override void RunPart1(bool display = true)
|
||||||
{
|
{
|
||||||
// Start
|
// Start
|
||||||
(int x, int y) start = (0, 0);
|
(int x, int y) start = (0, 0);
|
||||||
@@ -49,10 +49,13 @@ public class Day9 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]The tail visited [yellow]{tailVisitedPositions.Count}[/] unique positions[/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]The tail visited [yellow]{tailVisitedPositions.Count}[/] unique positions[/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RunPart2()
|
public override void RunPart2(bool display = true)
|
||||||
{
|
{
|
||||||
const int tailSegments = 9;
|
const int tailSegments = 9;
|
||||||
|
|
||||||
@@ -130,7 +133,10 @@ public class Day9 : Day
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[green]The tail visited [yellow]{tailVisitedPositions.Count}[/] unique positions[/]");
|
if (display)
|
||||||
|
{
|
||||||
|
AnsiConsole.MarkupLine($"[green]The tail visited [yellow]{tailVisitedPositions.Count}[/] unique positions[/]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Input
|
#region Input
|
||||||
|
|||||||
24
Program.cs
24
Program.cs
@@ -1,8 +1,18 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using AdventOfCode;
|
||||||
using AdventOfCode.Days;
|
using AdventOfCode.Days;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
using Spectre.Console;
|
using Spectre.Console;
|
||||||
|
|
||||||
|
// Benchmark
|
||||||
|
if (args is ["--bench" or "-b"])
|
||||||
|
{
|
||||||
|
BenchmarkRunner.Run<DayBenchmark>();
|
||||||
|
Environment.Exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normal run
|
||||||
var days = Assembly.GetAssembly(typeof(Day))!.GetTypes()
|
var days = Assembly.GetAssembly(typeof(Day))!.GetTypes()
|
||||||
.Where(t => t.IsAssignableTo(typeof(Day)) && t.GetConstructor(Type.EmptyTypes) != null && !t.IsAbstract)
|
.Where(t => t.IsAssignableTo(typeof(Day)) && t.GetConstructor(Type.EmptyTypes) != null && !t.IsAbstract)
|
||||||
.Select(t => (Day)Activator.CreateInstance(t)!);
|
.Select(t => (Day)Activator.CreateInstance(t)!);
|
||||||
@@ -13,23 +23,35 @@ var select = new SelectionPrompt<Day>()
|
|||||||
|
|
||||||
var selectedDay = AnsiConsole.Prompt(select);
|
var selectedDay = AnsiConsole.Prompt(select);
|
||||||
|
|
||||||
|
var stopWatch = new Stopwatch();
|
||||||
|
|
||||||
AnsiConsole.MarkupLine($"[cyan]Running [yellow]{selectedDay}[/]...[/]\n");
|
AnsiConsole.MarkupLine($"[cyan]Running [yellow]{selectedDay}[/]...[/]\n");
|
||||||
|
|
||||||
AnsiConsole.MarkupLine("[cyan]Part [yellow]1[/] result:[/]");
|
AnsiConsole.MarkupLine("[cyan]Part [yellow]1[/] result:[/]");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
stopWatch.Start();
|
||||||
selectedDay.RunPart1();
|
selectedDay.RunPart1();
|
||||||
|
stopWatch.Stop();
|
||||||
|
|
||||||
|
AnsiConsole.MarkupLine($"[red]Approximate run time: [yellow]{stopWatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000:F3} ms[/][/]");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
AnsiConsole.WriteException(e);
|
AnsiConsole.WriteException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stopWatch.Reset();
|
||||||
|
|
||||||
AnsiConsole.MarkupLine("\n[cyan]Part [yellow]2[/] result:[/]");
|
AnsiConsole.MarkupLine("\n[cyan]Part [yellow]2[/] result:[/]");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
stopWatch.Start();
|
||||||
selectedDay.RunPart2();
|
selectedDay.RunPart2();
|
||||||
|
stopWatch.Stop();
|
||||||
|
|
||||||
|
AnsiConsole.MarkupLine($"[red]Approximate run time: [yellow]{stopWatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000:F3} ms[/][/]");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user