Updated the solution for day 1, part 1 to facilitate part 2
This commit is contained in:
parent
b4352e8b87
commit
3b7bfa3103
|
@ -33,9 +33,12 @@ type
|
||||||
FValue: Integer;
|
FValue: Integer;
|
||||||
procedure RunSolution;
|
procedure RunSolution;
|
||||||
procedure ProcessDataLine(const ALine: string);
|
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;
|
||||||
public
|
public
|
||||||
class procedure Solve; static;
|
|
||||||
constructor Create;
|
constructor Create;
|
||||||
|
class procedure Solve; static;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
@ -58,28 +61,59 @@ begin
|
||||||
finally
|
finally
|
||||||
CloseFile(data)
|
CloseFile(data)
|
||||||
end;
|
end;
|
||||||
WriteLn(FValue);
|
WriteLn('Part 1: ', FValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
||||||
var
|
var
|
||||||
c: Char;
|
first, endIndex, last: Integer;
|
||||||
first, last: Integer;
|
|
||||||
begin
|
begin
|
||||||
first := -1;
|
first := FindFirstDigit(ALine, endIndex);
|
||||||
last := -1;
|
last := FindLastDigit(ALine, first, endIndex);
|
||||||
for c in ALine do
|
|
||||||
begin
|
|
||||||
if c in ['0'..'9'] then
|
|
||||||
begin
|
|
||||||
last := StrToInt(c);
|
|
||||||
if first < 0 then
|
|
||||||
first := last;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
Inc(FValue, first * 10 + last);
|
Inc(FValue, first * 10 + last);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TTrebuchet.FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
||||||
|
var
|
||||||
|
i: Integer;
|
||||||
|
begin
|
||||||
|
for i := 1 to ALine.Length do
|
||||||
|
if CheckDigit(ALine, i, Result, OEndIndex) then
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TTrebuchet.FindLastDigit(const ALine: string; const AFirstDigit, AFirstDigitEndIndex: Integer): Integer;
|
||||||
|
var
|
||||||
|
i, endIndex: Integer;
|
||||||
|
begin
|
||||||
|
for i := ALine.Length downto AFirstDigitEndIndex + 1 do
|
||||||
|
if CheckDigit(ALine, i, Result, endIndex) then
|
||||||
|
Exit;
|
||||||
|
|
||||||
|
// Returns the first digit if no digit was found after the end index of the first digit.
|
||||||
|
Result := AFirstDigit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TTrebuchet.CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||||
|
begin
|
||||||
|
if ALine[AIndex] in ['0'..'9'] then
|
||||||
|
begin
|
||||||
|
ODigit := StrToInt(ALine[AIndex]);
|
||||||
|
OEndIndex := AIndex;
|
||||||
|
Result := true;
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
ODigit := -1;
|
||||||
|
OEndIndex := -1;
|
||||||
|
Result := false;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
constructor TTrebuchet.Create;
|
||||||
|
begin
|
||||||
|
FValue := 0;
|
||||||
|
end;
|
||||||
|
|
||||||
class procedure TTrebuchet.Solve;
|
class procedure TTrebuchet.Solve;
|
||||||
var
|
var
|
||||||
trebuchet: TTrebuchet;
|
trebuchet: TTrebuchet;
|
||||||
|
@ -90,10 +124,5 @@ begin
|
||||||
trebuchet.Free;
|
trebuchet.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TTrebuchet.Create;
|
|
||||||
begin
|
|
||||||
FValue := 0;
|
|
||||||
end;
|
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue