Added solution for "Day 24: Never Tell Me The Odds", part 1
This commit is contained in:
128
solvers/UNeverTellMeTheOdds.pas
Normal file
128
solvers/UNeverTellMeTheOdds.pas
Normal file
@@ -0,0 +1,128 @@
|
||||
{
|
||||
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 UNeverTellMeTheOdds;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, Generics.Collections, Math, USolver;
|
||||
|
||||
type
|
||||
|
||||
{ THailstone }
|
||||
|
||||
THailstone = record
|
||||
X, Y, Z: Int64;
|
||||
VX, VY, VZ: Integer;
|
||||
end;
|
||||
|
||||
THailstones = specialize TList<THailstone>;
|
||||
|
||||
{ TNeverTellMeTheOdds }
|
||||
|
||||
TNeverTellMeTheOdds = class(TSolver)
|
||||
private
|
||||
FMin, FMax: Int64;
|
||||
FHailStones: THailstones;
|
||||
function AreIntersecting(constref AHailstone1, AHailstone2: THailstone): Boolean;
|
||||
public
|
||||
constructor Create(const AMin: Int64 = 200000000000000; const AMax: Int64 = 400000000000000);
|
||||
destructor Destroy; override;
|
||||
procedure ProcessDataLine(const ALine: string); override;
|
||||
procedure Finish; override;
|
||||
function GetDataFileName: string; override;
|
||||
function GetPuzzleName: string; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TNeverTellMeTheOdds }
|
||||
|
||||
function TNeverTellMeTheOdds.AreIntersecting(constref AHailstone1, AHailstone2: THailstone): Boolean;
|
||||
var
|
||||
m1, m2, x, y: Double;
|
||||
begin
|
||||
Result := False;
|
||||
m1 := AHailstone1.VY / AHailstone1.VX;
|
||||
m2 := AHailstone2.VY / AHailstone2.VX;
|
||||
if m1 <> m2 then
|
||||
begin
|
||||
x := (AHailstone2.Y - m2 * AHailstone2.X - AHailstone1.Y + m1 * AHailstone1.X) / (m1 - m2);
|
||||
if (FMin <= x) and (x <= FMax)
|
||||
and (x * Sign(AHailstone1.VX) >= AHailstone1.X * Sign(AHailstone1.VX))
|
||||
and (x * Sign(AHailstone2.VX) >= AHailstone2.X * Sign(AHailstone2.VX)) then
|
||||
begin
|
||||
y := m1 * (x - AHailstone1.X) + AHailstone1.Y;
|
||||
if (FMin <= y) and (y <= FMax) then
|
||||
Result := True
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TNeverTellMeTheOdds.Create(const AMin: Int64; const AMax: Int64);
|
||||
begin
|
||||
FMin := AMin;
|
||||
FMax := AMax;
|
||||
FHailStones := THailstones.Create;
|
||||
end;
|
||||
|
||||
destructor TNeverTellMeTheOdds.Destroy;
|
||||
begin
|
||||
FHailStones.Free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TNeverTellMeTheOdds.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
split: TStringArray;
|
||||
hailstone: THailstone;
|
||||
begin
|
||||
split := ALine.Split([',', '@']);
|
||||
hailstone.X := StrToInt64(Trim(split[0]));
|
||||
hailstone.Y := StrToInt64(Trim(split[1]));
|
||||
hailstone.Z := StrToInt64(Trim(split[2]));
|
||||
hailstone.VX := StrToInt(Trim(split[3]));
|
||||
hailstone.VY := StrToInt(Trim(split[4]));
|
||||
hailstone.VZ := StrToInt(Trim(split[5]));
|
||||
FHailStones.Add(hailstone);
|
||||
end;
|
||||
|
||||
procedure TNeverTellMeTheOdds.Finish;
|
||||
var
|
||||
i, j: Integer;
|
||||
begin
|
||||
for i := 0 to FHailStones.Count - 2 do
|
||||
for j := i + 1 to FHailStones.Count - 1 do
|
||||
if AreIntersecting(FHailStones[i], FHailStones[j]) then
|
||||
Inc(FPart1);
|
||||
end;
|
||||
|
||||
function TNeverTellMeTheOdds.GetDataFileName: string;
|
||||
begin
|
||||
Result := 'never_tell_me_the_odds.txt';
|
||||
end;
|
||||
|
||||
function TNeverTellMeTheOdds.GetPuzzleName: string;
|
||||
begin
|
||||
Result := 'Day 24: Never Tell Me The Odds';
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user