advent_of_code_2024/Level2/Level2Solver.cs

94 lines
2.4 KiB
C#
Raw Normal View History

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