Added custom constructors for TCosmicExpansion and TStepCounter to simplify test case setup

This commit is contained in:
Stefan Müller 2023-12-23 20:16:18 +01:00 committed by Stefan Müller
parent 2bb89c952b
commit c3ecaf59fa
4 changed files with 12 additions and 48 deletions

View File

@ -35,17 +35,17 @@ type
TCosmicExpansion = class(TSolver)
private
FExpansionFactor: Integer;
FColumnExpansion, FRowExpansion: specialize TList<Integer>;
FGalaxies: specialize TList<TPoint>;
procedure InitColumnExpansion(const ASize: Integer);
public
constructor Create;
constructor Create(const AExpansionFactor: Integer = 999999);
destructor Destroy; override;
procedure ProcessDataLine(const ALine: string); override;
procedure Finish; override;
function GetDataFileName: string; override;
function GetPuzzleName: string; override;
function GetExpansionFactor: Integer; virtual;
end;
implementation
@ -64,8 +64,9 @@ begin
end;
end;
constructor TCosmicExpansion.Create;
constructor TCosmicExpansion.Create(const AExpansionFactor: Integer);
begin
FExpansionFactor := AExpansionFactor;
FColumnExpansion := specialize TList<Integer>.Create;
FRowExpansion := specialize TList<Integer>.Create;
FGalaxies := specialize TList<TPoint>.Create;
@ -110,12 +111,12 @@ begin
for k := Min(FGalaxies[i].X, FGalaxies[j].X) to Max(FGalaxies[i].X, FGalaxies[j].X) - 1 do
begin
Inc(FPart1, 1 + FColumnExpansion[k]);
Inc(FPart2, 1 + FColumnExpansion[k] * GetExpansionFactor);
Inc(FPart2, 1 + FColumnExpansion[k] * FExpansionFactor);
end;
for k := Min(FGalaxies[i].Y, FGalaxies[j].Y) + 1 to Max(FGalaxies[i].Y, FGalaxies[j].Y) do
begin
Inc(FPart1, 1 + FRowExpansion[k]);
Inc(FPart2, 1 + FRowExpansion[k] * GetExpansionFactor);
Inc(FPart2, 1 + FRowExpansion[k] * FExpansionFactor);
end;
end;
end;
@ -130,10 +131,5 @@ begin
Result := 'Day 11: Cosmic Expansion';
end;
function TCosmicExpansion.GetExpansionFactor: Integer;
begin
Result := 999999;
end;
end.

View File

@ -38,8 +38,7 @@ type
function GetPosition(constref APoint: TPoint): Char;
procedure SetPosition(constref APoint: TPoint; const AValue: Char);
public
property MaxSteps: Integer read FMaxSteps write FMaxSteps;
constructor Create;
constructor Create(const AMaxSteps: Integer = 64);
destructor Destroy; override;
procedure ProcessDataLine(const ALine: string); override;
procedure Finish; override;
@ -90,9 +89,9 @@ begin
FLines[APoint.Y] := s;
end;
constructor TStepCounter.Create;
constructor TStepCounter.Create(const AMaxSteps: Integer);
begin
FMaxSteps := 64;
FMaxSteps := AMaxSteps;
FLines := TStringList.Create;
end;

View File

@ -45,13 +45,6 @@ type
procedure TestPart1;
end;
{ TFactor10CosmicExpansion }
TFactor10CosmicExpansion = class(TCosmicExpansion)
public
function GetExpansionFactor: Integer; override;
end;
{ TCosmicExpansionExampleFactor10TestCase }
TCosmicExpansionExampleFactor10TestCase = class(TExampleEngineBaseTest)
@ -61,12 +54,6 @@ type
procedure TestPart2;
end;
{ TFactor100CosmicExpansion }
TFactor100CosmicExpansion = class(TCosmicExpansion)
function GetExpansionFactor: Integer; override;
end;
{ TCosmicExpansionExampleFactor100TestCase }
TCosmicExpansionExampleFactor100TestCase = class(TExampleEngineBaseTest)
@ -107,18 +94,11 @@ begin
AssertEquals(374, FSolver.GetResultPart1);
end;
{ TFactor10CosmicExpansion }
function TFactor10CosmicExpansion.GetExpansionFactor: Integer;
begin
Result := 9;
end;
{ TCosmicExpansionExampleFactor10TestCase }
function TCosmicExpansionExampleFactor10TestCase.CreateSolver: ISolver;
begin
Result := TFactor10CosmicExpansion.Create;
Result := TCosmicExpansion.Create(9);
end;
procedure TCosmicExpansionExampleFactor10TestCase.TestPart2;
@ -126,18 +106,11 @@ begin
AssertEquals(1030, FSolver.GetResultPart2);
end;
{ TFactor100CosmicExpansion }
function TFactor100CosmicExpansion.GetExpansionFactor: Integer;
begin
Result := 99;
end;
{ TCosmicExpansionExampleFactor100TestCase }
function TCosmicExpansionExampleFactor100TestCase.CreateSolver: ISolver;
begin
Result := TFactor100CosmicExpansion.Create;
Result := TCosmicExpansion.Create(99);
end;
procedure TCosmicExpansionExampleFactor100TestCase.TestPart2;

View File

@ -61,12 +61,8 @@ end;
{ TStepCounterMax6ExampleTestCase }
function TStepCounterMax6ExampleTestCase.CreateSolver: ISolver;
var
solver: TStepCounter;
begin
solver := TStepCounter.Create;
solver.MaxSteps := 6;
Result := solver;
Result := TStepCounter.Create(6);
end;
procedure TStepCounterMax6ExampleTestCase.TestPart1;