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