Add Day 9
This commit is contained in:
parent
00bfb7d46e
commit
4816b640bf
5 changed files with 142 additions and 8 deletions
27
.vscode/launch.json
vendored
27
.vscode/launch.json
vendored
|
|
@ -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"
|
||||
|
|
|
|||
8
.vscode/tasks.json
vendored
8
.vscode/tasks.json
vendored
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
93
src/2024/09/Program.cs
Normal file
93
src/2024/09/Program.cs
Normal 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);
|
||||
}
|
||||
15
src/2024/09/aoc-2024-09.csproj
Normal file
15
src/2024/09/aoc-2024-09.csproj
Normal 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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue