Add Day 7
This commit is contained in:
parent
367cd56a42
commit
d9bc639366
5 changed files with 118 additions and 0 deletions
81
src/2024/07/Program.cs
Normal file
81
src/2024/07/Program.cs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
using System.Buffers;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Globalization;
|
||||
|
||||
using ConsoleAppFramework;
|
||||
|
||||
var builder = ConsoleApp.Create();
|
||||
builder.Add("part-one", static ([Argument, FileExists] string file) =>
|
||||
{
|
||||
Console.WriteLine($"Result: {ReadFile(file, true)}");
|
||||
});
|
||||
builder.Add("part-two", static ([Argument, FileExists] string file) =>
|
||||
{
|
||||
Console.WriteLine($"Result: {ReadFile(file, false)}");
|
||||
});
|
||||
builder.Run(args);
|
||||
|
||||
static long ReadFile(string file, bool simple)
|
||||
{
|
||||
long result = 0;
|
||||
using (var fileReader = new StreamReader(file))
|
||||
{
|
||||
ArrayBufferWriter<long> valueBuffer = new();
|
||||
while (fileReader.ReadLine() is { } line)
|
||||
{
|
||||
valueBuffer.ResetWrittenCount();
|
||||
var colon = line.IndexOf(':');
|
||||
var expected = long.Parse(line.AsSpan(0, line.IndexOf(':')));
|
||||
var valueSpan = line.AsSpan(colon + 1).Trim();
|
||||
foreach (var valueRange in valueSpan.Split(' '))
|
||||
{
|
||||
var numberSpan = valueSpan[valueRange];
|
||||
if (numberSpan.IsWhiteSpace())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
valueBuffer.GetSpan(1)[0] = int.Parse(numberSpan, NumberStyles.None);
|
||||
valueBuffer.Advance(1);
|
||||
}
|
||||
|
||||
var values = valueBuffer.WrittenSpan;
|
||||
if (IsValid(expected, values[..1], values[1..]))
|
||||
{
|
||||
result = checked(result + expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
bool IsValid(long expected, ReadOnlySpan<long> results, in ReadOnlySpan<long> values)
|
||||
{
|
||||
if (values.Length == 0)
|
||||
{
|
||||
return results.Contains(expected);
|
||||
}
|
||||
|
||||
var optionLength = simple ? 2 : 3;
|
||||
var newResults = new long[results.Length * optionLength];
|
||||
var factor = values[0];
|
||||
var factorPow10 = (int)Math.Pow(10, (int)Math.Floor(Math.Log10(factor) + 1));
|
||||
|
||||
for (int i = 0; i < results.Length; i++)
|
||||
{
|
||||
var value = results[i];
|
||||
((ReadOnlySpan<long>)[value + factor, value * factor]).CopyTo(newResults.AsSpan(i * optionLength, 2));
|
||||
if (!simple)
|
||||
{
|
||||
newResults[i * optionLength + 2] = value * factorPow10 + factor;
|
||||
}
|
||||
}
|
||||
|
||||
return IsValid(expected, newResults, values[1..]);
|
||||
}
|
||||
}
|
||||
|
||||
class FileExistsAttribute : ValidationAttribute
|
||||
{
|
||||
public override bool IsValid(object? value) => value is string path && File.Exists(path);
|
||||
}
|
||||
11
src/2024/07/aoc-2024-07.csproj
Normal file
11
src/2024/07/aoc-2024-07.csproj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>aoc_2024_07</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue