1
0
Fork 0

Add Day 9

This commit is contained in:
Jöran Malek 2024-12-26 13:21:12 +01:00
parent 00bfb7d46e
commit 4816b640bf
5 changed files with 142 additions and 8 deletions

27
.vscode/launch.json vendored
View file

@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-01", "preLaunchTask": "dotnet: build aoc-2024-01",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-01/debug/aoc-2024-01.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -21,7 +21,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-02", "preLaunchTask": "dotnet: build aoc-2024-02",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-02/debug/aoc-2024-02.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -32,7 +32,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-03", "preLaunchTask": "dotnet: build aoc-2024-03",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-03/debug/aoc-2024-03.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -43,7 +43,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-04", "preLaunchTask": "dotnet: build aoc-2024-04",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-04/debug/aoc-2024-04.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -54,7 +54,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-05", "preLaunchTask": "dotnet: build aoc-2024-05",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-05/debug/aoc-2024-05.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -65,7 +65,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-06", "preLaunchTask": "dotnet: build aoc-2024-06",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-06/debug/aoc-2024-06.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -76,7 +76,7 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-07", "preLaunchTask": "dotnet: build aoc-2024-07",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-07/debug/aoc-2024-07.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"
@ -87,7 +87,18 @@
"request": "launch", "request": "launch",
"preLaunchTask": "dotnet: build aoc-2024-08", "preLaunchTask": "dotnet: build aoc-2024-08",
"program": "${workspaceFolder}/artifacts/bin/aoc-2024-08/debug/aoc-2024-08.dll", "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}", "cwd": "${workspaceFolder}",
"stopAtEntry": false, "stopAtEntry": false,
"console": "internalConsole" "console": "internalConsole"

8
.vscode/tasks.json vendored
View file

@ -66,6 +66,14 @@
"group": "build", "group": "build",
"problemMatcher": [], "problemMatcher": [],
"label": "dotnet: build aoc-2024-08" "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"
} }
] ]
} }

View file

@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-08", "src\2024\08\
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "core", "src\core\core.csproj", "{9E2BD5D6-55DD-4760-AD30-75B168F03F60}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "core", "src\core\core.csproj", "{9E2BD5D6-55DD-4760-AD30-75B168F03F60}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-09", "src\2024\09\aoc-2024-09.csproj", "{BFF5A025-BCA5-426C-AA21-237E1059AEA7}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{9E2BD5D6-55DD-4760-AD30-75B168F03F60}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{FAF70800-E2C3-4AD7-B433-86C1F20380F7} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {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} {EEC6EF36-EE16-4DB4-9AE8-CF0234751458} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859} = {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} {44BBC1CF-D2FC-4906-9691-B439EC9EFB8C} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
{BFF5A025-BCA5-426C-AA21-237E1059AEA7} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

93
src/2024/09/Program.cs Normal file
View file

@ -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);
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>aoc_2024_09</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\core\core.csproj" />
</ItemGroup>
</Project>