namespace AoC24; using AoC24.Common; using AoCLevelInputProvider; using Parsing; using Parsing.Schema; public class Level2Solver : FullTextLevelSolverBase { public override int LevelNumber { get { return 2; } } protected override InputSchemaBuilder DefineInputSchema(InputSchemaBuilder schemaBuilder) { return schemaBuilder .Repeat() .Expect(InputType.Integer) .EndRepetition(); } private class MyDistanceChecker : DataStreamConstraintVerifier.IIntegerDataStreamConstraint { public int Verify(int leftValue, int rightValue) { return Math.Abs(leftValue - rightValue); } } private class MyTrendChecker : DataStreamConstraintVerifier.IIntegerDataStreamConstraint { public int Verify(int leftValue, int rightValue) { return (leftValue - rightValue) < 0 ? -1 : 1; } } public override string SolveFirstStar() { var data = this.GetData() .AsListRows(); var distanceChecker = new MyDistanceChecker(); var trendChecker = new MyTrendChecker(); var verifier = new DataStreamConstraintVerifier() .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(); var distanceChecker = new MyDistanceChecker(); var trendChecker = new MyTrendChecker(); var verifier = new DataStreamConstraintVerifier() .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(); } }