Compare commits
20 Commits
33dd47128b
...
2k23
| Author | SHA1 | Date | |
|---|---|---|---|
| 828f9bc3dc | |||
| 63c866f5a0 | |||
| 147c348ee1 | |||
| aa833569da | |||
| 0dfc6b59de | |||
| 331d064f66 | |||
| 4ff214e321 | |||
| 254f7f2e71 | |||
| 2b4d26e725 | |||
| fd794bab16 | |||
| a985d2e00c | |||
| fe91c0f07a | |||
| 94e9159f8e | |||
| ccdf246d5c | |||
| 5d6835b2e6 | |||
| 489ecc6d8d | |||
| 57002d439a | |||
| a2f6d92f25 | |||
| 5aeb2669c9 | |||
| 6e55404b19 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -3,4 +3,6 @@ obj/
|
||||
/packages/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
.idea/.idea.AdventOfCode/.idea
|
||||
.idea/.idea.AdventOfCode/.idea
|
||||
AdventOfCode.sln.DotSettings.user
|
||||
BenchmarkDotNet.Artifacts
|
||||
|
||||
@@ -2,13 +2,19 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.45.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Inputs\Day1.txt" />
|
||||
<EmbeddedResource Include="Inputs\Day1.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
29
DayBenchmarker.cs
Normal file
29
DayBenchmarker.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using AdventOfCode.Days;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
|
||||
namespace AdventOfCode;
|
||||
|
||||
[ShortRunJob]
|
||||
[MemoryDiagnoser(false)]
|
||||
public class DayBenchmark
|
||||
{
|
||||
private Day Day { get; } = new Day1();
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
Day.ReadInput();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Part1()
|
||||
{
|
||||
Day.RunPart1(display: false);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Part2()
|
||||
{
|
||||
Day.RunPart2(display: false);
|
||||
}
|
||||
}
|
||||
18
Days/Day.cs
18
Days/Day.cs
@@ -1,12 +1,24 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace AdventOfCode.Days;
|
||||
|
||||
public abstract class Day
|
||||
{
|
||||
public abstract int Number { get; }
|
||||
public abstract string Name { get; }
|
||||
|
||||
public abstract void RunPart1();
|
||||
public abstract void RunPart2();
|
||||
|
||||
protected string Input { get; private set; } = null!;
|
||||
|
||||
public abstract void RunPart1(bool display = true);
|
||||
public abstract void RunPart2(bool display = true);
|
||||
|
||||
public void ReadInput()
|
||||
{
|
||||
using var inputStream = Assembly.GetExecutingAssembly()
|
||||
.GetManifestResourceStream($"AdventOfCode.Inputs.Day{Number}.txt")!;
|
||||
|
||||
Input = new StreamReader(inputStream).ReadToEnd();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
2364
Days/Day1.cs
2364
Days/Day1.cs
File diff suppressed because it is too large
Load Diff
19
Days/Day2.cs
19
Days/Day2.cs
@@ -1,19 +0,0 @@
|
||||
using Spectre.Console;
|
||||
|
||||
namespace AdventOfCode.Days;
|
||||
|
||||
public class Day2 : Day
|
||||
{
|
||||
public override int Number { get; } = 2;
|
||||
public override string Name { get; } = "Second day";
|
||||
|
||||
public override void RunPart1()
|
||||
{
|
||||
AnsiConsole.WriteLine("Day 2 result");
|
||||
}
|
||||
|
||||
public override void RunPart2()
|
||||
{
|
||||
AnsiConsole.WriteLine("Day 2 result");
|
||||
}
|
||||
}
|
||||
25
EnumerableExtensions.cs
Normal file
25
EnumerableExtensions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace AdventOfCode;
|
||||
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static IEnumerable<TSource> Flatten<TSource>(this TSource root, Func<TSource, IEnumerable<TSource>> flattener)
|
||||
{
|
||||
var toVisit = new Stack<TSource>();
|
||||
toVisit.Push(root);
|
||||
|
||||
while (toVisit.TryPop(out var node))
|
||||
{
|
||||
yield return node;
|
||||
|
||||
foreach (var child in flattener(node))
|
||||
{
|
||||
toVisit.Push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
|
||||
{
|
||||
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
|
||||
}
|
||||
}
|
||||
1000
Inputs/Day1.txt
Normal file
1000
Inputs/Day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
BIN
Preview.png
Normal file
BIN
Preview.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 80 KiB |
60
Program.cs
60
Program.cs
@@ -1,22 +1,74 @@
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using AdventOfCode;
|
||||
using AdventOfCode.Days;
|
||||
using BenchmarkDotNet.Running;
|
||||
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()
|
||||
.Where(t => t.IsAssignableTo(typeof(Day)) && t.GetConstructor(Type.EmptyTypes) != null && !t.IsAbstract)
|
||||
.Select(t => (Day)Activator.CreateInstance(t)!);
|
||||
|
||||
var select = new SelectionPrompt<Day>()
|
||||
.Title("[cyan]Select a [yellow]day[/] to run:[/]")
|
||||
.AddChoices(days);
|
||||
.AddChoices(days.OrderByDescending(d => d.Number));
|
||||
|
||||
var selectedDay = AnsiConsole.Prompt(select);
|
||||
|
||||
var stopWatch = new Stopwatch();
|
||||
|
||||
// Read input
|
||||
try
|
||||
{
|
||||
selectedDay.ReadInput();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AnsiConsole.WriteException(e);
|
||||
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
// Part 1
|
||||
AnsiConsole.MarkupLine($"[cyan]Running [yellow]{selectedDay}[/]...[/]\n");
|
||||
|
||||
AnsiConsole.MarkupLine("[cyan]Part [yellow]1[/] result:[/]");
|
||||
selectedDay.RunPart1();
|
||||
|
||||
try
|
||||
{
|
||||
stopWatch.Start();
|
||||
selectedDay.RunPart1();
|
||||
stopWatch.Stop();
|
||||
|
||||
AnsiConsole.MarkupLine($"[red]Approximate run time: [yellow]{stopWatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000:F3} ms[/][/]");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AnsiConsole.WriteException(e);
|
||||
}
|
||||
|
||||
stopWatch.Reset();
|
||||
|
||||
// Part 2
|
||||
AnsiConsole.MarkupLine("\n[cyan]Part [yellow]2[/] result:[/]");
|
||||
selectedDay.RunPart2();
|
||||
|
||||
try
|
||||
{
|
||||
stopWatch.Start();
|
||||
selectedDay.RunPart2();
|
||||
stopWatch.Stop();
|
||||
|
||||
AnsiConsole.MarkupLine($"[red]Approximate run time: [yellow]{stopWatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000:F3} ms[/][/]");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AnsiConsole.WriteException(e);
|
||||
}
|
||||
15
README.md
Normal file
15
README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Advent of Code
|
||||
|
||||

|
||||
|
||||
Advent of Code made in **C#**, using **[Spectre.Console](https://spectreconsole.net/)** for **console display** and **[BenchmarkDotNet](https://benchmarkdotnet.org/)** for **benchmarks**
|
||||
|
||||
## Build and run
|
||||
|
||||
- **Install [.Net 7 SDK](https://dotnet.microsoft.com/en-us/download)** if needed
|
||||
- **Clone** the repository
|
||||
- In a shell, execute `dotnet run`
|
||||
|
||||
## Benchmarks
|
||||
|
||||
> Todo...
|
||||
17
StringExtensions.cs
Normal file
17
StringExtensions.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace AdventOfCode;
|
||||
|
||||
public static partial class StringExtensions
|
||||
{
|
||||
public static IEnumerable<string> ReadAllLines(this StringReader reader)
|
||||
{
|
||||
while (reader.ReadLine() is { } line)
|
||||
{
|
||||
yield return line;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> ReadAllLines(this string text)
|
||||
{
|
||||
return new StringReader(text).ReadAllLines();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user