Compare commits

27 Commits

Author SHA1 Message Date
0c3a8478d4 [2k25] Add day 1 2025-12-01 22:14:56 +01:00
f8843866ca Migrato to .Net 10 2025-12-01 22:13:17 +01:00
69a941d34d Add day 25 2025-01-24 10:19:13 +01:00
d647742ca0 Add day 24 2025-01-24 10:19:13 +01:00
bc79ea9fa6 Add day 23 2025-01-24 10:19:13 +01:00
c0b05d45bc Add day 22 2025-01-24 10:19:13 +01:00
ebd5d67fff Add day 21 2025-01-24 10:19:13 +01:00
217cabac34 Add day 20 2025-01-24 10:19:13 +01:00
0ff21ba937 Add day 19 2025-01-24 10:19:13 +01:00
ed8f30c972 Add day 18 2025-01-24 10:19:12 +01:00
380eeda47b Add day 17 2025-01-24 10:19:12 +01:00
8f8448a0e7 Add day 16 2025-01-24 10:19:12 +01:00
be3fbcd34c Add day 15 2025-01-24 10:19:12 +01:00
ab3ff19db3 Add day 14 2025-01-24 10:19:12 +01:00
10429f3302 Add day 13 2025-01-24 10:19:12 +01:00
5f08adc3cb Add day 12 2025-01-24 10:19:12 +01:00
6e433fcf3d Add day 11 2025-01-24 10:19:12 +01:00
d839ab7ec9 Add day 10 2025-01-24 10:19:12 +01:00
8b46a65d56 Add day 9 2025-01-24 10:19:11 +01:00
ef9892502e Add day 8 2025-01-24 10:19:11 +01:00
9b865a12b1 Add day 7 2025-01-24 10:19:11 +01:00
9920041301 Add day 6 2025-01-24 10:19:11 +01:00
323bb83e56 Add day 5 2025-01-24 10:19:11 +01:00
ca58bd804b Add day 4 2025-01-24 10:19:11 +01:00
bbaa3289f6 Add day 3 2025-01-24 10:19:11 +01:00
83ae67a03a Add day 2 2025-01-24 10:19:11 +01:00
2590458b13 Add day 1 2025-01-24 10:19:10 +01:00
4 changed files with 4841 additions and 1064 deletions

View File

@@ -2,19 +2,20 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="Spectre.Console" Version="0.45.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
<PackageReference Include="Spectre.Console" Version="0.54.0" />
</ItemGroup>
<ItemGroup>
<None Remove="Inputs\Day1.txt" />
<EmbeddedResource Include="Inputs\Day1.txt" />
<None Remove="Inputs\*" />
<EmbeddedResource Include="Inputs\*" />
</ItemGroup>
</Project>

View File

@@ -5,96 +5,83 @@ namespace AdventOfCode.Days;
public class Day1 : Day
{
public override int Number => 1;
public override string Name => "Trebuchet?!";
private static readonly (string, int)[] WrittenDigits =
[
("one" , 1),
("two" , 2),
("three", 3),
("four" , 4),
("five" , 5),
("six" , 6),
("seven", 7),
("eight", 8),
("nine" , 9)
];
public override string Name => "Secret Entrance";
public override void RunPart1(bool display = true)
{
var calibrationSum = 0;
var password = 0;
var dialPosition = 50;
foreach (var line in Input.AsSpan().EnumerateLines())
foreach (var line in Input.EnumerateLines())
{
var firstDigitIndex = line.IndexOfAnyInRange('0', '9');
var lastDigitIndex = line.LastIndexOfAnyInRange('0', '9');
var direction = line[0];
var offset = int.Parse(line[1..]);
var firstDigit = line[firstDigitIndex] - '0';
var lastDigit = line[lastDigitIndex] - '0';
var sign = direction is 'L'
? -1
: +1;
calibrationSum += firstDigit * 10 + lastDigit;
dialPosition = MathMod(dialPosition + (offset * sign), 100);
if (dialPosition is 0)
{
password++;
}
}
if (display)
{
AnsiConsole.MarkupLine($"[green]Sum of calibration values is: [yellow]{calibrationSum}[/][/]");
AnsiConsole.MarkupLine($"[green]Safe password is: [yellow]{password}[/][/]");
}
}
public override void RunPart2(bool display = true)
{
var calibrationSum = 0;
var password = 0;
var dialPosition = 50;
foreach (var line in Input.AsSpan().EnumerateLines())
foreach (var line in Input.EnumerateLines())
{
var minFirstDigitIndex = line.IndexOfAnyInRange('0', '9');
var maxLastDigitIndex = line.LastIndexOfAnyInRange('0', '9');
var direction = line[0];
var offset = int.Parse(line[1..]);
var firstDigitValue = 0;
if (minFirstDigitIndex == -1)
var originalSign = Math.Sign(dialPosition);
var sign = direction is 'L'
? -1
: +1;
dialPosition = dialPosition + (offset * sign);
int clickCount;
if (dialPosition is 0)
{
minFirstDigitIndex = int.MaxValue;
clickCount = 1;
}
else
{
firstDigitValue = line[minFirstDigitIndex] - '0';
}
clickCount = Math.Abs(dialPosition) / 100; // Number of times the dial passed by 0
var lastDigitValue = 0;
if (maxLastDigitIndex == -1)
{
maxLastDigitIndex = int.MinValue;
}
else
{
lastDigitValue = line[maxLastDigitIndex] - '0';
}
foreach (var (writtenDigit, value) in WrittenDigits)
{
var firstWrittenDigitIndex = line.IndexOf(writtenDigit);
if (firstWrittenDigitIndex != -1 && firstWrittenDigitIndex < minFirstDigitIndex)
// If we did a loop around it counts as a "click" too
if (originalSign != 0 && originalSign != Math.Sign(dialPosition))
{
minFirstDigitIndex = firstWrittenDigitIndex;
firstDigitValue = value;
}
var lastWrittenDigitIndex = line.LastIndexOf(writtenDigit);
if (lastWrittenDigitIndex != -1 && lastWrittenDigitIndex > maxLastDigitIndex)
{
maxLastDigitIndex = lastWrittenDigitIndex;
lastDigitValue = value;
clickCount++;
}
}
calibrationSum += 10 * firstDigitValue + lastDigitValue;
dialPosition = MathMod(dialPosition, 100);
password += clickCount;
}
if (display)
{
AnsiConsole.MarkupLine($"[green]Sum of calibration values is: [yellow]{calibrationSum}[/][/]");
AnsiConsole.MarkupLine($"[green]Safe password is: [yellow]{password}[/][/]");
}
}
private static int MathMod(int left, int right)
{
return (Math.Abs(left * right) + left) % right;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
namespace AdventOfCode;
public static partial class StringExtensions
public static class StringExtensions
{
public static IEnumerable<string> ReadAllLines(this StringReader reader)
{
@@ -14,4 +14,25 @@ public static partial class StringExtensions
{
return new StringReader(text).ReadAllLines();
}
public static char[,] AsCharGrid(this string text)
{
var lines = text.ReadAllLines().ToArray();
var lineCount = lines.Length;
var columnCount = lines[0].Length;
var grid = new char[lineCount, columnCount];
for (int i = 0; i < lineCount; i++)
{
var line = lines[i];
for (int j = 0; j < columnCount; j++)
{
grid[i, j] = line[j];
}
}
return grid;
}
}