Add Day 7
This commit is contained in:
parent
367cd56a42
commit
d9bc639366
5 changed files with 118 additions and 0 deletions
11
.vscode/launch.json
vendored
11
.vscode/launch.json
vendored
|
|
@ -69,6 +69,17 @@
|
|||
"cwd": "${workspaceFolder}",
|
||||
"stopAtEntry": false,
|
||||
"console": "internalConsole"
|
||||
},
|
||||
{
|
||||
"name": "aoc-2024-07 Debug",
|
||||
"type": "coreclr",
|
||||
"request": "launch",
|
||||
"preLaunchTask": "dotnet: build aoc-2024-07",
|
||||
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-07/debug/aoc-2024-07.dll",
|
||||
"args": ["part-two", "data/2024/07/dev.txt"],
|
||||
"cwd": "${workspaceFolder}",
|
||||
"stopAtEntry": false,
|
||||
"console": "internalConsole"
|
||||
}
|
||||
]
|
||||
}
|
||||
8
.vscode/tasks.json
vendored
8
.vscode/tasks.json
vendored
|
|
@ -50,6 +50,14 @@
|
|||
"group": "build",
|
||||
"problemMatcher": [],
|
||||
"label": "dotnet: build aoc-2024-06"
|
||||
},
|
||||
{
|
||||
"type": "dotnet",
|
||||
"task": "build aoc-2024-07.csproj",
|
||||
"file": "src/2024/07/aoc-2024-07.csproj",
|
||||
"group": "build",
|
||||
"problemMatcher": [],
|
||||
"label": "dotnet: build aoc-2024-07"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-05", "src\2024\05\
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-06", "src\2024\06\aoc-2024-06.csproj", "{EEC6EF36-EE16-4DB4-9AE8-CF0234751458}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-07", "src\2024\07\aoc-2024-07.csproj", "{58941BB2-E4AC-40F0-AA92-BEF51DEFC859}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -50,6 +52,10 @@ Global
|
|||
{EEC6EF36-EE16-4DB4-9AE8-CF0234751458}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EEC6EF36-EE16-4DB4-9AE8-CF0234751458}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EEC6EF36-EE16-4DB4-9AE8-CF0234751458}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{FAF70800-E2C3-4AD7-B433-86C1F20380F7} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
|
||||
|
|
@ -58,5 +64,6 @@ Global
|
|||
{49021FCE-1CA9-430F-99B2-8E5C14A48394} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
|
||||
{D108A6AB-B974-4BD9-B97A-CB1D1A2F3920} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
|
||||
{EEC6EF36-EE16-4DB4-9AE8-CF0234751458} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
|
||||
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
|||
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