From 4816b640bf9fdad4e86cdd4c97bb8b625c88b48b Mon Sep 17 00:00:00 2001 From: AliveDevil Date: Thu, 26 Dec 2024 13:21:12 +0100 Subject: [PATCH] Add Day 9 --- .vscode/launch.json | 27 +++++++--- .vscode/tasks.json | 8 +++ AdventOfCode.sln | 7 +++ src/2024/09/Program.cs | 93 ++++++++++++++++++++++++++++++++++ src/2024/09/aoc-2024-09.csproj | 15 ++++++ 5 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 src/2024/09/Program.cs create mode 100644 src/2024/09/aoc-2024-09.csproj diff --git a/.vscode/launch.json b/.vscode/launch.json index c85dbeb..5fac21b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-01", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-01/debug/aoc-2024-01.dll", - "args": ["part-one", "data/2024/01/input.txt"], + "args": ["part-one", "test-results/2024/01/input.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -21,7 +21,7 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-02", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-02/debug/aoc-2024-02.dll", - "args": ["part-two", "data/2024/02/dev.txt"], + "args": ["part-two", "test-results/2024/02/dev.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -32,7 +32,7 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-03", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-03/debug/aoc-2024-03.dll", - "args": ["part-two", "data/2024/03/dev2.txt"], + "args": ["part-two", "test-results/2024/03/dev2.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -43,7 +43,7 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-04", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-04/debug/aoc-2024-04.dll", - "args": ["part-one", "data/2024/04/dev.txt"], + "args": ["part-one", "test-results/2024/04/dev.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -54,7 +54,7 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-05", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-05/debug/aoc-2024-05.dll", - "args": ["part-one", "data/2024/05/dev.txt"], + "args": ["part-one", "test-results/2024/05/dev.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -65,7 +65,7 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-06", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-06/debug/aoc-2024-06.dll", - "args": ["part-two", "data/2024/06/dev.txt"], + "args": ["part-two", "test-results/2024/06/dev.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -76,7 +76,7 @@ "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"], + "args": ["part-two", "test-results/2024/07/dev.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" @@ -87,7 +87,18 @@ "request": "launch", "preLaunchTask": "dotnet: build aoc-2024-08", "program": "${workspaceFolder}/artifacts/bin/aoc-2024-08/debug/aoc-2024-08.dll", - "args": ["part-one", "data/2024/08/dev.txt"], + "args": ["part-one", "test-results/2024/08/dev.txt"], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + "console": "internalConsole" + }, + { + "name": "aoc-2024-09 Debug", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "dotnet: build aoc-2024-09", + "program": "${workspaceFolder}/artifacts/bin/aoc-2024-09/debug/aoc-2024-09.dll", + "args": ["part-one", "test-results/2024/09/dev.txt"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "internalConsole" diff --git a/.vscode/tasks.json b/.vscode/tasks.json index aa0cdd5..7b6d3ed 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -66,6 +66,14 @@ "group": "build", "problemMatcher": [], "label": "dotnet: build aoc-2024-08" + }, + { + "type": "dotnet", + "task": "build aoc-2024-09.csproj", + "file": "src/2024/09/aoc-2024-09.csproj", + "group": "build", + "problemMatcher": [], + "label": "dotnet: build aoc-2024-09" } ] } \ No newline at end of file diff --git a/AdventOfCode.sln b/AdventOfCode.sln index 129e7bd..87a24f2 100644 --- a/AdventOfCode.sln +++ b/AdventOfCode.sln @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-08", "src\2024\08\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "core", "src\core\core.csproj", "{9E2BD5D6-55DD-4760-AD30-75B168F03F60}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-09", "src\2024\09\aoc-2024-09.csproj", "{BFF5A025-BCA5-426C-AA21-237E1059AEA7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -68,6 +70,10 @@ Global {9E2BD5D6-55DD-4760-AD30-75B168F03F60}.Debug|Any CPU.Build.0 = Debug|Any CPU {9E2BD5D6-55DD-4760-AD30-75B168F03F60}.Release|Any CPU.ActiveCfg = Release|Any CPU {9E2BD5D6-55DD-4760-AD30-75B168F03F60}.Release|Any CPU.Build.0 = Release|Any CPU + {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {FAF70800-E2C3-4AD7-B433-86C1F20380F7} = {37CA8017-085A-4F6A-BADB-535F929C10B9} @@ -78,5 +84,6 @@ Global {EEC6EF36-EE16-4DB4-9AE8-CF0234751458} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {58941BB2-E4AC-40F0-AA92-BEF51DEFC859} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {44BBC1CF-D2FC-4906-9691-B439EC9EFB8C} = {37CA8017-085A-4F6A-BADB-535F929C10B9} + {BFF5A025-BCA5-426C-AA21-237E1059AEA7} = {37CA8017-085A-4F6A-BADB-535F929C10B9} EndGlobalSection EndGlobal diff --git a/src/2024/09/Program.cs b/src/2024/09/Program.cs new file mode 100644 index 0000000..67918fd --- /dev/null +++ b/src/2024/09/Program.cs @@ -0,0 +1,93 @@ +using System.Collections; +using System.Runtime.InteropServices; +using System.Xml; + +using ConsoleAppFramework; + +using core; + +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 fragment) +{ + LinkedList<(int? Id, int Length)> linkedList = []; + + int rolling = 0; + bool isFile = true; + using (var fileReader = new StreamReader(file)) + { + while (fileReader.Read() is int c and not -1 && char.IsDigit((char)c)) + { + linkedList.AddLast((((_, isFile) = ((int?)(isFile ? rolling++ : null), !isFile)).Item1, c - '0')); + } + } + + for (var node = linkedList.Last; node is not null; node = node.Previous) + { + if (node.Value.Id is null) + { + continue; + } + + ref var block = ref node.ValueRef; + var compact = block.Length; + for (var free = linkedList.First; free is not null && free != node; free = free.Next) + { + if (free.Value.Id is not null) + { + continue; + } + + var move = fragment ? Math.Min(compact, free.Value.Length) : compact; + var remaining = free.Value.Length - move; + if (remaining < 0) + { + continue; + } + + LinkedListNode<(int? Id, int Length)> moved = free; + if (remaining > 0) + { + moved = linkedList.AddBefore(free, (null, move)); + free.ValueRef.Length = remaining; + } + + moved.ValueRef.Id = block.Id; + remaining = compact - move; + if ((compact -= move) != 0) + { + linkedList.AddAfter(node, (null, remaining)); + block.Length = compact; + } + else + { + block.Id = null; + break; + } + } + } + + return linkedList.Aggregate((Result: 0L, Index: 0), (result, node) => + { + var sum = result.Result; + + if (node.Id is int id) + { + for (int i = 0; i < node.Length; i++) + { + sum = checked(sum + (result.Index + i) * id); + } + } + + return (sum, result.Index + node.Length); + }, static result => result.Result); +} diff --git a/src/2024/09/aoc-2024-09.csproj b/src/2024/09/aoc-2024-09.csproj new file mode 100644 index 0000000..eab9e5b --- /dev/null +++ b/src/2024/09/aoc-2024-09.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + aoc_2024_09 + enable + enable + + + + + + + \ No newline at end of file