From d9bc6393666db97ac5b44caffe2f386a9128ded8 Mon Sep 17 00:00:00 2001 From: AliveDevil Date: Sun, 22 Dec 2024 21:27:07 +0100 Subject: [PATCH] Add Day 7 --- .vscode/launch.json | 11 +++++ .vscode/tasks.json | 8 ++++ AdventOfCode.sln | 7 +++ src/2024/07/Program.cs | 81 ++++++++++++++++++++++++++++++++++ src/2024/07/aoc-2024-07.csproj | 11 +++++ 5 files changed, 118 insertions(+) create mode 100644 src/2024/07/Program.cs create mode 100644 src/2024/07/aoc-2024-07.csproj diff --git a/.vscode/launch.json b/.vscode/launch.json index 0cc5409..3cea784 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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" } ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index efc390a..a2c7f14 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -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" } ] } \ No newline at end of file diff --git a/AdventOfCode.sln b/AdventOfCode.sln index a86866b..3694faa 100644 --- a/AdventOfCode.sln +++ b/AdventOfCode.sln @@ -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 diff --git a/src/2024/07/Program.cs b/src/2024/07/Program.cs new file mode 100644 index 0000000..d55beb9 --- /dev/null +++ b/src/2024/07/Program.cs @@ -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 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 results, in ReadOnlySpan 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)[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); +} diff --git a/src/2024/07/aoc-2024-07.csproj b/src/2024/07/aoc-2024-07.csproj new file mode 100644 index 0000000..1eaef44 --- /dev/null +++ b/src/2024/07/aoc-2024-07.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + aoc_2024_07 + enable + enable + + +