Compare commits

20 Commits

Author SHA1 Message Date
828f9bc3dc Add day 1 2024-06-03 17:29:56 +02:00
63c866f5a0 Add benchmark support using -b 2022-12-17 21:48:16 +01:00
147c348ee1 Add approximate run time using StopWatch 2022-12-17 19:30:38 +01:00
aa833569da Add day 15 2022-12-17 19:17:15 +01:00
0dfc6b59de Add day 14 2022-12-16 23:02:19 +01:00
331d064f66 Add day 13 2022-12-16 14:23:58 +01:00
4ff214e321 Update README.md 2022-12-15 21:42:10 +01:00
254f7f2e71 Add day 12 2022-12-15 21:28:42 +01:00
2b4d26e725 Add README 2022-12-15 10:00:26 +01:00
fd794bab16 Add day 11 part 2 2022-12-15 09:03:11 +01:00
a985d2e00c Add day 11 part 1 2022-12-14 13:39:01 +01:00
fe91c0f07a Add day 10 2022-12-12 15:21:30 +01:00
94e9159f8e Add day 9 2022-12-09 15:28:31 +01:00
ccdf246d5c Add day 8 2022-12-08 20:01:32 +01:00
5d6835b2e6 Add day 7 2022-12-08 15:51:56 +01:00
489ecc6d8d Add day 6 2022-12-06 15:51:21 +01:00
57002d439a Add day 5 2022-12-06 15:30:29 +01:00
a2f6d92f25 Add day 4 2022-12-05 16:15:50 +01:00
5aeb2669c9 Add day 3 2022-12-05 14:20:59 +01:00
6e55404b19 Add day 2 2022-12-05 14:20:59 +01:00
12 changed files with 1246 additions and 2313 deletions

4
.gitignore vendored
View File

@@ -3,4 +3,6 @@ obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/.idea.AdventOfCode/.idea
.idea/.idea.AdventOfCode/.idea
AdventOfCode.sln.DotSettings.user
BenchmarkDotNet.Artifacts

View File

@@ -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
View 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);
}
}

View File

@@ -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()
{

File diff suppressed because it is too large Load Diff

View File

@@ -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
View 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

File diff suppressed because it is too large Load Diff

BIN
Preview.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

View File

@@ -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
View File

@@ -0,0 +1,15 @@
# Advent of Code
![CLI Preview](Preview.png)
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
View 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();
}
}