Renamed folder for puzzle solvers
This commit is contained in:
162
solvers/UCubeConundrum.pas
Normal file
162
solvers/UCubeConundrum.pas
Normal file
@@ -0,0 +1,162 @@
|
||||
{
|
||||
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;
|
||||
|
||||
const
|
||||
CMaxRed = 12;
|
||||
CMaxGreen = 13;
|
||||
CMaxBlue = 14;
|
||||
|
||||
type
|
||||
|
||||
{ TCubeConundrum }
|
||||
|
||||
TCubeConundrum = class(TObject)
|
||||
private
|
||||
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
|
||||
procedure Solve;
|
||||
property Part1: Integer read FValue1;
|
||||
property Part2: Integer read FValue2;
|
||||
class procedure Run; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TCubeConundrum }
|
||||
|
||||
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.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(FValue1, id);
|
||||
end;
|
||||
|
||||
Inc(FValue2, 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.Solve;
|
||||
var
|
||||
data: TextFile;
|
||||
s: string;
|
||||
begin
|
||||
FValue1 := 0;
|
||||
FValue2 := 0;
|
||||
|
||||
AssignFile(data, ConcatPaths(['data', 'cube_conundrum.txt']));
|
||||
try
|
||||
reset(data);
|
||||
while (not EOF(data)) do
|
||||
begin
|
||||
readln(data, s);
|
||||
ProcessDataLine(s);
|
||||
end;
|
||||
finally
|
||||
CloseFile(data)
|
||||
end;
|
||||
end;
|
||||
|
||||
class procedure TCubeConundrum.Run;
|
||||
var
|
||||
cubeConundrum: TCubeConundrum;
|
||||
begin
|
||||
WriteLn;
|
||||
WriteLn('--- Day 2: Cube Conundrum ---');
|
||||
cubeConundrum := TCubeConundrum.Create;
|
||||
cubeConundrum.Solve;
|
||||
WriteLn('Part 1: ', cubeConundrum.Part1);
|
||||
WriteLn('Part 2: ', cubeConundrum.Part2);
|
||||
cubeConundrum.Free;
|
||||
end;
|
||||
|
||||
end.
|
||||
158
solvers/UTrebuchet.pas
Normal file
158
solvers/UTrebuchet.pas
Normal file
@@ -0,0 +1,158 @@
|
||||
{
|
||||
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 UTrebuchet;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, TypInfo;
|
||||
|
||||
type
|
||||
TSpelledOutDigit = (zero, one, two, three, four, five, six, seven, eight, nine);
|
||||
|
||||
{ TTrebuchet }
|
||||
|
||||
TTrebuchet = class(TObject)
|
||||
private
|
||||
FValue: Integer;
|
||||
FConsiderSpelledOutDigits: Boolean;
|
||||
procedure ProcessDataLine(const ALine: string);
|
||||
function FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
||||
function FindLastDigit(const ALine: string; const AFirstDigit, AFirstDigitEndIndex: Integer): Integer;
|
||||
function CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||
function CheckSpelledOutDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||
public
|
||||
function Solve(const AConsiderSpelledOutDigits: Boolean): Integer;
|
||||
class procedure Run; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TTrebuchet }
|
||||
|
||||
procedure TTrebuchet.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
first, endIndex, last: Integer;
|
||||
begin
|
||||
first := FindFirstDigit(ALine, endIndex);
|
||||
last := FindLastDigit(ALine, first, endIndex);
|
||||
Inc(FValue, first * 10 + last);
|
||||
end;
|
||||
|
||||
function TTrebuchet.FindFirstDigit(const ALine: string; out OEndIndex: Integer): Integer;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
for i := 1 to ALine.Length do
|
||||
if CheckDigit(ALine, i, Result, OEndIndex) then
|
||||
Exit;
|
||||
end;
|
||||
|
||||
function TTrebuchet.FindLastDigit(const ALine: string; const AFirstDigit, AFirstDigitEndIndex: Integer): Integer;
|
||||
var
|
||||
i, endIndex: Integer;
|
||||
begin
|
||||
for i := ALine.Length downto AFirstDigitEndIndex + 1 do
|
||||
if CheckDigit(ALine, i, Result, endIndex) then
|
||||
Exit;
|
||||
|
||||
// Returns the first digit if no digit was found after the end index of the first digit.
|
||||
Result := AFirstDigit;
|
||||
end;
|
||||
|
||||
function TTrebuchet.CheckDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer): Boolean;
|
||||
begin
|
||||
if ALine[AIndex] in ['0'..'9'] then
|
||||
begin
|
||||
ODigit := StrToInt(ALine[AIndex]);
|
||||
OEndIndex := AIndex;
|
||||
Result := True;
|
||||
end
|
||||
else if FConsiderSpelledOutDigits and CheckSpelledOutDigit(ALine, AIndex, ODigit, OEndIndex) then
|
||||
begin
|
||||
Result := true;
|
||||
end
|
||||
else begin
|
||||
ODigit := -1;
|
||||
OEndIndex := -1;
|
||||
Result := False;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TTrebuchet.CheckSpelledOutDigit(const ALine: string; const AIndex: Integer; out ODigit, OEndIndex: Integer):
|
||||
Boolean;
|
||||
var
|
||||
i: Integer;
|
||||
digitName: string;
|
||||
begin
|
||||
for i := Ord(Low(TSpelledOutDigit)) to Ord(High(TSpelledOutDigit)) do
|
||||
begin
|
||||
digitName := GetEnumName(TypeInfo(TSpelledOutDigit), Ord(i));
|
||||
if CompareStr(digitName, Copy(ALine, AIndex, digitName.Length)) = 0 then
|
||||
begin
|
||||
ODigit := i;
|
||||
OEndIndex := AIndex + digitName.Length - 1;
|
||||
Result := True;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
ODigit := -1;
|
||||
OEndIndex := -1;
|
||||
Result := False;
|
||||
end;
|
||||
|
||||
function TTrebuchet.Solve(const AConsiderSpelledOutDigits: Boolean): Integer;
|
||||
var
|
||||
data: TextFile;
|
||||
s: string;
|
||||
begin
|
||||
FValue := 0;
|
||||
FConsiderSpelledOutDigits := AConsiderSpelledOutDigits;
|
||||
|
||||
AssignFile(data, ConcatPaths(['data', 'trebuchet_calibration_document.txt']));
|
||||
try
|
||||
reset(data);
|
||||
while (not EOF(data)) do
|
||||
begin
|
||||
readln(data, s);
|
||||
ProcessDataLine(s);
|
||||
end;
|
||||
finally
|
||||
CloseFile(data)
|
||||
end;
|
||||
|
||||
Result := FValue;
|
||||
end;
|
||||
|
||||
class procedure TTrebuchet.Run;
|
||||
var
|
||||
trebuchet: TTrebuchet;
|
||||
begin
|
||||
WriteLn;
|
||||
WriteLn('--- Day 1: Trebuchet?! ---');
|
||||
trebuchet := TTrebuchet.Create;
|
||||
WriteLn('Part 1: ', trebuchet.Solve(False));
|
||||
WriteLn('Part 2: ', trebuchet.Solve(True));
|
||||
trebuchet.Free;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user