{ 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 . } unit UPolynomial; {$mode ObjFPC}{$H+} interface uses Classes, SysUtils, UBigInt; type TInt64Array = array of Int64; { TBigIntPolynomial } TBigIntPolynomial = object private FCoefficients: array of TBigInt; public function CalcValueAt(const AX: Int64): TBigInt; function IsEqualTo(const AOther: TBigIntPolynomial): Boolean; 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 } 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; 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; class function TBigIntPolynomial.Create(const ACoefficients: array of TBigInt): TBigIntPolynomial; var high, i: integer; begin high := 1; for i := Length(ACoefficients) - 1 downto 1 do if ACoefficients[i] <> 0 then begin high := i; Break; end; SetLength(Result.FCoefficients, high + 1); for i := 0 to high do Result.FCoefficients[i] := ACoefficients[i]; 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.