From d8b298dad86c944e0eadbafee4d80916775728f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Fri, 15 Dec 2023 00:46:21 +0100 Subject: [PATCH] Updated day 14 solution with some small code improvements --- solvers/UParabolicReflectorDish.pas | 49 ++++++++++++++--------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/solvers/UParabolicReflectorDish.pas b/solvers/UParabolicReflectorDish.pas index 183db38..f62740e 100644 --- a/solvers/UParabolicReflectorDish.pas +++ b/solvers/UParabolicReflectorDish.pas @@ -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