implement level1 and level2
This commit is contained in:
14
Level1/Level1.csproj
Normal file
14
Level1/Level1.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../Common/Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
74
Level1/Level1Solver.cs
Normal file
74
Level1/Level1Solver.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
namespace AoC24;
|
||||
|
||||
using AoC24.Common;
|
||||
using AoCLevelInputProvider;
|
||||
using Parsing;
|
||||
using Parsing.Schema;
|
||||
|
||||
public class Level1Solver : LevelSolverBase
|
||||
{
|
||||
public override int LevelNumber
|
||||
{
|
||||
get { return 1; }
|
||||
}
|
||||
|
||||
protected override InputSchemaBuilder DefineInputSchema(InputSchemaBuilder schemaBuilder)
|
||||
{
|
||||
return schemaBuilder
|
||||
.Repeat(2)
|
||||
.Expect(InputType.Integer)
|
||||
.EndRepetition();
|
||||
}
|
||||
|
||||
public override string SolveFirstStar()
|
||||
{
|
||||
var data = this.GetData()
|
||||
.AsListColumns<int>();
|
||||
|
||||
var sortedLeftColumn = data[0];
|
||||
sortedLeftColumn.Sort();
|
||||
var sortedRightColumn = data[1];
|
||||
sortedRightColumn.Sort();
|
||||
|
||||
var sumDifferences = 0;
|
||||
|
||||
for (var i = 0; i < sortedLeftColumn.Count; i++)
|
||||
{
|
||||
sumDifferences += Math.Abs(sortedLeftColumn[i]-sortedRightColumn[i]);
|
||||
}
|
||||
|
||||
return sumDifferences.ToString();
|
||||
}
|
||||
|
||||
public override string SolveSecondStar()
|
||||
{
|
||||
var data = this.GetData()
|
||||
.AsListColumns<int>();
|
||||
|
||||
var uniqueLeftColumn = data[0].Distinct();
|
||||
var rightColumn = data[1];
|
||||
|
||||
Dictionary<int, int> numInstances = new Dictionary<int, int>();
|
||||
foreach(var value in uniqueLeftColumn)
|
||||
{
|
||||
numInstances[value] = 0;
|
||||
}
|
||||
|
||||
foreach(var value in rightColumn)
|
||||
{
|
||||
if (numInstances.ContainsKey(value))
|
||||
{
|
||||
numInstances[value] = numInstances[value] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
var sumScore = 0;
|
||||
|
||||
foreach(var key in numInstances.Keys)
|
||||
{
|
||||
sumScore = sumScore + numInstances[key] * key;
|
||||
}
|
||||
|
||||
return sumScore.ToString();
|
||||
}
|
||||
}
|
||||
24
Level1/Program.cs
Normal file
24
Level1/Program.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using AoC24;
|
||||
|
||||
var levelSolver = new Level1Solver();
|
||||
var solution1 = levelSolver.SolveFirstStar();
|
||||
var solution2 = levelSolver.SolveSecondStar();
|
||||
|
||||
if (!string.IsNullOrEmpty(solution1))
|
||||
{
|
||||
Console.WriteLine("Solution for example 1 is: " + solution1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Example 1 has not been solved yet!");
|
||||
}
|
||||
if (!string.IsNullOrEmpty(solution2))
|
||||
{
|
||||
Console.WriteLine("Solution for example 2 is: " + solution2);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Example 2 has not been solved yet!");
|
||||
}
|
||||
Reference in New Issue
Block a user