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