2023-12-12 15:47:58 +01:00
|
|
|
{
|
|
|
|
Solutions to the Advent Of Code.
|
2024-10-15 11:45:44 +02:00
|
|
|
Copyright (C) 2023-2024 Stefan Müller
|
2023-12-12 15:47:58 +01:00
|
|
|
|
|
|
|
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 UHotSprings;
|
|
|
|
|
|
|
|
{$mode ObjFPC}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2024-11-09 23:17:47 +01:00
|
|
|
Classes, SysUtils, Math, Generics.Collections, USolver, UCommon, UMultiIndexEnumerator, UBinomialCoefficients;
|
2023-12-12 15:47:58 +01:00
|
|
|
|
|
|
|
const
|
|
|
|
COperationalChar = '.';
|
|
|
|
CDamagedChar = '#';
|
|
|
|
CWildcardChar = '?';
|
2024-11-12 19:16:12 +01:00
|
|
|
CPart2Repetition = 5;
|
2023-12-12 15:47:58 +01:00
|
|
|
|
|
|
|
type
|
2024-10-15 11:45:44 +02:00
|
|
|
TValidationLengths = array of array of Integer;
|
|
|
|
|
|
|
|
{ TDamage }
|
|
|
|
|
|
|
|
TDamage = record
|
2024-11-12 19:16:12 +01:00
|
|
|
Start, Length, CharsRemaining: Integer;
|
2024-10-15 11:45:44 +02:00
|
|
|
end;
|
|
|
|
TDamages = specialize TList<TDamage>;
|
2024-11-15 19:08:33 +01:00
|
|
|
|
2024-11-19 22:58:15 +01:00
|
|
|
TBlockCombinationsCache = specialize THashMap<Int64, Int64>;
|
|
|
|
TCombinationsCache = specialize TObjectHashMap<string, TBlockCombinationsCache>;
|
|
|
|
|
2024-11-15 19:08:33 +01:00
|
|
|
{ TBlock }
|
|
|
|
|
|
|
|
TBlock = class
|
|
|
|
private
|
|
|
|
FPattern: string;
|
|
|
|
FDamages: TDamages;
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache: TBlockCombinationsCache;
|
2024-11-15 19:08:33 +01:00
|
|
|
procedure ParseDamages;
|
|
|
|
public
|
2024-11-19 22:58:15 +01:00
|
|
|
constructor Create(const APattern: string; constref ACombinationsCache: TBlockCombinationsCache);
|
2024-11-15 19:08:33 +01:00
|
|
|
destructor Destroy; override;
|
|
|
|
property Pattern: string read FPattern;
|
|
|
|
// List of damages in this block, containing exactly one entry for each sequence of consecutive damage characters in
|
|
|
|
// the block's pattern, ordered such that a damage with lower index is further left.
|
|
|
|
// For example, if Pattern is '??##?#?', then Damages would have 2 entries.
|
|
|
|
property Damages: TDamages read FDamages;
|
2024-11-19 22:58:15 +01:00
|
|
|
property CombinationsCache: TBlockCombinationsCache read FCombinationsCache;
|
2024-11-15 19:08:33 +01:00
|
|
|
end;
|
|
|
|
TBlocks = specialize TObjectList<TBlock>;
|
2024-10-15 11:45:44 +02:00
|
|
|
|
2024-11-19 18:14:14 +01:00
|
|
|
{ TAccumulatedCombinationsMultiIndexStrategy }
|
|
|
|
|
|
|
|
// Adds accumulated combinations to the enumerable strategy to allow calculation of combinations on the fly, and
|
|
|
|
// therefore early rejection of invalid multi-index configurations.
|
|
|
|
TAccumulatedCombinationsMultiIndexStrategy = class(TEnumerableMultiIndexStrategy)
|
|
|
|
private
|
|
|
|
FAccumulatedCombinations: TInt64Array;
|
|
|
|
protected
|
|
|
|
function CalcCombinations(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer): Int64; virtual;
|
|
|
|
abstract;
|
|
|
|
function UpdateCombinations(const AValidationResult: TIndexValidationResult; constref ACurrentIndexArray:
|
|
|
|
TIndexArray; const ACurrentIndex: Integer): TIndexValidationResult;
|
|
|
|
public
|
|
|
|
function GetCombinations: Int64;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TConditionRecord = class;
|
|
|
|
|
|
|
|
{ TValidationsToBlockAssignments }
|
|
|
|
|
|
|
|
// Enumerable strategy that enumerates all valid assignments of ranges of validation numbers to individual blocks in
|
|
|
|
// the form of start and stop indices.
|
|
|
|
TValidationsToBlockAssignments = class(TAccumulatedCombinationsMultiIndexStrategy)
|
|
|
|
private
|
|
|
|
FConditionRecord: TConditionRecord;
|
|
|
|
protected
|
|
|
|
function CalcCombinations(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer): Int64; override;
|
|
|
|
public
|
|
|
|
constructor Create(constref AConditionRecord: TConditionRecord);
|
|
|
|
function GetCardinality: Integer; override;
|
|
|
|
function TryGetStartIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer;
|
|
|
|
out AStartIndexValue: Integer): Boolean; override;
|
|
|
|
function ValidateIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer):
|
|
|
|
TIndexValidationResult; override;
|
|
|
|
end;
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
{ TDamageToValidationAssignments }
|
2024-11-09 00:42:11 +01:00
|
|
|
|
2024-11-19 18:14:14 +01:00
|
|
|
// Enumerable strategy that enumerates all valid assignments of each damage in the block to a specific validation
|
|
|
|
// number from the validation numbers that have been assigned to the block, as indicated by start and stop indices.
|
2024-11-17 23:54:08 +01:00
|
|
|
TDamageToValidationAssignments = class(TEnumerableMultiIndexStrategy)
|
2024-11-09 00:42:11 +01:00
|
|
|
private
|
2024-11-19 18:14:14 +01:00
|
|
|
FConditionRecord: TConditionRecord;
|
|
|
|
FBlock: TBlock;
|
2024-11-09 00:42:11 +01:00
|
|
|
FValidationStartIndex, FValidationStopIndex: Integer;
|
2024-11-19 18:14:14 +01:00
|
|
|
// Calculates "span", the length of all damages for one validation number combined.
|
2024-11-09 00:42:11 +01:00
|
|
|
function CalcValidationSpan(constref ACurrentIndexArray: TIndexArray; const ALastDamageIndex, AValidationNumber:
|
|
|
|
Integer): Integer;
|
|
|
|
public
|
2024-11-19 18:14:14 +01:00
|
|
|
constructor Create(constref AConditionRecord: TConditionRecord; constref ABlock: TBlock;
|
|
|
|
const AStartValidationIndex, AStopValidationIndex: Integer);
|
2024-11-09 00:42:11 +01:00
|
|
|
function GetCardinality: Integer; override;
|
|
|
|
function TryGetStartIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer;
|
|
|
|
out AStartIndexValue: Integer): Boolean; override;
|
|
|
|
function ValidateIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer):
|
|
|
|
TIndexValidationResult; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TValidationPositionInfo }
|
|
|
|
|
|
|
|
TValidationPositionInfo = record
|
2024-11-12 19:16:12 +01:00
|
|
|
ValidationIndex, MinStart, MaxStart: Integer;
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TValidationPositionInfos = specialize TList<TValidationPositionInfo>;
|
|
|
|
|
|
|
|
{ TValidationPositionOffsets }
|
|
|
|
|
2024-11-19 18:14:14 +01:00
|
|
|
// Enumerable strategy that enumerates all valid assignments of start positions (positions mean character indices in
|
|
|
|
// the block patterns) of validation numbers that have been assigned to damages in the current block, as indicated by
|
|
|
|
// provided TValidationPositionInfos.
|
2024-11-17 23:54:08 +01:00
|
|
|
TValidationPositionOffsets = class(TAccumulatedCombinationsMultiIndexStrategy)
|
2024-11-09 00:42:11 +01:00
|
|
|
private
|
2024-11-17 23:54:08 +01:00
|
|
|
FConditionRecord: TConditionRecord;
|
2024-11-09 00:42:11 +01:00
|
|
|
FPositionInfos: TValidationPositionInfos;
|
2024-11-19 18:14:14 +01:00
|
|
|
FBlockLength, FValidationStartIndex, FValidationStopIndex: Integer;
|
2024-11-17 23:54:08 +01:00
|
|
|
protected
|
|
|
|
function CalcCombinations(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer): Int64; override;
|
2024-11-09 00:42:11 +01:00
|
|
|
public
|
2024-11-17 23:54:08 +01:00
|
|
|
constructor Create(constref AConditionRecord: TConditionRecord; constref APositionInfos: TValidationPositionInfos;
|
2024-11-19 18:14:14 +01:00
|
|
|
const ABlockLength, AValidationStartIndex, AValidationStopIndex: Integer);
|
2024-11-09 00:42:11 +01:00
|
|
|
function GetCardinality: Integer; override;
|
|
|
|
function TryGetStartIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer;
|
|
|
|
out AStartIndexValue: Integer): Boolean; override;
|
|
|
|
function ValidateIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer):
|
|
|
|
TIndexValidationResult; override;
|
|
|
|
end;
|
|
|
|
|
2024-10-15 11:45:44 +02:00
|
|
|
{ TConditionRecord }
|
|
|
|
|
|
|
|
TConditionRecord = class
|
2023-12-12 15:47:58 +01:00
|
|
|
private
|
2024-11-19 18:14:14 +01:00
|
|
|
// List of validation numbers as stated in the problem input.
|
2024-10-15 11:45:44 +02:00
|
|
|
FValidation: TIntegerList;
|
2024-11-09 00:42:11 +01:00
|
|
|
// List of non-empty, maximum-length parts of the pattern without operational springs ("blocks").
|
2024-11-15 19:08:33 +01:00
|
|
|
FBlocks: TBlocks;
|
2024-11-09 00:42:11 +01:00
|
|
|
// Array 'a' of accumulated validation series lengths. 'a[i, j]' denotes the combined length of consecutive
|
2024-10-15 11:45:44 +02:00
|
|
|
// validation numbers from 'FValidation[i]' to 'FValidation[j - 1]' with a single space in between each pair of
|
|
|
|
// them.
|
|
|
|
FValidationLengths: TValidationLengths;
|
|
|
|
// Array 'a' of minimum indices 'a[i]', such that all remaining validation numbers starting at index 'a[i] - 1'
|
2024-11-09 00:42:11 +01:00
|
|
|
// cannot fit into the remaining blocks starting at 'FBlocks[i]'.
|
2024-11-19 18:14:14 +01:00
|
|
|
FMinIndices: TIndexArray;
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache: TCombinationsCache;
|
2024-10-15 11:45:44 +02:00
|
|
|
procedure InitValidationLengths;
|
|
|
|
procedure InitMinIndices;
|
2024-11-15 19:08:33 +01:00
|
|
|
function CalcCombinationsBlockSingleValidation(constref ABlock: TBlock; const AIndex: Integer): Int64;
|
|
|
|
function CalcCombinationsBlockMultiValidations(constref ABlock: TBlock; constref AIndices: TIndexArray;
|
|
|
|
const AStartIndex, AStopIndex: Integer): Int64;
|
2024-11-19 22:58:15 +01:00
|
|
|
function CalcValidationsId(const AStartIndex, AStopIndex: Integer): Int64;
|
2023-12-12 15:47:58 +01:00
|
|
|
public
|
2024-11-19 22:58:15 +01:00
|
|
|
constructor Create(constref ACombinationsCache: TCombinationsCache);
|
2023-12-12 15:47:58 +01:00
|
|
|
destructor Destroy; override;
|
2024-11-09 00:42:11 +01:00
|
|
|
// Adds all non-empty, maximum-length parts of the pattern without operational springs ("blocks").
|
|
|
|
procedure AddBlocks(const APattern: string);
|
2024-10-15 11:45:44 +02:00
|
|
|
function GenerateBlockAssignments: Int64;
|
2024-11-19 18:14:14 +01:00
|
|
|
function CalcCombinationsBlock(constref ABlock: TBlock; const AStartIndex, AStopIndex: Integer): Int64;
|
2024-11-17 23:54:08 +01:00
|
|
|
function CalcCombinationsWildcardSequence(const ASequenceLength, AStartIndex, AStopIndex: Integer): Int64;
|
2024-11-15 19:08:33 +01:00
|
|
|
property Validation: TIntegerList read FValidation;
|
2024-11-19 18:14:14 +01:00
|
|
|
property Blocks: TBlocks read FBlocks;
|
|
|
|
property ValidationLengths: TValidationLengths read FValidationLengths;
|
|
|
|
property MinIndices: TIndexArray read FMinIndices;
|
2024-10-15 11:45:44 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ THotSprings }
|
|
|
|
|
|
|
|
THotSprings = class(TSolver)
|
2024-11-09 23:17:47 +01:00
|
|
|
private
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache: TCombinationsCache;
|
2024-10-15 11:45:44 +02:00
|
|
|
public
|
2024-11-09 23:17:47 +01:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2023-12-12 15:47:58 +01:00
|
|
|
procedure ProcessDataLine(const ALine: string); override;
|
|
|
|
procedure Finish; override;
|
|
|
|
function GetDataFileName: string; override;
|
|
|
|
function GetPuzzleName: string; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2024-11-15 19:08:33 +01:00
|
|
|
{ TBlock }
|
|
|
|
|
|
|
|
procedure TBlock.ParseDamages;
|
|
|
|
var
|
|
|
|
i, len: Integer;
|
|
|
|
damage: TDamage;
|
|
|
|
begin
|
|
|
|
FDamages := TDamages.Create;
|
|
|
|
damage.Length := 0;
|
|
|
|
len := Length(FPattern);
|
|
|
|
for i := 1 to len do
|
|
|
|
// The pattern must only contain damage and wildcard characters here.
|
|
|
|
if FPattern[i] = CDamagedChar then
|
|
|
|
begin
|
|
|
|
if damage.Length = 0 then
|
|
|
|
damage.Start := i;
|
|
|
|
Inc(damage.Length);
|
|
|
|
end
|
|
|
|
else if damage.Length > 0 then
|
|
|
|
begin
|
|
|
|
damage.CharsRemaining := len - damage.Start - damage.Length + 1;
|
|
|
|
FDamages.Add(damage);
|
|
|
|
damage.Length := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if damage.Length > 0 then
|
|
|
|
begin
|
|
|
|
damage.CharsRemaining := 0;
|
|
|
|
FDamages.Add(damage);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2024-11-19 22:58:15 +01:00
|
|
|
constructor TBlock.Create(const APattern: string; constref ACombinationsCache: TBlockCombinationsCache);
|
2024-11-15 19:08:33 +01:00
|
|
|
begin
|
|
|
|
FPattern := APattern;
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache := ACombinationsCache;
|
2024-11-15 19:08:33 +01:00
|
|
|
ParseDamages;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TBlock.Destroy;
|
|
|
|
begin
|
|
|
|
FDamages.Free;
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2024-11-19 18:14:14 +01:00
|
|
|
{ TAccumulatedCombinationsMultiIndexStrategy }
|
|
|
|
|
|
|
|
function TAccumulatedCombinationsMultiIndexStrategy.UpdateCombinations(const AValidationResult: TIndexValidationResult;
|
|
|
|
constref ACurrentIndexArray: TIndexArray; const ACurrentIndex: Integer): TIndexValidationResult;
|
|
|
|
var
|
|
|
|
combinations: Int64;
|
|
|
|
begin
|
|
|
|
Result := AValidationResult;
|
|
|
|
if Result = ivrValid then
|
|
|
|
begin
|
|
|
|
combinations := CalcCombinations(ACurrentIndexArray, ACurrentIndex);
|
|
|
|
if combinations = 0 then
|
|
|
|
Result := ivrSkip
|
|
|
|
else if ACurrentIndex > 0 then
|
|
|
|
FAccumulatedCombinations[ACurrentIndex] := combinations * FAccumulatedCombinations[ACurrentIndex - 1]
|
|
|
|
else begin
|
|
|
|
SetLength(FAccumulatedCombinations, GetCardinality);
|
|
|
|
FAccumulatedCombinations[ACurrentIndex] := combinations;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TAccumulatedCombinationsMultiIndexStrategy.GetCombinations: Int64;
|
|
|
|
begin
|
|
|
|
if FAccumulatedCombinations <> nil then
|
|
|
|
Result := FAccumulatedCombinations[GetCardinality - 1]
|
|
|
|
else
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TValidationsToBlockAssignments }
|
|
|
|
|
|
|
|
function TValidationsToBlockAssignments.CalcCombinations(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex:
|
|
|
|
Integer): Int64;
|
|
|
|
var
|
|
|
|
block: TBlock;
|
|
|
|
start, stop: Integer;
|
|
|
|
begin
|
|
|
|
// 'ACurrentIndexArray[i] - 1' denotes the index of the last validation number assigned to 'Block[i]', and the index
|
|
|
|
// of the first validation number in 'Validation' assigned to 'Block[i + 1]'. If two consecutive values in
|
|
|
|
// 'ACurrentIndexArray' are the same, then the block in between has no numbers assigned to it.
|
|
|
|
block := FConditionRecord.Blocks[ACurrentIndex];
|
|
|
|
if ACurrentIndex > 0 then
|
|
|
|
start := ACurrentIndexArray[ACurrentIndex - 1]
|
|
|
|
else
|
|
|
|
start := 0;
|
|
|
|
stop := ACurrentIndexArray[ACurrentIndex] - 1;
|
|
|
|
if block.Damages.Count > 0 then
|
|
|
|
Result := FConditionRecord.CalcCombinationsBlock(block, start, stop)
|
|
|
|
else
|
|
|
|
Result := FConditionRecord.CalcCombinationsWildcardSequence(Length(block.Pattern), start, stop);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TValidationsToBlockAssignments.Create(constref AConditionRecord: TConditionRecord);
|
|
|
|
begin
|
|
|
|
FConditionRecord := AConditionRecord;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TValidationsToBlockAssignments.GetCardinality: Integer;
|
|
|
|
begin
|
|
|
|
Result := FConditionRecord.Blocks.Count;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TValidationsToBlockAssignments.TryGetStartIndexValue(constref ACurrentIndexArray: TIndexArray;
|
|
|
|
const ACurrentIndex: Integer; out AStartIndexValue: Integer): Boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
if ACurrentIndex + 1 = GetCardinality then
|
|
|
|
AStartIndexValue := FConditionRecord.Validation.Count
|
|
|
|
else if ACurrentIndex > 0 then
|
|
|
|
AStartIndexValue := Max(ACurrentIndexArray[ACurrentIndex - 1], FConditionRecord.MinIndices[ACurrentIndex])
|
|
|
|
else
|
|
|
|
AStartIndexValue := FConditionRecord.MinIndices[ACurrentIndex];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TValidationsToBlockAssignments.ValidateIndexValue(constref ACurrentIndexArray: TIndexArray;
|
|
|
|
const ACurrentIndex: Integer): TIndexValidationResult;
|
|
|
|
var
|
|
|
|
start: Integer;
|
|
|
|
begin
|
|
|
|
if ACurrentIndexArray[ACurrentIndex] > FConditionRecord.Validation.Count then
|
|
|
|
Result := ivrBacktrack
|
|
|
|
else begin
|
|
|
|
if ACurrentIndex > 0 then
|
|
|
|
start := ACurrentIndexArray[ACurrentIndex - 1]
|
|
|
|
else
|
|
|
|
start := 0;
|
|
|
|
|
|
|
|
if FConditionRecord.ValidationLengths[start, ACurrentIndexArray[ACurrentIndex]]
|
|
|
|
<= Length(FConditionRecord.Blocks[ACurrentIndex].Pattern) then
|
|
|
|
Result := ivrValid
|
|
|
|
else
|
|
|
|
Result := ivrBacktrack;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := UpdateCombinations(Result, ACurrentIndexArray, ACurrentIndex);
|
|
|
|
end;
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
{ TDamageToValidationAssignments }
|
2024-11-09 00:42:11 +01:00
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
function TDamageToValidationAssignments.CalcValidationSpan(constref ACurrentIndexArray: TIndexArray;
|
2024-11-09 00:42:11 +01:00
|
|
|
const ALastDamageIndex, AValidationNumber: Integer): Integer;
|
|
|
|
var
|
|
|
|
spanStart: Integer;
|
|
|
|
begin
|
|
|
|
spanStart := ALastDamageIndex;
|
|
|
|
while (spanStart > 0) and (ACurrentIndexArray[spanStart - 1] = AValidationNumber) do
|
|
|
|
Dec(spanStart);
|
2024-11-19 18:14:14 +01:00
|
|
|
Result := FBlock.Damages[ALastDamageIndex].Length;
|
2024-11-09 00:42:11 +01:00
|
|
|
if spanStart < ALastDamageIndex then
|
2024-11-19 18:14:14 +01:00
|
|
|
Inc(Result, FBlock.Damages[ALastDamageIndex].Start - FBlock.Damages[spanStart].Start);
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
|
2024-11-19 18:14:14 +01:00
|
|
|
constructor TDamageToValidationAssignments.Create(constref AConditionRecord: TConditionRecord; constref ABlock: TBlock;
|
|
|
|
const AStartValidationIndex, AStopValidationIndex: Integer);
|
2024-11-09 00:42:11 +01:00
|
|
|
begin
|
2024-11-19 18:14:14 +01:00
|
|
|
FConditionRecord := AConditionRecord;
|
|
|
|
FBlock := ABlock;
|
|
|
|
FValidationStartIndex := AStartValidationIndex;
|
|
|
|
FValidationStopIndex := AStopValidationIndex;
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
function TDamageToValidationAssignments.GetCardinality: Integer;
|
2024-11-09 00:42:11 +01:00
|
|
|
begin
|
2024-11-19 18:14:14 +01:00
|
|
|
Result := FBlock.Damages.Count;
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
function TDamageToValidationAssignments.TryGetStartIndexValue(constref ACurrentIndexArray: TIndexArray;
|
2024-11-09 00:42:11 +01:00
|
|
|
const ACurrentIndex: Integer; out AStartIndexValue: Integer): Boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
if ACurrentIndex > 0 then
|
|
|
|
AStartIndexValue := ACurrentIndexArray[ACurrentIndex - 1]
|
|
|
|
else
|
|
|
|
AStartIndexValue := FValidationStartIndex;
|
|
|
|
end;
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
function TDamageToValidationAssignments.ValidateIndexValue(constref ACurrentIndexArray: TIndexArray;
|
2024-11-09 00:42:11 +01:00
|
|
|
const ACurrentIndex: Integer): TIndexValidationResult;
|
|
|
|
var
|
2024-11-19 18:14:14 +01:00
|
|
|
i, prev: Integer;
|
2024-11-09 00:42:11 +01:00
|
|
|
begin
|
|
|
|
i := ACurrentIndexArray[ACurrentIndex];
|
2024-11-19 18:14:14 +01:00
|
|
|
prev := ACurrentIndex - 1;
|
|
|
|
// Checks maximum index value.
|
2024-11-09 00:42:11 +01:00
|
|
|
if i > FValidationStopIndex then
|
2024-11-19 18:14:14 +01:00
|
|
|
Result := ivrBacktrack
|
2024-11-09 00:42:11 +01:00
|
|
|
// Checks if there is enough space after this damage for remaining validation numbers.
|
2024-11-19 18:14:14 +01:00
|
|
|
else if (i < FValidationStopIndex)
|
|
|
|
and (FConditionRecord.ValidationLengths[i + 1, FValidationStopIndex + 1] + 1 > FBlock.Damages[ACurrentIndex].CharsRemaining) then
|
|
|
|
Result := ivrSkip
|
2024-11-09 00:42:11 +01:00
|
|
|
// Checks if there is enough space before this damage for previous validation numbers.
|
2024-11-19 18:14:14 +01:00
|
|
|
else if (FValidationStartIndex < i)
|
|
|
|
and (FConditionRecord.ValidationLengths[FValidationStartIndex, i] + 1 >= FBlock.Damages[ACurrentIndex].Start) then
|
|
|
|
Result := ivrBacktrack
|
2024-11-09 00:42:11 +01:00
|
|
|
// Checks if there is enough space between previous and this damage for skipped validation numbers.
|
2024-11-19 18:14:14 +01:00
|
|
|
else if (ACurrentIndex > 0)
|
|
|
|
and (ACurrentIndexArray[prev] + 1 < i)
|
|
|
|
and (FConditionRecord.ValidationLengths[ACurrentIndexArray[prev] + 1, i] + 2
|
|
|
|
> FBlock.Damages[ACurrentIndex].Start - FBlock.Damages[prev].Start - FBlock.Damages[prev].Length) then
|
|
|
|
Result := ivrBacktrack
|
2024-11-09 00:42:11 +01:00
|
|
|
// Checks if span is small enough to fit within this validation number.
|
2024-11-19 18:14:14 +01:00
|
|
|
else if FConditionRecord.Validation[i] < CalcValidationSpan(ACurrentIndexArray, ACurrentIndex, i) then
|
|
|
|
Result := ivrSkip
|
|
|
|
else
|
|
|
|
Result := ivrValid;
|
2024-11-17 23:54:08 +01:00
|
|
|
end;
|
|
|
|
|
2024-11-09 00:42:11 +01:00
|
|
|
{ TValidationPositionOffsets }
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
function TValidationPositionOffsets.CalcCombinations(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex:
|
|
|
|
Integer): Int64;
|
|
|
|
var
|
|
|
|
space, start, stop: Integer;
|
2024-11-09 00:42:11 +01:00
|
|
|
begin
|
2024-11-17 23:54:08 +01:00
|
|
|
stop := FPositionInfos[ACurrentIndex].ValidationIndex - 1;
|
|
|
|
if ACurrentIndex > 0 then
|
|
|
|
begin
|
|
|
|
space := ACurrentIndexArray[ACurrentIndex] - ACurrentIndexArray[ACurrentIndex - 1]
|
|
|
|
- FConditionRecord.Validation[FPositionInfos[ACurrentIndex - 1].ValidationIndex] - 2;
|
|
|
|
start := FPositionInfos[ACurrentIndex - 1].ValidationIndex + 1;
|
|
|
|
Result := FConditionRecord.CalcCombinationsWildcardSequence(space, start, stop);
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
// Handles first calculated offset.
|
|
|
|
space := ACurrentIndexArray[0] - 2;
|
2024-11-19 18:14:14 +01:00
|
|
|
Result := FConditionRecord.CalcCombinationsWildcardSequence(space, FValidationStartIndex, stop);
|
2024-11-17 23:54:08 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
if (Result > 0) and (ACurrentIndex + 1 = GetCardinality) then
|
|
|
|
begin
|
|
|
|
// Handles last calculated offset.
|
|
|
|
space := FBlockLength - ACurrentIndexArray[ACurrentIndex] - FConditionRecord.Validation[FPositionInfos.Last.ValidationIndex];
|
2024-11-19 18:14:14 +01:00
|
|
|
start := FPositionInfos.Last.ValidationIndex + 1;
|
|
|
|
Result := Result * FConditionRecord.CalcCombinationsWildcardSequence(space, start, FValidationStopIndex);
|
2024-11-17 23:54:08 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TValidationPositionOffsets.Create(constref AConditionRecord: TConditionRecord; constref APositionInfos:
|
2024-11-19 18:14:14 +01:00
|
|
|
TValidationPositionInfos; const ABlockLength, AValidationStartIndex, AValidationStopIndex: Integer);
|
2024-11-17 23:54:08 +01:00
|
|
|
begin
|
|
|
|
FConditionRecord := AConditionRecord;
|
2024-11-09 00:42:11 +01:00
|
|
|
FPositionInfos := APositionInfos;
|
2024-11-17 23:54:08 +01:00
|
|
|
FBlockLength := ABlockLength;
|
2024-11-19 18:14:14 +01:00
|
|
|
FValidationStartIndex := AValidationStartIndex;
|
|
|
|
FValidationStopIndex := AValidationStopIndex;
|
2024-11-17 23:54:08 +01:00
|
|
|
inherited Create;
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TValidationPositionOffsets.GetCardinality: Integer;
|
|
|
|
begin
|
|
|
|
Result := FPositionInfos.Count;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TValidationPositionOffsets.TryGetStartIndexValue(constref ACurrentIndexArray: TIndexArray;
|
|
|
|
const ACurrentIndex: Integer; out AStartIndexValue: Integer): Boolean;
|
|
|
|
var
|
|
|
|
info: TValidationPositionInfo;
|
|
|
|
begin
|
|
|
|
info := FPositionInfos[ACurrentIndex];
|
|
|
|
AStartIndexValue := info.MinStart;
|
|
|
|
// Adjusts start value to avoid overlap of this validation number with the previous one (the one from previous
|
|
|
|
// position info).
|
|
|
|
if ACurrentIndex > 0 then
|
|
|
|
AStartIndexValue := Max(AStartIndexValue,
|
2024-11-17 23:54:08 +01:00
|
|
|
ACurrentIndexArray[ACurrentIndex - 1] + FConditionRecord.Validation[FPositionInfos[ACurrentIndex - 1].ValidationIndex] + 1);
|
2024-11-09 00:42:11 +01:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TValidationPositionOffsets.ValidateIndexValue(constref ACurrentIndexArray: TIndexArray; const ACurrentIndex:
|
|
|
|
Integer): TIndexValidationResult;
|
|
|
|
begin
|
|
|
|
if ACurrentIndexArray[ACurrentIndex] <= FPositionInfos[ACurrentIndex].MaxStart then
|
|
|
|
Result := ivrValid
|
|
|
|
else
|
|
|
|
Result := ivrBacktrack;
|
2024-11-17 23:54:08 +01:00
|
|
|
|
|
|
|
Result := UpdateCombinations(Result, ACurrentIndexArray, ACurrentIndex);
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
|
2024-10-15 11:45:44 +02:00
|
|
|
{ TConditionRecord }
|
|
|
|
|
|
|
|
procedure TConditionRecord.InitValidationLengths;
|
|
|
|
var
|
|
|
|
i, j: Integer;
|
|
|
|
begin
|
|
|
|
SetLength(FValidationLengths, FValidation.Count + 1, FValidation.Count + 1);
|
|
|
|
for i := 0 to FValidation.Count do
|
|
|
|
begin
|
|
|
|
FValidationLengths[i, i] := 0;
|
|
|
|
for j := i + 1 to FValidation.Count do
|
|
|
|
if FValidationLengths[i, j - 1] <> 0 then
|
|
|
|
FValidationLengths[i, j] := FValidationLengths[i, j - 1] + FValidation[j - 1] + 1
|
|
|
|
else
|
|
|
|
FValidationLengths[i, j] := FValidationLengths[i, j - 1] + FValidation[j - 1]
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TConditionRecord.InitMinIndices;
|
|
|
|
var
|
|
|
|
i, j, patternsLength: Integer;
|
|
|
|
begin
|
2024-11-09 00:42:11 +01:00
|
|
|
SetLength(FMinIndices, FBlocks.Count - 1);
|
2024-11-15 19:08:33 +01:00
|
|
|
patternsLength := Length(FBlocks[FBlocks.Count - 1].Pattern);
|
2024-10-15 11:45:44 +02:00
|
|
|
j := FValidation.Count;
|
2024-11-09 00:42:11 +01:00
|
|
|
for i := FBlocks.Count - 2 downto 0 do
|
2024-10-15 11:45:44 +02:00
|
|
|
begin
|
|
|
|
while (j >= 0) and (FValidationLengths[j, FValidation.Count] <= patternsLength) do
|
|
|
|
Dec(j);
|
|
|
|
FMinIndices[i] := j + 1;
|
2024-11-15 19:08:33 +01:00
|
|
|
patternsLength := patternsLength + 1 + Length(FBlocks[i].Pattern);
|
2024-10-15 11:45:44 +02:00
|
|
|
end;
|
|
|
|
end;
|
2023-12-12 15:47:58 +01:00
|
|
|
|
2024-11-15 19:08:33 +01:00
|
|
|
function TConditionRecord.CalcCombinationsBlockSingleValidation(constref ABlock: TBlock; const AIndex: Integer): Int64;
|
2024-10-15 11:45:44 +02:00
|
|
|
var
|
2024-11-15 19:08:33 +01:00
|
|
|
len, combinedDamagesLength: Integer;
|
2023-12-12 15:47:58 +01:00
|
|
|
begin
|
2024-11-15 19:08:33 +01:00
|
|
|
len := Length(ABlock.Pattern);
|
|
|
|
if len < FValidation[AIndex] then
|
2024-10-15 11:45:44 +02:00
|
|
|
Result := 0
|
2024-11-15 19:08:33 +01:00
|
|
|
else if ABlock.Damages.Count = 0 then
|
|
|
|
Result := len - FValidation[AIndex] + 1
|
2024-10-15 11:45:44 +02:00
|
|
|
else begin
|
2024-11-15 19:08:33 +01:00
|
|
|
combinedDamagesLength := ABlock.Damages.Last.Start + ABlock.Damages.Last.Length - ABlock.Damages.First.Start;
|
2024-10-15 11:45:44 +02:00
|
|
|
if FValidation[AIndex] < combinedDamagesLength then
|
|
|
|
Result := 0
|
|
|
|
else begin
|
|
|
|
Result := Min(Min(Min(
|
2024-11-15 19:08:33 +01:00
|
|
|
ABlock.Damages.First.Start,
|
2024-10-15 11:45:44 +02:00
|
|
|
FValidation[AIndex] - combinedDamagesLength + 1),
|
2024-11-15 19:08:33 +01:00
|
|
|
len - FValidation[AIndex] + 1),
|
|
|
|
ABlock.Damages.Last.CharsRemaining + 1);
|
2024-10-15 11:45:44 +02:00
|
|
|
end;
|
|
|
|
end;
|
2023-12-12 15:47:58 +01:00
|
|
|
end;
|
|
|
|
|
2024-11-15 19:08:33 +01:00
|
|
|
function TConditionRecord.CalcCombinationsBlockMultiValidations(constref ABlock: TBlock; constref AIndices:
|
|
|
|
TIndexArray; const AStartIndex, AStopIndex: Integer): Int64;
|
2024-11-09 00:42:11 +01:00
|
|
|
var
|
|
|
|
i, high: Integer;
|
|
|
|
position: TValidationPositionInfo;
|
|
|
|
positions: TValidationPositionInfos;
|
|
|
|
validationPositionOffsets: TValidationPositionOffsets;
|
|
|
|
offsets: TIndexArray;
|
|
|
|
begin
|
|
|
|
positions := TValidationPositionInfos.Create;
|
|
|
|
high := Length(AIndices) - 1;
|
|
|
|
// Initializes first info record.
|
|
|
|
position.ValidationIndex := AIndices[0];
|
2024-11-15 19:08:33 +01:00
|
|
|
position.MaxStart := ABlock.Damages[0].Start;
|
2024-11-09 00:42:11 +01:00
|
|
|
position.MinStart := 1;
|
|
|
|
for i := 1 to high do
|
|
|
|
if AIndices[i] <> position.ValidationIndex then
|
|
|
|
begin
|
|
|
|
// Finalizes current info record.
|
2024-11-15 19:08:33 +01:00
|
|
|
position.MaxStart := Min(position.MaxStart, ABlock.Damages[i].Start - 1 - FValidation[position.ValidationIndex]);
|
2024-11-09 00:42:11 +01:00
|
|
|
position.MinStart := Max(position.MinStart,
|
2024-11-15 19:08:33 +01:00
|
|
|
ABlock.Damages[i - 1].Start + ABlock.Damages[i - 1].Length - FValidation[position.ValidationIndex]);
|
2024-11-09 00:42:11 +01:00
|
|
|
positions.Add(position);
|
|
|
|
// Initializes next info record.
|
|
|
|
position.ValidationIndex := AIndices[i];
|
2024-11-15 19:08:33 +01:00
|
|
|
position.MaxStart := ABlock.Damages[i].Start;
|
|
|
|
position.MinStart := position.MinStart + FValidationLengths[AIndices[i - 1], AIndices[i]] + 1;
|
2024-11-09 00:42:11 +01:00
|
|
|
end;
|
|
|
|
// Finalizes last info record.
|
2024-11-15 19:08:33 +01:00
|
|
|
position.MaxStart := Min(position.MaxStart, Length(ABlock.Pattern) + 1 - FValidation[position.ValidationIndex]);
|
2024-11-09 00:42:11 +01:00
|
|
|
position.MinStart := Max(position.MinStart,
|
2024-11-15 19:08:33 +01:00
|
|
|
ABlock.Damages[high].Start + ABlock.Damages[high].Length - FValidation[position.ValidationIndex]);
|
2024-11-09 00:42:11 +01:00
|
|
|
positions.Add(position);
|
|
|
|
|
2024-11-09 23:17:47 +01:00
|
|
|
Result := 0;
|
2024-11-17 23:54:08 +01:00
|
|
|
validationPositionOffsets := TValidationPositionOffsets.Create(Self, positions, Length(ABlock.Pattern),
|
|
|
|
AStartIndex, AStopIndex);
|
2024-11-09 00:42:11 +01:00
|
|
|
for offsets in validationPositionOffsets do
|
2024-11-17 23:54:08 +01:00
|
|
|
Result := Result + validationPositionOffsets.GetCombinations;
|
2024-11-09 00:42:11 +01:00
|
|
|
validationPositionOffsets.Free;
|
|
|
|
|
|
|
|
positions.Free;
|
|
|
|
end;
|
|
|
|
|
2024-11-19 22:58:15 +01:00
|
|
|
function TConditionRecord.CalcValidationsId(const AStartIndex, AStopIndex: Integer): Int64;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
// Requires 'FValidations[i] < 32' for each 'i' and 'AStopIndex - AStartIndex < 12'.
|
|
|
|
Result := FValidation[AStartIndex];
|
|
|
|
for i := AStartIndex + 1 to AStopIndex do
|
|
|
|
Result := (Result shl 5) or FValidation[i];
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TConditionRecord.Create(constref ACombinationsCache: TCombinationsCache);
|
2024-11-09 00:42:11 +01:00
|
|
|
begin
|
2024-11-15 19:08:33 +01:00
|
|
|
FBlocks := TBlocks.Create;
|
2024-10-15 11:45:44 +02:00
|
|
|
FValidation := TIntegerList.Create;
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache := ACombinationsCache;
|
2023-12-12 15:47:58 +01:00
|
|
|
end;
|
|
|
|
|
2024-10-15 11:45:44 +02:00
|
|
|
destructor TConditionRecord.Destroy;
|
2023-12-12 15:47:58 +01:00
|
|
|
begin
|
2024-11-09 00:42:11 +01:00
|
|
|
FBlocks.Free;
|
2023-12-12 15:47:58 +01:00
|
|
|
FValidation.Free;
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2024-11-09 00:42:11 +01:00
|
|
|
procedure TConditionRecord.AddBlocks(const APattern: string);
|
2023-12-12 15:47:58 +01:00
|
|
|
var
|
|
|
|
split: TStringArray;
|
2024-10-15 11:45:44 +02:00
|
|
|
part: string;
|
2024-11-19 22:58:15 +01:00
|
|
|
blockCache: TBlockCombinationsCache;
|
2023-12-12 15:47:58 +01:00
|
|
|
begin
|
2024-10-15 11:45:44 +02:00
|
|
|
split := APattern.Split([COperationalChar]);
|
|
|
|
for part in split do
|
|
|
|
if Length(part) > 0 then
|
2024-11-19 22:58:15 +01:00
|
|
|
begin
|
|
|
|
if not FCombinationsCache.TryGetValue(part, blockCache) then
|
|
|
|
begin
|
|
|
|
blockCache := TBlockCombinationsCache.Create;
|
|
|
|
FCombinationsCache.Add(part, blockCache);
|
|
|
|
end;
|
|
|
|
FBlocks.Add(TBlock.Create(part, blockCache));
|
|
|
|
end;
|
2024-10-15 11:45:44 +02:00
|
|
|
end;
|
2023-12-12 15:47:58 +01:00
|
|
|
|
2024-10-15 11:45:44 +02:00
|
|
|
function TConditionRecord.GenerateBlockAssignments: Int64;
|
|
|
|
var
|
2024-11-19 18:14:14 +01:00
|
|
|
validationsToBlockAssignments: TValidationsToBlockAssignments;
|
|
|
|
indices: TIndexArray;
|
2024-10-15 11:45:44 +02:00
|
|
|
begin
|
|
|
|
InitValidationLengths;
|
|
|
|
InitMinIndices;
|
|
|
|
|
|
|
|
Result := 0;
|
2024-11-19 18:14:14 +01:00
|
|
|
validationsToBlockAssignments := TValidationsToBlockAssignments.Create(Self);
|
|
|
|
for indices in validationsToBlockAssignments do
|
|
|
|
Result := Result + validationsToBlockAssignments.GetCombinations;
|
|
|
|
validationsToBlockAssignments.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TConditionRecord.CalcCombinationsBlock(constref ABlock: TBlock; const AStartIndex, AStopIndex: Integer): Int64;
|
|
|
|
var
|
2024-11-19 22:58:15 +01:00
|
|
|
validationsId: Int64;
|
2024-11-19 18:14:14 +01:00
|
|
|
indices: TIndexArray;
|
|
|
|
damageToValidationAssignments: TDamageToValidationAssignments;
|
|
|
|
begin
|
|
|
|
// No validation number assigned to this block.
|
|
|
|
if AStartIndex > AStopIndex then
|
|
|
|
begin
|
|
|
|
if ABlock.Damages.Count = 0 then
|
|
|
|
Result := 1
|
|
|
|
else
|
|
|
|
Result := 0;
|
|
|
|
end
|
|
|
|
// One validation number assigned to this block.
|
|
|
|
else if AStartIndex = AStopIndex then
|
|
|
|
Result := CalcCombinationsBlockSingleValidation(ABlock, AStartIndex)
|
2024-11-19 22:58:15 +01:00
|
|
|
// Multiple validation numbers assigned to this block. Checks cache first.
|
2024-11-19 18:14:14 +01:00
|
|
|
else begin
|
2024-11-19 22:58:15 +01:00
|
|
|
validationsId := CalcValidationsId(AStartIndex, AStopIndex);
|
|
|
|
if not ABlock.CombinationsCache.TryGetValue(validationsId, Result) then
|
2024-10-15 11:45:44 +02:00
|
|
|
begin
|
2024-11-19 22:58:15 +01:00
|
|
|
Result := 0;
|
2024-11-19 18:14:14 +01:00
|
|
|
|
2024-11-19 22:58:15 +01:00
|
|
|
// Assigns validation numbers to specific damages.
|
|
|
|
damageToValidationAssignments := TDamageToValidationAssignments.Create(Self, ABlock, AStartIndex, AStopIndex);
|
|
|
|
for indices in damageToValidationAssignments do
|
|
|
|
Result := Result + CalcCombinationsBlockMultiValidations(ABlock, indices, AStartIndex, AStopIndex);
|
|
|
|
damageToValidationAssignments.Free;
|
2023-12-12 15:47:58 +01:00
|
|
|
|
2024-11-19 22:58:15 +01:00
|
|
|
ABlock.CombinationsCache.Add(validationsId, Result);
|
2024-11-19 18:14:14 +01:00
|
|
|
end;
|
|
|
|
end;
|
2024-10-15 11:45:44 +02:00
|
|
|
end;
|
|
|
|
|
2024-11-17 23:54:08 +01:00
|
|
|
function TConditionRecord.CalcCombinationsWildcardSequence(const ASequenceLength, AStartIndex, AStopIndex: Integer):
|
|
|
|
Int64;
|
|
|
|
var
|
|
|
|
count, freedoms: Integer;
|
|
|
|
begin
|
|
|
|
if AStartIndex < AStopIndex + 1 then
|
|
|
|
begin
|
|
|
|
count := AStopIndex + 1 - AStartIndex;
|
|
|
|
freedoms := ASequenceLength - FValidationLengths[AStartIndex, AStopIndex + 1];
|
|
|
|
if freedoms >= 0 then
|
|
|
|
Result := BinomialCoefficients.Get(count + freedoms, freedoms)
|
|
|
|
else
|
|
|
|
Result := 0;
|
|
|
|
end
|
2024-11-19 22:58:15 +01:00
|
|
|
else
|
2024-11-17 23:54:08 +01:00
|
|
|
Result := 1;
|
|
|
|
end;
|
|
|
|
|
2024-10-15 11:45:44 +02:00
|
|
|
{ THotSprings }
|
2023-12-13 12:32:12 +01:00
|
|
|
|
2024-11-09 23:17:47 +01:00
|
|
|
constructor THotSprings.Create;
|
|
|
|
begin
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache := TCombinationsCache.Create([doOwnsValues]);
|
2024-11-09 23:17:47 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor THotSprings.Destroy;
|
|
|
|
begin
|
2024-11-19 22:58:15 +01:00
|
|
|
FCombinationsCache.Free;
|
2024-11-09 23:17:47 +01:00
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
2024-10-15 11:45:44 +02:00
|
|
|
procedure THotSprings.ProcessDataLine(const ALine: string);
|
|
|
|
var
|
|
|
|
conditionRecord1, conditionRecord2: TConditionRecord;
|
|
|
|
mainSplit, split: TStringArray;
|
|
|
|
part, unfolded: string;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
2024-11-19 22:58:15 +01:00
|
|
|
conditionRecord1 := TConditionRecord.Create(FCombinationsCache);
|
|
|
|
conditionRecord2 := TConditionRecord.Create(FCombinationsCache);
|
2024-10-15 11:45:44 +02:00
|
|
|
|
|
|
|
mainSplit := ALine.Split([' ']);
|
|
|
|
|
|
|
|
// Adds blocks for part 1.
|
2024-11-09 00:42:11 +01:00
|
|
|
conditionRecord1.AddBlocks(mainSplit[0]);
|
2024-10-15 11:45:44 +02:00
|
|
|
|
|
|
|
// Adds blocks for part 2.
|
|
|
|
unfolded := mainSplit[0];
|
|
|
|
for i := 2 to CPart2Repetition do
|
|
|
|
unfolded := unfolded + CWildcardChar + mainSplit[0];
|
2024-11-09 00:42:11 +01:00
|
|
|
conditionRecord2.AddBlocks(unfolded);
|
2024-10-15 11:45:44 +02:00
|
|
|
|
|
|
|
// Adds validation numbers.
|
|
|
|
split := mainSplit[1].Split([',']);
|
|
|
|
for part in split do
|
|
|
|
conditionRecord1.Validation.Add(StrToInt(part));
|
|
|
|
for i := 1 to CPart2Repetition do
|
|
|
|
conditionRecord2.Validation.AddRange(conditionRecord1.Validation);
|
|
|
|
|
2024-11-12 19:16:12 +01:00
|
|
|
FPart1 := FPart1 + conditionRecord1.GenerateBlockAssignments;
|
2024-10-15 11:45:44 +02:00
|
|
|
FPart2 := FPart2 + conditionRecord2.GenerateBlockAssignments;
|
|
|
|
|
|
|
|
conditionRecord1.Free;
|
|
|
|
conditionRecord2.Free;
|
2023-12-12 15:47:58 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure THotSprings.Finish;
|
|
|
|
begin
|
2024-11-12 19:16:12 +01:00
|
|
|
|
2023-12-12 15:47:58 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function THotSprings.GetDataFileName: string;
|
|
|
|
begin
|
|
|
|
Result := 'hot_springs.txt';
|
|
|
|
end;
|
|
|
|
|
|
|
|
function THotSprings.GetPuzzleName: string;
|
|
|
|
begin
|
|
|
|
Result := 'Day 12: Hot Springs';
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|