2024-12-02 18:51:49 +01:00
|
|
|
|
namespace AoC24;
|
|
|
|
|
|
|
|
|
|
using AoC24.Common;
|
|
|
|
|
using AoCLevelInputProvider;
|
|
|
|
|
using Parsing;
|
|
|
|
|
using Parsing.Schema;
|
|
|
|
|
|
2024-12-04 12:47:10 +01:00
|
|
|
|
public class Level1Solver : FullTextLevelSolverBase
|
2024-12-02 18:51:49 +01:00
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|