Added solution for "Day 9: Mirage Maintenance", part 2
This commit is contained in:
parent
f69d60e5db
commit
09baefc881
|
@ -31,7 +31,7 @@ type
|
||||||
TMirageMaintenance = class(TSolver)
|
TMirageMaintenance = class(TSolver)
|
||||||
private
|
private
|
||||||
FN: Integer;
|
FN: Integer;
|
||||||
FLagrangePolynomialsInN: specialize TList<Int64>;
|
FLagrangePolynomialsInN, FLagrangePolynomialsInMinusOne: specialize TList<Int64>;
|
||||||
procedure CalcLagrangePolynomials;
|
procedure CalcLagrangePolynomials;
|
||||||
public
|
public
|
||||||
constructor Create;
|
constructor Create;
|
||||||
|
@ -48,19 +48,23 @@ implementation
|
||||||
|
|
||||||
procedure TMirageMaintenance.CalcLagrangePolynomials;
|
procedure TMirageMaintenance.CalcLagrangePolynomials;
|
||||||
var
|
var
|
||||||
sign, i, j: Integer;
|
sign1, sign2, i, j: Integer;
|
||||||
begin
|
begin
|
||||||
FLagrangePolynomialsInN.Clear;
|
FLagrangePolynomialsInN.Clear;
|
||||||
|
FLagrangePolynomialsInMinusOne.Clear;
|
||||||
if FN mod 2 = 0 then
|
if FN mod 2 = 0 then
|
||||||
sign := -1
|
sign1 := -1
|
||||||
else
|
else
|
||||||
sign := 1;
|
sign1 := 1;
|
||||||
|
sign2 := 1;
|
||||||
|
|
||||||
// Calculates the polynomials in N and -1.
|
// Calculates the polynomials in N and -1.
|
||||||
for i := 0 to FN - 1 do
|
for i := 0 to FN - 1 do
|
||||||
begin
|
begin
|
||||||
FLagrangePolynomialsInN.Add(sign);
|
FLagrangePolynomialsInN.Add(sign1);
|
||||||
sign := -sign;
|
sign1 := -sign1;
|
||||||
|
FLagrangePolynomialsInMinusOne.Add(sign2);
|
||||||
|
sign2 := -sign2;
|
||||||
|
|
||||||
if i < FN - FN div 2 then
|
if i < FN - FN div 2 then
|
||||||
begin
|
begin
|
||||||
|
@ -81,18 +85,38 @@ begin
|
||||||
for j := 2 to FN - i - 1 do
|
for j := 2 to FN - i - 1 do
|
||||||
FLagrangePolynomialsInN[i] := FLagrangePolynomialsInN[i] div j;
|
FLagrangePolynomialsInN[i] := FLagrangePolynomialsInN[i] div j;
|
||||||
end;
|
end;
|
||||||
end;
|
|
||||||
|
if i < FN div 2 then
|
||||||
|
begin
|
||||||
|
// Multiplies by the non-cancelled numerator terms.
|
||||||
|
for j := FN - i to FN do
|
||||||
|
FLagrangePolynomialsInMinusOne[i] := FLagrangePolynomialsInMinusOne[i] * j;
|
||||||
|
// Divides by the non-cancelled denominator terms.
|
||||||
|
for j := 2 to i + 1 do
|
||||||
|
FLagrangePolynomialsInMinusOne[i] := FLagrangePolynomialsInMinusOne[i] div j;
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
// Multiplies by the non-cancelled numerator terms.
|
||||||
|
for j := i + 2 to FN do
|
||||||
|
FLagrangePolynomialsInMinusOne[i] := FLagrangePolynomialsInMinusOne[i] * j;
|
||||||
|
// Divides by the non-cancelled denominator terms.
|
||||||
|
for j := 2 to FN - i - 1 do
|
||||||
|
FLagrangePolynomialsInMinusOne[i] := FLagrangePolynomialsInMinusOne[i] div j;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TMirageMaintenance.Create;
|
constructor TMirageMaintenance.Create;
|
||||||
begin
|
begin
|
||||||
FLagrangePolynomialsInN := specialize TList<Int64>.Create;
|
FLagrangePolynomialsInN := specialize TList<Int64>.Create;
|
||||||
|
FLagrangePolynomialsInMinusOne := specialize TList<Int64>.Create;
|
||||||
FN := 0;
|
FN := 0;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TMirageMaintenance.Destroy;
|
destructor TMirageMaintenance.Destroy;
|
||||||
begin
|
begin
|
||||||
FLagrangePolynomialsInN.Free;
|
FLagrangePolynomialsInN.Free;
|
||||||
|
FLagrangePolynomialsInMinusOne.Free;
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -114,6 +138,8 @@ begin
|
||||||
y := StrToInt(split[i]);
|
y := StrToInt(split[i]);
|
||||||
p := y * FLagrangePolynomialsInN[i];
|
p := y * FLagrangePolynomialsInN[i];
|
||||||
Inc(FPart1, p);
|
Inc(FPart1, p);
|
||||||
|
p := y * FLagrangePolynomialsInMinusOne[i];
|
||||||
|
Inc(FPart2, p);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ end;
|
||||||
|
|
||||||
procedure TMirageMaintenanceFullDataTestCase.TestPart2;
|
procedure TMirageMaintenanceFullDataTestCase.TestPart2;
|
||||||
begin
|
begin
|
||||||
AssertEquals(-1, FSolver.GetResultPart2);
|
AssertEquals(1108, FSolver.GetResultPart2);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TMirageMaintenanceExampleTestCase }
|
{ TMirageMaintenanceExampleTestCase }
|
||||||
|
@ -79,7 +79,7 @@ end;
|
||||||
|
|
||||||
procedure TMirageMaintenanceExampleTestCase.TestPart2;
|
procedure TMirageMaintenanceExampleTestCase.TestPart2;
|
||||||
begin
|
begin
|
||||||
AssertEquals(-1, FSolver.GetResultPart2);
|
AssertEquals(2, FSolver.GetResultPart2);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
|
|
Loading…
Reference in New Issue