2024-03-14 11:42:09 +01:00
|
|
|
{
|
|
|
|
Solutions to the Advent Of Code.
|
|
|
|
Copyright (C) 2024 Stefan Müller
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
}
|
|
|
|
|
|
|
|
unit UPolynomial;
|
|
|
|
|
|
|
|
{$mode ObjFPC}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, UBigInt;
|
|
|
|
|
|
|
|
type
|
|
|
|
TInt64Array = array of Int64;
|
|
|
|
|
|
|
|
{ TBigIntPolynomial }
|
|
|
|
|
|
|
|
TBigIntPolynomial = object
|
|
|
|
private
|
|
|
|
FCoefficients: array of TBigInt;
|
2024-04-04 20:24:56 +02:00
|
|
|
function GetDegree: Integer;
|
|
|
|
function GetCoefficient(const AIndex: Integer): TBigInt;
|
2024-03-14 11:42:09 +01:00
|
|
|
public
|
2024-04-04 20:24:56 +02:00
|
|
|
property Degree: Integer read GetDegree;
|
|
|
|
property Coefficient[const AIndex: Integer]: TBigInt read GetCoefficient;
|
2024-03-14 11:42:09 +01:00
|
|
|
function CalcValueAt(const AX: Int64): TBigInt;
|
2024-04-04 20:26:21 +02:00
|
|
|
function ScaleVariable(const AScaleFactor: TBigInt): TBigIntPolynomial;
|
2024-05-23 22:07:58 +02:00
|
|
|
function IsEqualTo(const AOther: TBigIntPolynomial): Boolean;
|
2024-05-20 01:04:18 +02:00
|
|
|
function ToString: string;
|
2024-03-14 11:42:09 +01:00
|
|
|
class function Create(const ACoefficients: array of TBigInt): TBigIntPolynomial; static;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ Operators }
|
|
|
|
|
|
|
|
operator = (const A, B: TBigIntPolynomial): Boolean;
|
|
|
|
operator <> (const A, B: TBigIntPolynomial): Boolean;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TBigIntPolynomial }
|
|
|
|
|
2024-04-04 20:24:56 +02:00
|
|
|
function TBigIntPolynomial.GetDegree: Integer;
|
|
|
|
begin
|
|
|
|
Result := Length(FCoefficients) - 1;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TBigIntPolynomial.GetCoefficient(const AIndex: Integer): TBigInt;
|
|
|
|
begin
|
|
|
|
Result := FCoefficients[AIndex];
|
|
|
|
end;
|
|
|
|
|
2024-03-14 11:42:09 +01:00
|
|
|
function TBigIntPolynomial.CalcValueAt(const AX: Int64): TBigInt;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := TBigInt.Zero;
|
|
|
|
for i := High(FCoefficients) downto 0 do
|
|
|
|
Result := Result * AX + FCoefficients[i];
|
|
|
|
end;
|
|
|
|
|
2024-04-04 20:26:21 +02:00
|
|
|
function TBigIntPolynomial.ScaleVariable(const AScaleFactor: TBigInt): TBigIntPolynomial;
|
|
|
|
var
|
|
|
|
len, i: Integer;
|
|
|
|
factor: TBigInt;
|
|
|
|
begin
|
|
|
|
if AScaleFactor <> TBigInt.Zero then
|
|
|
|
begin
|
|
|
|
len := Length(FCoefficients);
|
|
|
|
SetLength(Result.FCoefficients, len);
|
|
|
|
Result.FCoefficients[0] := FCoefficients[0];
|
|
|
|
factor := AScaleFactor;
|
|
|
|
for i := 1 to len - 1 do begin
|
|
|
|
Result.FCoefficients[i] := FCoefficients[i] * factor;
|
|
|
|
factor := factor * AScaleFactor;
|
|
|
|
end;
|
|
|
|
end
|
2024-05-23 22:11:22 +02:00
|
|
|
else begin
|
|
|
|
SetLength(Result.FCoefficients, 1);
|
|
|
|
Result.FCoefficients[0] := TBigInt.Zero;
|
|
|
|
end;
|
2024-04-04 20:26:21 +02:00
|
|
|
end;
|
|
|
|
|
2024-05-23 22:07:58 +02:00
|
|
|
function TBigIntPolynomial.IsEqualTo(const AOther: TBigIntPolynomial): Boolean;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
if Length(FCoefficients) = Length(AOther.FCoefficients) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
for i := 0 to Length(FCoefficients) - 1 do
|
|
|
|
if FCoefficients[i] <> AOther.FCoefficients[i] then
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
2024-05-20 01:04:18 +02:00
|
|
|
function TBigIntPolynomial.ToString: string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
Result := FCoefficients[0].ToString;
|
|
|
|
for i := 1 to Length(FCoefficients) - 1 do
|
|
|
|
if i > 1 then
|
|
|
|
Result := Result + ' + ' + FCoefficients[i].ToString + ' * x^' + IntToStr(i)
|
|
|
|
else
|
|
|
|
Result := Result + ' + ' + FCoefficients[i].ToString + ' * x';
|
|
|
|
end;
|
|
|
|
|
2024-03-14 11:42:09 +01:00
|
|
|
class function TBigIntPolynomial.Create(const ACoefficients: array of TBigInt): TBigIntPolynomial;
|
|
|
|
var
|
|
|
|
high, i: integer;
|
|
|
|
begin
|
2024-04-04 20:24:56 +02:00
|
|
|
high := -1;
|
|
|
|
for i := Length(ACoefficients) - 1 downto 0 do
|
2024-03-14 11:42:09 +01:00
|
|
|
if ACoefficients[i] <> 0 then
|
|
|
|
begin
|
|
|
|
high := i;
|
|
|
|
Break;
|
|
|
|
end;
|
2024-04-04 20:24:56 +02:00
|
|
|
if high >= 0 then
|
|
|
|
begin
|
|
|
|
SetLength(Result.FCoefficients, high + 1);
|
|
|
|
for i := 0 to high do
|
|
|
|
Result.FCoefficients[i] := ACoefficients[i];
|
|
|
|
end;
|
2024-03-14 11:42:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ Operators }
|
|
|
|
|
|
|
|
operator = (const A, B: TBigIntPolynomial): Boolean;
|
|
|
|
begin
|
|
|
|
Result := A.IsEqualTo(B);
|
|
|
|
end;
|
|
|
|
|
|
|
|
operator <> (const A, B: TBigIntPolynomial): Boolean;
|
|
|
|
begin
|
|
|
|
Result := not A.IsEqualTo(B);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|