Added solution for "Day 2: Cube Conundrum", part 2

This commit is contained in:
Stefan Müller 2023-12-03 01:47:23 +01:00 committed by Stefan Müller
parent 665f168273
commit af0fa04c38
1 changed files with 46 additions and 22 deletions

View File

@ -35,12 +35,14 @@ type
TCubeConundrum = class(TObject) TCubeConundrum = class(TObject)
private private
FValue: Integer; FValue1, FValue2, FGameMaxRed, FGameMaxGreen, FGameMaxBlue: Integer;
procedure ProcessDataLine(const ALine: string); procedure ProcessDataLine(const ALine: string);
procedure ProcessGame(const id: Integer; const draws: TStringArray); procedure ProcessGame(const id: Integer; const draws: TStringArray);
function ProcessDraw(const cubes: TStringArray): Boolean; function ProcessDraw(const cubes: TStringArray): Boolean;
public public
function Solve: Integer; procedure Solve;
property Part1: Integer read FValue1;
property Part2: Integer read FValue2;
class procedure Run; static; class procedure Run; static;
end; end;
@ -64,50 +66,72 @@ procedure TCubeConundrum.ProcessGame(const id: Integer; const draws: TStringArra
var var
draw: string; draw: string;
drawSplit: TStringArray; drawSplit: TStringArray;
isWithinMaxima: Boolean;
begin begin
FGameMaxRed := 0;
FGameMaxGreen := 0;
FGameMaxBlue := 0;
isWithinMaxima := True;
for draw in draws do for draw in draws do
begin begin
drawSplit := draw.Split(','); drawSplit := draw.Split(',');
if not ProcessDraw(drawSplit) then if not ProcessDraw(drawSplit) then
Exit; isWithinMaxima := False;
end; end;
if isWithinMaxima then
begin
// All draws fulfill maxima. // All draws fulfill maxima.
Inc(FValue, id); Inc(FValue1, id);
end;
Inc(FValue2, FGameMaxRed * FGameMaxGreen * FGameMaxBlue);
end; end;
function TCubeConundrum.ProcessDraw(const cubes: TStringArray): Boolean; function TCubeConundrum.ProcessDraw(const cubes: TStringArray): Boolean;
var var
cubeColor: string; cubeColor: string;
colorSplit: TStringArray; colorSplit: TStringArray;
max: Integer; cubeCount, max: Integer;
begin begin
Result := True;
for cubeColor in cubes do for cubeColor in cubes do
begin begin
colorSplit := cubeColor.Trim.Split(' '); colorSplit := cubeColor.Trim.Split(' ');
cubeCount := StrToInt(colorSplit[0]);
if colorSplit[1] = 'red' then if colorSplit[1] = 'red' then
max := CMaxRed
else if colorSplit[1] = 'green' then
max := CMaxGreen
else if colorSplit[1] = 'blue' then
max := CMaxBlue;
if StrToInt(colorSplit[0]) > max then
begin begin
if FGameMaxRed < cubeCount then
FGameMaxRed := cubeCount;
max := CMaxRed
end
else if colorSplit[1] = 'green' then
begin
if FGameMaxGreen < cubeCount then
FGameMaxGreen := cubeCount;
max := CMaxGreen;
end
else if colorSplit[1] = 'blue' then
begin
if FGameMaxBlue < cubeCount then
FGameMaxBlue := cubeCount;
max := CMaxBlue;
end;
if max < cubeCount then
Result := False; Result := False;
Exit;
end; end;
end; end;
Result := True; procedure TCubeConundrum.Solve;
end;
function TCubeConundrum.Solve: Integer;
var var
data: TextFile; data: TextFile;
s: string; s: string;
begin begin
FValue := 0; FValue1 := 0;
FValue2 := 0;
AssignFile(data, ConcatPaths(['data', 'cube_conundrum.txt'])); AssignFile(data, ConcatPaths(['data', 'cube_conundrum.txt']));
try try
@ -120,8 +144,6 @@ begin
finally finally
CloseFile(data) CloseFile(data)
end; end;
Result := FValue;
end; end;
class procedure TCubeConundrum.Run; class procedure TCubeConundrum.Run;
@ -131,7 +153,9 @@ begin
WriteLn; WriteLn;
WriteLn('--- Day 2: Cube Conundrum ---'); WriteLn('--- Day 2: Cube Conundrum ---');
cubeConundrum := TCubeConundrum.Create; cubeConundrum := TCubeConundrum.Create;
WriteLn('Part 1: ', cubeConundrum.Solve); cubeConundrum.Solve;
WriteLn('Part 1: ', cubeConundrum.Part1);
WriteLn('Part 2: ', cubeConundrum.Part2);
cubeConundrum.Free; cubeConundrum.Free;
end; end;