Updated day 14 solution with some small code improvements

This commit is contained in:
Stefan Müller 2023-12-15 00:46:21 +01:00 committed by Stefan Müller
parent 22b9a24893
commit d8b298dad8
1 changed files with 24 additions and 25 deletions

View File

@ -27,6 +27,7 @@ uses
const
CRoundRockChar = 'O';
CCubeRockChar = '#';
CEmptyChar = '.';
CMaxSpinCount = 1000000000;
type
@ -56,6 +57,7 @@ type
procedure TiltWest;
procedure TiltEast;
function IsEqualTo(const FOther: TStringList): Boolean;
procedure SwapRockLocation(const AColumn, ARockLineIndex, AEmptyLineIndex: Integer);
public
constructor Create;
destructor Destroy; override;
@ -115,50 +117,34 @@ end;
procedure TPlatform.TiltNorth;
var
i, j, k: Integer;
s: string;
begin
for i := 0 to FLines.Count - 1 do
for j := 1 to Length(FLines[i]) do
if FLines[i][j] = CRoundRockChar then
begin
k := i - 1;
while (k >= 0) and (FLines[k][j] = '.') do
while (k >= 0) and (FLines[k][j] = CEmptyChar) do
Dec(k);
Inc(k);
if k < i then
begin
s := FLines[i];
s[j] := '.';
FLines[i] := s;
s := FLines[k];
s[j] := CRoundRockChar;
FLines[k] := s;
end;
SwapRockLocation(j, i, k);
end;
end;
procedure TPlatform.TiltSouth;
var
i, j, k: Integer;
s: string;
begin
for i := FLines.Count - 1 downto 0 do
for j := 1 to Length(FLines[i]) do
if FLines[i][j] = CRoundRockChar then
begin
k := i + 1;
while (k < FLines.Count) and (FLines[k][j] = '.') do
while (k < FLines.Count) and (FLines[k][j] = CEmptyChar) do
Inc(k);
Dec(k);
if k > i then
begin
s := FLines[i];
s[j] := '.';
FLines[i] := s;
s := FLines[k];
s[j] := CRoundRockChar;
FLines[k] := s;
end;
SwapRockLocation(j, i, k);
end;
end;
@ -174,14 +160,14 @@ begin
for j := 1 to Length(s) do
begin
case s[j] of
'.':
CEmptyChar:
if k <= 0 then
k := j;
CRoundRockChar: begin
if (k > 0) and (k < j) then
begin
s[k] := CRoundRockChar;
s[j] := '.';
s[j] := CEmptyChar;
Inc(k);
end
else
@ -206,14 +192,14 @@ begin
for j := Length(s) downto 1 do
begin
case s[j] of
'.':
CEmptyChar:
if k > Length(s) then
k := j;
CRoundRockChar: begin
if (k <= Length(s)) and (j < k) then
begin
s[k] := CRoundRockChar;
s[j] := '.';
s[j] := CEmptyChar;
Dec(k);
end
else
@ -241,7 +227,20 @@ begin
end;
end
else
Result := False;end;
Result := False;
end;
procedure TPlatform.SwapRockLocation(const AColumn, ARockLineIndex, AEmptyLineIndex: Integer);
var
s: string;
begin
s := FLines[ARockLineIndex];
s[AColumn] := CEmptyChar;
FLines[ARockLineIndex] := s;
s := FLines[AEmptyLineIndex];
s[AColumn] := CRoundRockChar;
FLines[AEmptyLineIndex] := s;
end;
constructor TPlatform.Create;
begin