Initial Days

This commit is contained in:
henrik
2024-12-03 20:34:16 +01:00
commit fb3a16c772
13 changed files with 252 additions and 0 deletions

20
Day1/Day1.csproj Normal file
View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TUnit" Version="0.4.51" />
</ItemGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

21
Day1/Program.cs Normal file
View File

@@ -0,0 +1,21 @@
string[] inputs = File.ReadAllLines("input.txt");
var a = new List<int>();
var b = new List<int>();
foreach (string input in inputs)
{
string[] tokens = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
a.Add(int.Parse(tokens[0]));
b.Add(int.Parse(tokens[1]));
}
a = a.OrderBy(x => x).ToList();
b = b.OrderBy(x => x).ToList();
int totalDistance = a.Select((t, i) => Math.Abs(t - b[i])).Sum();
Console.WriteLine(totalDistance);
var aBucket = a.GroupBy(x => x);
var star2 = aBucket.Select(bucket => bucket.Key * b.Count(x => x == bucket.Key) * bucket.Count()).Sum();
Console.WriteLine(star2);