Updated "Trebuchet?!" to use the solver-engine framework

This commit is contained in:
Stefan Müller 2023-12-05 16:58:44 +01:00 committed by Stefan Müller
parent 2e13199796
commit 9ab9dcfb51
2 changed files with 67 additions and 62 deletions

View File

@ -49,8 +49,8 @@ var
engine: TSolverEngine; engine: TSolverEngine;
begin begin
WriteLn('### Advent of Code 2023 ###'); WriteLn('### Advent of Code 2023 ###');
TTrebuchet.Run;
engine := TSolverEngine.Create('data'); engine := TSolverEngine.Create('data');
engine.RunAndFree(TTrebuchet.Create);
engine.RunAndFree(TCubeConundrum.Create); engine.RunAndFree(TCubeConundrum.Create);
engine.RunAndFree(TGearRatios.Create); engine.RunAndFree(TGearRatios.Create);
engine.RunAndFree(TScratchcards.Create); engine.RunAndFree(TScratchcards.Create);

View File

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