Added solution for "Day 6: Wait For It", part 1
This commit is contained in:
107
solvers/UWaitForIt.pas
Normal file
107
solvers/UWaitForIt.pas
Normal file
@@ -0,0 +1,107 @@
|
||||
{
|
||||
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 UWaitForIt;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fgl, Math, USolver;
|
||||
|
||||
type
|
||||
|
||||
{ TWaitForIt }
|
||||
|
||||
TWaitForIt = class(TSolver)
|
||||
FTimes, FDistances: specialize TFPGList<Cardinal>;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
procedure ProcessDataLine(const ALine: string); override;
|
||||
procedure Finish; override;
|
||||
function GetDataFileName: string; override;
|
||||
function GetPuzzleName: string; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TWaitForIt }
|
||||
|
||||
constructor TWaitForIt.Create;
|
||||
begin
|
||||
FTimes := specialize TFPGList<Cardinal>.Create;
|
||||
FDistances := specialize TFPGList<Cardinal>.Create;
|
||||
end;
|
||||
|
||||
destructor TWaitForIt.Destroy;
|
||||
begin
|
||||
FTimes.Free;
|
||||
FDistances.Free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TWaitForIt.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
split: TStringArray;
|
||||
list: specialize TFPGList<Cardinal>;
|
||||
i: Integer;
|
||||
begin
|
||||
split := ALine.Split([' ', ':']);
|
||||
if split[0] = 'Time' then
|
||||
list := FTimes
|
||||
else if split[0] = 'Distance' then
|
||||
list := FDistances
|
||||
else
|
||||
Exit;
|
||||
|
||||
for i := 1 to Length(split) - 1 do
|
||||
if split[i] <> '' then
|
||||
begin
|
||||
list.Add(StrToDWord(split[i]));
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TWaitForIt.Finish;
|
||||
var
|
||||
i, x1, x2: Integer;
|
||||
p, s: Double;
|
||||
begin
|
||||
FPart1 := 1;
|
||||
for i := 0 to FTimes.Count - 1 do
|
||||
begin
|
||||
p := FTimes[i] / 2;
|
||||
s := Sqrt(p * p - FDistances[i]);
|
||||
x1 := Math.Floor(p - s + 1);
|
||||
x2 := Math.Ceil(p + s - 1);
|
||||
FPart1 := FPart1 * (x2 - x1 + 1);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TWaitForIt.GetDataFileName: string;
|
||||
begin
|
||||
Result := 'wait_for_it.txt';
|
||||
end;
|
||||
|
||||
function TWaitForIt.GetPuzzleName: string;
|
||||
begin
|
||||
Result := 'Day 6: Wait For It';
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user