Updated "Trebuchet?!" to use the solver-engine framework
This commit is contained in:
parent
2e13199796
commit
9ab9dcfb51
|
@ -49,8 +49,8 @@ var
|
|||
engine: TSolverEngine;
|
||||
begin
|
||||
WriteLn('### Advent of Code 2023 ###');
|
||||
TTrebuchet.Run;
|
||||
engine := TSolverEngine.Create('data');
|
||||
engine.RunAndFree(TTrebuchet.Create);
|
||||
engine.RunAndFree(TCubeConundrum.Create);
|
||||
engine.RunAndFree(TGearRatios.Create);
|
||||
engine.RunAndFree(TScratchcards.Create);
|
||||
|
|
|
@ -22,77 +22,92 @@ unit UTrebuchet;
|
|||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, TypInfo;
|
||||
Classes, SysUtils, TypInfo, USolver;
|
||||
|
||||
type
|
||||
TSpelledOutDigit = (zero, one, two, three, four, five, six, seven, eight, nine);
|
||||
TFoundDigitType = (None, Digit, SpelledOutDigit);
|
||||
|
||||
{ TTrebuchet }
|
||||
|
||||
TTrebuchet = class(TObject)
|
||||
TTrebuchet = class(TSolver)
|
||||
private
|
||||
FValue: Integer;
|
||||
FConsiderSpelledOutDigits: Boolean;
|
||||
procedure ProcessDataLine(const ALine: string);
|
||||
function FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
||||
function FindLastDigit(const ALine: string; const AFirstDigit, AFirstDigitEndIndex: Integer): Integer;
|
||||
function CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||
procedure FindFirstDigit(const ALine: string; out OFirst1, OFirst2, OEndIndex: Integer);
|
||||
procedure FindLastDigit(const ALine: string; const AFallbackDigit, AEndIndex: Integer; out OLast1, OLast2: Integer);
|
||||
function CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): TFoundDigitType;
|
||||
function CheckSpelledOutDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||
public
|
||||
function Solve(const AConsiderSpelledOutDigits: Boolean): Integer;
|
||||
class procedure Run; static;
|
||||
procedure ProcessDataLine(const ALine: string); override;
|
||||
procedure Finish; override;
|
||||
function GetDataFileName: string; override;
|
||||
function GetPuzzleName: string; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TTrebuchet }
|
||||
|
||||
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
first, endIndex, last: Integer;
|
||||
begin
|
||||
first := FindFirstDigit(ALine, endIndex);
|
||||
last := FindLastDigit(ALine, first, endIndex);
|
||||
Inc(FValue, first * 10 + last);
|
||||
end;
|
||||
|
||||
function TTrebuchet.FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
||||
procedure TTrebuchet.FindFirstDigit(const ALine: string; out OFirst1, OFirst2, OEndIndex: Integer);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
OFirst2 := -1;
|
||||
for i := 1 to ALine.Length do
|
||||
if CheckDigit(ALine, i, Result, OEndIndex) then
|
||||
Exit;
|
||||
case CheckDigit(ALine, i, OFirst1, OEndIndex) of
|
||||
SpelledOutDigit:
|
||||
if OFirst2 <= -1 then begin
|
||||
OFirst2 := OFirst1;
|
||||
end;
|
||||
Digit: begin
|
||||
if OFirst2 <= -1 then begin
|
||||
OFirst2 := OFirst1;
|
||||
end;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TTrebuchet.FindLastDigit(const ALine: string; const AFirstDigit, AFirstDigitEndIndex: Integer): Integer;
|
||||
procedure TTrebuchet.FindLastDigit(const ALine: string; const AFallbackDigit, AEndIndex: Integer; out OLast1, OLast2:
|
||||
Integer);
|
||||
var
|
||||
i, endIndex: Integer;
|
||||
begin
|
||||
for i := ALine.Length downto AFirstDigitEndIndex + 1 do
|
||||
if CheckDigit(ALine, i, Result, endIndex) then
|
||||
Exit;
|
||||
OLast1 := -1;
|
||||
OLast2 := -1;
|
||||
for i := ALine.Length downto AEndIndex + 1 do
|
||||
case CheckDigit(ALine, i, OLast1, endIndex) of
|
||||
SpelledOutDigit:
|
||||
if OLast2 <= -1 then
|
||||
OLast2 := OLast1;
|
||||
Digit: begin
|
||||
if OLast2 <= -1 then
|
||||
OLast2 := OLast1;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
// Returns the first digit if no digit was found after the end index of the first digit.
|
||||
Result := AFirstDigit;
|
||||
if OLast2 <= -1 then
|
||||
OLast2 := AFallbackDigit;
|
||||
OLast1 := AFallbackDigit;
|
||||
end;
|
||||
|
||||
function TTrebuchet.CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||
function TTrebuchet.CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer):
|
||||
TFoundDigitType;
|
||||
begin
|
||||
if ALine[AIndex] in ['0'..'9'] then
|
||||
begin
|
||||
ODigit := StrToInt(ALine[AIndex]);
|
||||
OEndIndex := AIndex;
|
||||
Result := True;
|
||||
Result := Digit;
|
||||
end
|
||||
else if FConsiderSpelledOutDigits and CheckSpelledOutDigit(ALine, AIndex, ODigit, OEndIndex) then
|
||||
else if CheckSpelledOutDigit(ALine, AIndex, ODigit, OEndIndex) then
|
||||
begin
|
||||
Result := true;
|
||||
Result := SpelledOutDigit;
|
||||
end
|
||||
else begin
|
||||
ODigit := -1;
|
||||
OEndIndex := -1;
|
||||
Result := False;
|
||||
Result := None;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
@ -119,39 +134,29 @@ begin
|
|||
Result := False;
|
||||
end;
|
||||
|
||||
function TTrebuchet.Solve(const AConsiderSpelledOutDigits: Boolean): Integer;
|
||||
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
data: TextFile;
|
||||
s: string;
|
||||
first1, first2, endIndex, last1, last2: Integer;
|
||||
begin
|
||||
FValue := 0;
|
||||
FConsiderSpelledOutDigits := AConsiderSpelledOutDigits;
|
||||
|
||||
AssignFile(data, ConcatPaths(['data', 'trebuchet_calibration_document.txt']));
|
||||
try
|
||||
reset(data);
|
||||
while (not EOF(data)) do
|
||||
begin
|
||||
readln(data, s);
|
||||
ProcessDataLine(s);
|
||||
end;
|
||||
finally
|
||||
CloseFile(data)
|
||||
end;
|
||||
|
||||
Result := FValue;
|
||||
FindFirstDigit(ALine, first1, first2, endIndex);
|
||||
FindLastDigit(ALine, first1, endIndex, last1, last2);
|
||||
Inc(FPart1, first1 * 10 + last1);
|
||||
Inc(FPart2, first2 * 10 + last2);
|
||||
end;
|
||||
|
||||
class procedure TTrebuchet.Run;
|
||||
var
|
||||
trebuchet: TTrebuchet;
|
||||
procedure TTrebuchet.Finish;
|
||||
begin
|
||||
WriteLn;
|
||||
WriteLn('--- Day 1: Trebuchet?! ---');
|
||||
trebuchet := TTrebuchet.Create;
|
||||
WriteLn('Part 1: ', trebuchet.Solve(False));
|
||||
WriteLn('Part 2: ', trebuchet.Solve(True));
|
||||
trebuchet.Free;
|
||||
|
||||
end;
|
||||
|
||||
function TTrebuchet.GetDataFileName: string;
|
||||
begin
|
||||
Result := 'trebuchet_calibration_document.txt';
|
||||
end;
|
||||
|
||||
function TTrebuchet.GetPuzzleName: string;
|
||||
begin
|
||||
Result := 'Day 1: Trebuchet?!';
|
||||
end;
|
||||
|
||||
end.
|
||||
|
|
Loading…
Reference in New Issue