Compare commits
27 Commits
2k23
...
0c3a8478d4
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c3a8478d4 | |||
| f8843866ca | |||
| 69a941d34d | |||
| d647742ca0 | |||
| bc79ea9fa6 | |||
| c0b05d45bc | |||
| ebd5d67fff | |||
| 217cabac34 | |||
| 0ff21ba937 | |||
| ed8f30c972 | |||
| 380eeda47b | |||
| 8f8448a0e7 | |||
| be3fbcd34c | |||
| ab3ff19db3 | |||
| 10429f3302 | |||
| 5f08adc3cb | |||
| 6e433fcf3d | |||
| d839ab7ec9 | |||
| 8b46a65d56 | |||
| ef9892502e | |||
| 9b865a12b1 | |||
| 9920041301 | |||
| 323bb83e56 | |||
| ca58bd804b | |||
| bbaa3289f6 | |||
| 83ae67a03a | |||
| 2590458b13 |
@@ -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>
|
||||
|
||||
103
Days/Day1.cs
103
Days/Day1.cs
@@ -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;
|
||||
}
|
||||
}
|
||||
5768
Inputs/Day1.txt
5768
Inputs/Day1.txt
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user