Added solution for "Day 1: Trebuchet?!", part 2
This commit is contained in:
parent
3b7bfa3103
commit
37f4198325
|
@ -22,48 +22,31 @@ unit UTrebuchet;
|
||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils;
|
Classes, SysUtils, TypInfo;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
TSpelledOutDigit = (zero, one, two, three, four, five, six, seven, eight, nine);
|
||||||
|
|
||||||
{ TTrebuchet }
|
{ TTrebuchet }
|
||||||
|
|
||||||
TTrebuchet = class(TObject)
|
TTrebuchet = class(TObject)
|
||||||
private
|
private
|
||||||
FValue: Integer;
|
FValue: Integer;
|
||||||
procedure RunSolution;
|
FConsiderSpelledOutDigits: Boolean;
|
||||||
procedure ProcessDataLine(const ALine: string);
|
procedure ProcessDataLine(const ALine: string);
|
||||||
function FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
function FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
||||||
function FindLastDigit(const ALine: string; const AFirstDigit, AFirstDigitEndIndex: 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 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;
|
||||||
public
|
public
|
||||||
constructor Create;
|
function Solve(const AConsiderSpelledOutDigits: Boolean): Integer;
|
||||||
class procedure Solve; static;
|
class procedure Run; static;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
{ TTrebuchet }
|
{ TTrebuchet }
|
||||||
|
|
||||||
procedure TTrebuchet.RunSolution;
|
|
||||||
var
|
|
||||||
data: TextFile;
|
|
||||||
s: string;
|
|
||||||
begin
|
|
||||||
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;
|
|
||||||
WriteLn('Part 1: ', FValue);
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
||||||
var
|
var
|
||||||
first, endIndex, last: Integer;
|
first, endIndex, last: Integer;
|
||||||
|
@ -100,27 +83,74 @@ begin
|
||||||
begin
|
begin
|
||||||
ODigit := StrToInt(ALine[AIndex]);
|
ODigit := StrToInt(ALine[AIndex]);
|
||||||
OEndIndex := AIndex;
|
OEndIndex := AIndex;
|
||||||
|
Result := True;
|
||||||
|
end
|
||||||
|
else if FConsiderSpelledOutDigits and CheckSpelledOutDigit(ALine, AIndex, ODigit, OEndIndex) then
|
||||||
|
begin
|
||||||
Result := true;
|
Result := true;
|
||||||
end
|
end
|
||||||
else begin
|
else begin
|
||||||
ODigit := -1;
|
ODigit := -1;
|
||||||
OEndIndex := -1;
|
OEndIndex := -1;
|
||||||
Result := false;
|
Result := False;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TTrebuchet.Create;
|
function TTrebuchet.CheckSpelledOutDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer):
|
||||||
|
Boolean;
|
||||||
|
var
|
||||||
|
i: Integer;
|
||||||
|
digitName: string;
|
||||||
|
begin
|
||||||
|
for i := Ord(Low(TSpelledOutDigit)) to Ord(High(TSpelledOutDigit)) do
|
||||||
|
begin
|
||||||
|
digitName := GetEnumName(TypeInfo(TSpelledOutDigit), Ord(i));
|
||||||
|
if CompareStr(digitName, Copy(ALine, AIndex, digitName.Length)) = 0 then
|
||||||
|
begin
|
||||||
|
ODigit := i;
|
||||||
|
OEndIndex := AIndex + digitName.Length - 1;
|
||||||
|
Result := True;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
ODigit := -1;
|
||||||
|
OEndIndex := -1;
|
||||||
|
Result := False;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TTrebuchet.Solve(const AConsiderSpelledOutDigits: Boolean): Integer;
|
||||||
|
var
|
||||||
|
data: TextFile;
|
||||||
|
s: string;
|
||||||
begin
|
begin
|
||||||
FValue := 0;
|
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;
|
end;
|
||||||
|
|
||||||
class procedure TTrebuchet.Solve;
|
Result := FValue;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class procedure TTrebuchet.Run;
|
||||||
var
|
var
|
||||||
trebuchet: TTrebuchet;
|
trebuchet: TTrebuchet;
|
||||||
begin
|
begin
|
||||||
|
WriteLn;
|
||||||
WriteLn('--- Day 1: Trebuchet?! ---');
|
WriteLn('--- Day 1: Trebuchet?! ---');
|
||||||
trebuchet := TTrebuchet.Create;
|
trebuchet := TTrebuchet.Create;
|
||||||
trebuchet.RunSolution;
|
WriteLn('Part 1: ', trebuchet.Solve(False));
|
||||||
|
WriteLn('Part 2: ', trebuchet.Solve(True));
|
||||||
trebuchet.Free;
|
trebuchet.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue