94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
namespace AoC24;
|
|
|
|
using AoC24.Common;
|
|
using AoCLevelInputProvider;
|
|
using Parsing;
|
|
using Parsing.Schema;
|
|
|
|
public class Level2Solver : LevelSolverBase
|
|
{
|
|
public override int LevelNumber
|
|
{
|
|
get { return 2; }
|
|
}
|
|
|
|
protected override InputSchemaBuilder DefineInputSchema(InputSchemaBuilder schemaBuilder)
|
|
{
|
|
return schemaBuilder
|
|
.Repeat()
|
|
.Expect(InputType.Integer)
|
|
.EndRepetition();
|
|
}
|
|
|
|
private class MyDistanceChecker : DataStreamConstraintVerifier<int>.IIntegerDataStreamConstraint
|
|
{
|
|
public int Verify(int leftValue, int rightValue)
|
|
{
|
|
return Math.Abs(leftValue - rightValue);
|
|
}
|
|
}
|
|
|
|
private class MyTrendChecker : DataStreamConstraintVerifier<int>.IIntegerDataStreamConstraint
|
|
{
|
|
public int Verify(int leftValue, int rightValue)
|
|
{
|
|
return (leftValue - rightValue) < 0 ? -1 : 1;
|
|
}
|
|
}
|
|
|
|
public override string SolveFirstStar()
|
|
{
|
|
var data = this.GetData()
|
|
.AsListRows<int>();
|
|
|
|
var distanceChecker = new MyDistanceChecker();
|
|
|
|
var trendChecker = new MyTrendChecker();
|
|
|
|
var verifier = new DataStreamConstraintVerifier<int>()
|
|
.WithDistanceChecker(distanceChecker)
|
|
.WithDistanceLimits(1, 3)
|
|
.WithTrendChecker(trendChecker);
|
|
|
|
int numSafeReports = 0;
|
|
|
|
foreach(var row in data)
|
|
{
|
|
if(verifier.OnData(row).Verify())
|
|
{
|
|
numSafeReports++;
|
|
}
|
|
}
|
|
|
|
return numSafeReports.ToString();
|
|
}
|
|
|
|
public override string SolveSecondStar()
|
|
{
|
|
var data = this.GetData()
|
|
.AsListRows<int>();
|
|
|
|
var distanceChecker = new MyDistanceChecker();
|
|
|
|
var trendChecker = new MyTrendChecker();
|
|
|
|
var verifier = new DataStreamConstraintVerifier<int>()
|
|
.WithDistanceChecker(distanceChecker)
|
|
.WithDistanceLimits(1, 3)
|
|
.WithTrendChecker(trendChecker)
|
|
.WithAllowedIgnores(1);
|
|
|
|
int numSafeReports = 0;
|
|
|
|
foreach(var row in data)
|
|
{
|
|
if(verifier.OnData(row).Verify())
|
|
{
|
|
numSafeReports++;
|
|
}
|
|
}
|
|
|
|
return numSafeReports.ToString();
|
|
}
|
|
}
|