2023-12-03 17:55:00 +01:00
|
|
|
{
|
|
|
|
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;
|
2023-12-09 14:44:47 +01:00
|
|
|
function GetResultPart1: Int64;
|
|
|
|
function GetResultPart2: Int64;
|
2023-12-03 17:55:00 +01:00
|
|
|
property DataFileName: string read GetDataFileName;
|
|
|
|
property PuzzleName: string read GetPuzzleName;
|
2023-12-09 14:44:47 +01:00
|
|
|
property ResultPart1: Int64 read GetResultPart1;
|
|
|
|
property ResultPart2: Int64 read GetResultPart2;
|
2023-12-03 17:55:00 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TSolver }
|
|
|
|
|
|
|
|
TSolver = class abstract(ISolver)
|
|
|
|
protected
|
2023-12-09 14:44:47 +01:00
|
|
|
FPart1, FPart2: Int64;
|
2023-12-03 17:55:00 +01:00
|
|
|
public
|
|
|
|
procedure Init; virtual;
|
|
|
|
procedure ProcessDataLine(const ALine: string); virtual; abstract;
|
|
|
|
procedure Finish; virtual; abstract;
|
|
|
|
function GetDataFileName: string; virtual; abstract;
|
|
|
|
function GetPuzzleName: string; virtual; abstract;
|
2023-12-09 14:44:47 +01:00
|
|
|
function GetResultPart1: Int64; virtual;
|
|
|
|
function GetResultPart2: Int64; virtual;
|
2023-12-03 17:55:00 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TSolverEngine }
|
|
|
|
|
|
|
|
TSolverEngine = class
|
2023-12-04 16:29:32 +01:00
|
|
|
private
|
2024-02-22 22:26:31 +01:00
|
|
|
FRelativeDataPaths: TStringArray;
|
2023-12-03 17:55:00 +01:00
|
|
|
public
|
2024-02-22 22:26:31 +01:00
|
|
|
constructor Create(constref ARelativeDataPaths: TStringArray);
|
2023-12-04 16:28:06 +01:00
|
|
|
procedure ProcessData(const ASolver: ISolver);
|
|
|
|
procedure Run(const ASolver: ISolver);
|
|
|
|
procedure RunAndFree(const ASolver: ISolver);
|
2024-02-22 22:26:31 +01:00
|
|
|
function HasValidDataPath(const ASolver: ISolver): Boolean;
|
|
|
|
function TryGetFirstValidDataPath(const ASolver: ISolver; out ODataFilePath: string): Boolean;
|
|
|
|
function GetInvalidDataPathMessage(const ASolver: ISolver): string;
|
2023-12-03 17:55:00 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TSolver }
|
|
|
|
|
|
|
|
procedure TSolver.Init;
|
|
|
|
begin
|
|
|
|
FPart1 := 0;
|
|
|
|
FPart2 := 0;
|
|
|
|
end;
|
|
|
|
|
2023-12-09 14:44:47 +01:00
|
|
|
function TSolver.GetResultPart1: Int64;
|
2023-12-03 17:55:00 +01:00
|
|
|
begin
|
|
|
|
Result := FPart1;
|
|
|
|
end;
|
|
|
|
|
2023-12-09 14:44:47 +01:00
|
|
|
function TSolver.GetResultPart2: Int64;
|
2023-12-03 17:55:00 +01:00
|
|
|
begin
|
|
|
|
Result := FPart2;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TSolverEngine }
|
|
|
|
|
2024-02-22 22:26:31 +01:00
|
|
|
constructor TSolverEngine.Create(constref ARelativeDataPaths: TStringArray);
|
2023-12-04 16:29:32 +01:00
|
|
|
begin
|
2024-02-22 22:26:31 +01:00
|
|
|
if (ARelativeDataPaths = nil) or (Length(ARelativeDataPaths) = 0) then
|
|
|
|
raise EArgumentOutOfRangeException.Create('Must specify at least one data path.');
|
|
|
|
FRelativeDataPaths := ARelativeDataPaths;
|
2023-12-04 16:29:32 +01:00
|
|
|
end;
|
|
|
|
|
2023-12-04 16:28:06 +01:00
|
|
|
procedure TSolverEngine.ProcessData(const ASolver: ISolver);
|
2023-12-03 17:55:00 +01:00
|
|
|
var
|
|
|
|
data: TextFile;
|
2024-02-22 22:26:31 +01:00
|
|
|
dataFilePath, s: string;
|
2023-12-03 17:55:00 +01:00
|
|
|
begin
|
|
|
|
ASolver.Init;
|
|
|
|
|
2024-02-22 22:26:31 +01:00
|
|
|
TryGetFirstValidDataPath(ASolver, dataFilePath);
|
|
|
|
AssignFile(data, dataFilePath);
|
2023-12-03 17:55:00 +01:00
|
|
|
try
|
|
|
|
reset(data);
|
|
|
|
while (not EOF(data)) do
|
|
|
|
begin
|
|
|
|
readln(data, s);
|
|
|
|
ASolver.ProcessDataLine(s);
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
CloseFile(data)
|
|
|
|
end;
|
|
|
|
|
|
|
|
ASolver.Finish;
|
|
|
|
end;
|
|
|
|
|
2023-12-04 16:28:06 +01:00
|
|
|
procedure TSolverEngine.Run(const ASolver: ISolver);
|
2023-12-03 17:55:00 +01:00
|
|
|
begin
|
|
|
|
WriteLn;
|
|
|
|
WriteLn('--- ', ASolver.PuzzleName, ' ---');
|
2024-02-22 22:26:31 +01:00
|
|
|
if HasValidDataPath(ASolver) then
|
2024-02-21 20:59:21 +01:00
|
|
|
begin
|
|
|
|
ProcessData(ASolver);
|
|
|
|
WriteLn('Part 1: ', ASolver.ResultPart1);
|
|
|
|
WriteLn('Part 2: ', ASolver.ResultPart2);
|
|
|
|
end
|
2024-02-22 22:26:31 +01:00
|
|
|
else
|
|
|
|
WriteLn(GetInvalidDataPathMessage(ASolver));
|
2023-12-03 17:55:00 +01:00
|
|
|
end;
|
|
|
|
|
2023-12-04 16:28:06 +01:00
|
|
|
procedure TSolverEngine.RunAndFree(const ASolver: ISolver);
|
2023-12-03 17:55:00 +01:00
|
|
|
begin
|
|
|
|
Run(ASolver);
|
|
|
|
ASolver.Free;
|
|
|
|
end;
|
|
|
|
|
2024-02-22 22:26:31 +01:00
|
|
|
function TSolverEngine.HasValidDataPath(const ASolver: ISolver): Boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
Result := TryGetFirstValidDataPath(ASolver, s);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TSolverEngine.TryGetFirstValidDataPath(const ASolver: ISolver; out ODataFilePath: string): Boolean;
|
|
|
|
var
|
|
|
|
path: string;
|
|
|
|
begin
|
|
|
|
for path in FRelativeDataPaths do
|
|
|
|
begin
|
|
|
|
ODataFilePath := ConcatPaths([path, ASolver.DataFileName]);
|
|
|
|
if FileExists(ODataFilePath) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := False;
|
|
|
|
ODataFilePath := '';
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TSolverEngine.GetInvalidDataPathMessage(const ASolver: ISolver): string;
|
|
|
|
var
|
|
|
|
i: Integer;
|
2024-02-21 20:59:21 +01:00
|
|
|
begin
|
2024-02-22 22:26:31 +01:00
|
|
|
Result := 'Cannot find puzzle input file '''
|
|
|
|
+ ExpandFileName(ConcatPaths([FRelativeDataPaths[0], ASolver.DataFileName]));
|
|
|
|
for i := 1 to Length(FRelativeDataPaths) - 1 do
|
|
|
|
Result := Result + ''', or '''
|
|
|
|
+ ExpandFileName(ConcatPaths([FRelativeDataPaths[i], ASolver.DataFileName]));
|
|
|
|
Result := Result + '''. Please download the file content from https://adventofcode.com/2023/';
|
2024-02-21 20:59:21 +01:00
|
|
|
end;
|
|
|
|
|
2023-12-03 17:55:00 +01:00
|
|
|
end.
|
|
|
|
|