Added ISolver, TSolver, TSolverEngine for the next puzzle solver
This commit is contained in:
parent
2e4ab2ad5f
commit
83f6cdd3ad
|
@ -37,6 +37,10 @@
|
||||||
<Filename Value="solvers\UTrebuchet.pas"/>
|
<Filename Value="solvers\UTrebuchet.pas"/>
|
||||||
<IsPartOfProject Value="True"/>
|
<IsPartOfProject Value="True"/>
|
||||||
</Unit>
|
</Unit>
|
||||||
|
<Unit>
|
||||||
|
<Filename Value="USolver.pas"/>
|
||||||
|
<IsPartOfProject Value="True"/>
|
||||||
|
</Unit>
|
||||||
</Units>
|
</Units>
|
||||||
</ProjectOptions>
|
</ProjectOptions>
|
||||||
<CompilerOptions>
|
<CompilerOptions>
|
||||||
|
|
|
@ -24,6 +24,7 @@ uses
|
||||||
cthreads,
|
cthreads,
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
Classes, SysUtils, CustApp,
|
Classes, SysUtils, CustApp,
|
||||||
|
USolver,
|
||||||
UTrebuchet, UCubeConundrum;
|
UTrebuchet, UCubeConundrum;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
@ -44,10 +45,14 @@ type
|
||||||
{ TAdventOfCode }
|
{ TAdventOfCode }
|
||||||
|
|
||||||
procedure TAdventOfCode.RunPuzzleSolutions;
|
procedure TAdventOfCode.RunPuzzleSolutions;
|
||||||
|
var
|
||||||
|
engine: TSolverEngine;
|
||||||
begin
|
begin
|
||||||
WriteLn('### Advent of Code 2023 ###');
|
WriteLn('### Advent of Code 2023 ###');
|
||||||
TTrebuchet.Run;
|
TTrebuchet.Run;
|
||||||
TCubeConundrum.Run;
|
TCubeConundrum.Run;
|
||||||
|
engine := TSolverEngine.Create;
|
||||||
|
engine.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TAdventOfCode.DoRun;
|
procedure TAdventOfCode.DoRun;
|
||||||
|
|
|
@ -0,0 +1,138 @@
|
||||||
|
{
|
||||||
|
Solutions to the Advent Of Code.
|
||||||
|
Copyright (C) 2023 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 USolver;
|
||||||
|
|
||||||
|
{$mode ObjFPC}{$H+}
|
||||||
|
{$interfaces corba}
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
Classes, SysUtils;
|
||||||
|
|
||||||
|
type
|
||||||
|
|
||||||
|
{ ISolver }
|
||||||
|
|
||||||
|
ISolver = interface
|
||||||
|
procedure Init;
|
||||||
|
procedure ProcessDataLine(const ALine: string);
|
||||||
|
procedure Finish;
|
||||||
|
procedure Free;
|
||||||
|
function GetDataFileName: string;
|
||||||
|
function GetPuzzleName: string;
|
||||||
|
function GetResultPart1: Integer;
|
||||||
|
function GetResultPart2: Integer;
|
||||||
|
property DataFileName: string read GetDataFileName;
|
||||||
|
property PuzzleName: string read GetPuzzleName;
|
||||||
|
property ResultPart1: Integer read GetResultPart1;
|
||||||
|
property ResultPart2: Integer read GetResultPart2;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TSolver }
|
||||||
|
|
||||||
|
TSolver = class abstract(ISolver)
|
||||||
|
protected
|
||||||
|
FPart1, FPart2: Integer;
|
||||||
|
public
|
||||||
|
procedure Init; virtual;
|
||||||
|
procedure ProcessDataLine(const ALine: string); virtual; abstract;
|
||||||
|
procedure Finish; virtual; abstract;
|
||||||
|
procedure Free; virtual;
|
||||||
|
function GetDataFileName: string; virtual; abstract;
|
||||||
|
function GetPuzzleName: string; virtual; abstract;
|
||||||
|
function GetResultPart1: Integer; virtual;
|
||||||
|
function GetResultPart2: Integer; virtual;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TSolverEngine }
|
||||||
|
|
||||||
|
TSolverEngine = class
|
||||||
|
private
|
||||||
|
procedure ProcessData(constref ASolver: ISolver);
|
||||||
|
public
|
||||||
|
procedure Run(constref ASolver: ISolver);
|
||||||
|
procedure RunAndFree(constref ASolver: ISolver);
|
||||||
|
end;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
{ TSolver }
|
||||||
|
|
||||||
|
procedure TSolver.Init;
|
||||||
|
begin
|
||||||
|
FPart1 := 0;
|
||||||
|
FPart2 := 0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSolver.Free;
|
||||||
|
begin
|
||||||
|
Free;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TSolver.GetResultPart1: Integer;
|
||||||
|
begin
|
||||||
|
Result := FPart1;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TSolver.GetResultPart2: Integer;
|
||||||
|
begin
|
||||||
|
Result := FPart2;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TSolverEngine }
|
||||||
|
|
||||||
|
procedure TSolverEngine.ProcessData(constref ASolver: ISolver);
|
||||||
|
var
|
||||||
|
data: TextFile;
|
||||||
|
s: string;
|
||||||
|
begin
|
||||||
|
ASolver.Init;
|
||||||
|
|
||||||
|
AssignFile(data, ConcatPaths(['data', ASolver.DataFileName]));
|
||||||
|
try
|
||||||
|
reset(data);
|
||||||
|
while (not EOF(data)) do
|
||||||
|
begin
|
||||||
|
readln(data, s);
|
||||||
|
ASolver.ProcessDataLine(s);
|
||||||
|
end;
|
||||||
|
finally
|
||||||
|
CloseFile(data)
|
||||||
|
end;
|
||||||
|
|
||||||
|
ASolver.Finish;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSolverEngine.Run(constref ASolver: ISolver);
|
||||||
|
begin
|
||||||
|
WriteLn;
|
||||||
|
WriteLn('--- ', ASolver.PuzzleName, ' ---');
|
||||||
|
ProcessData(ASolver);
|
||||||
|
WriteLn('Part 1: ', ASolver.ResultPart1);
|
||||||
|
WriteLn('Part 2: ', ASolver.ResultPart2);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TSolverEngine.RunAndFree(constref ASolver: ISolver);
|
||||||
|
begin
|
||||||
|
Run(ASolver);
|
||||||
|
ASolver.Free;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
|
|
Loading…
Reference in New Issue