AdventOfCode2023/solvers/UCubeConundrum.pas

143 lines
3.2 KiB
Plaintext

{
Solutions to the Advent Of Code.
Copyright (C) 2023 Stefan Müller
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
}
unit UCubeConundrum;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, USolver;
const
CMaxRed = 12;
CMaxGreen = 13;
CMaxBlue = 14;
type
{ TCubeConundrum }
TCubeConundrum = class(TSolver)
private
FGameMaxRed, FGameMaxGreen, FGameMaxBlue: Integer;
procedure ProcessGame(const id: Integer; const draws: TStringArray);
function ProcessDraw(const cubes: TStringArray): Boolean;
public
procedure ProcessDataLine(const ALine: string); override;
procedure Finish; override;
function GetDataFileName: string; override;
function GetPuzzleName: string; override;
end;
implementation
{ TCubeConundrum }
procedure TCubeConundrum.ProcessGame(const id: Integer; const draws: TStringArray);
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
isWithinMaxima := False;
end;
if isWithinMaxima then
begin
// All draws fulfill maxima.
Inc(FPart1, id);
end;
Inc(FPart2, FGameMaxRed * FGameMaxGreen * FGameMaxBlue);
end;
function TCubeConundrum.ProcessDraw(const cubes: TStringArray): Boolean;
var
cubeColor: string;
colorSplit: TStringArray;
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
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;
end;
end;
procedure TCubeConundrum.ProcessDataLine(const ALine: string);
var
gameSplit, subSplit: TStringArray;
id: Integer;
begin
gameSplit := ALine.Split(':');
subSplit := gameSplit[0].Split(' ');
id := StrToInt(subSplit[1]);
subSplit := gameSplit[1].Split(';');
ProcessGame(id, subSplit);
end;
procedure TCubeConundrum.Finish;
begin
end;
function TCubeConundrum.GetDataFileName: string;
begin
Result := 'cube_conundrum.txt';
end;
function TCubeConundrum.GetPuzzleName: string;
begin
Result := 'Day 2: Cube Conundrum';
end;
end.