From 585083f5ca55b9aadd0677f3db32d017b8e2d5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sat, 2 Dec 2023 00:25:07 +0100 Subject: [PATCH] Added solution for "Day 1: Trebuchet?!", part 1 --- AdventOfCode.lpi | 69 +++++++++++++++++++++++++++ AdventOfCode.lpr | 103 +++++++++++++++++++++++++++++++++++++++++ puzzles/utrebuchet.pas | 81 ++++++++++++++++++++++++++++++++ 3 files changed, 253 insertions(+) create mode 100644 AdventOfCode.lpi create mode 100644 AdventOfCode.lpr create mode 100644 puzzles/utrebuchet.pas diff --git a/AdventOfCode.lpi b/AdventOfCode.lpi new file mode 100644 index 0000000..4333dce --- /dev/null +++ b/AdventOfCode.lpi @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes> + <Item Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <UseFileFilters Value="True"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <Units> + <Unit> + <Filename Value="AdventOfCode.lpr"/> + <IsPartOfProject Value="True"/> + </Unit> + <Unit> + <Filename Value="puzzles\utrebuchet.pas"/> + <IsPartOfProject Value="True"/> + <UnitName Value="UTrebuchet"/> + </Unit> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <PathDelim Value="\"/> + <Target> + <Filename Value="bin\AdventOfCode"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <OtherUnitFiles Value="puzzles"/> + <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + <Linking> + <Debugging> + <DebugInfoType Value="dsDwarf3"/> + </Debugging> + </Linking> + </CompilerOptions> + <Debugging> + <Exceptions> + <Item> + <Name Value="EAbort"/> + </Item> + <Item> + <Name Value="ECodetoolError"/> + </Item> + <Item> + <Name Value="EFOpenError"/> + </Item> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/AdventOfCode.lpr b/AdventOfCode.lpr new file mode 100644 index 0000000..173894a --- /dev/null +++ b/AdventOfCode.lpr @@ -0,0 +1,103 @@ +{ + 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/>. +} + +program AdventOfCode; + +{$mode objfpc}{$H+} + +uses + {$IFDEF UNIX} + cthreads, + {$ENDIF} + Classes, SysUtils, CustApp, UTrebuchet; + +type + + { TAdventOfCode } + + TAdventOfCode = class(TCustomApplication) + private + procedure RunPuzzleSolutions; + protected + procedure DoRun; override; + public + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + procedure WriteHelp; virtual; + end; + +{ TAdventOfCode } + +procedure TAdventOfCode.RunPuzzleSolutions; +begin + TTrebuchet.Solve; +end; + +procedure TAdventOfCode.DoRun; +var + ErrorMsg: String; +begin + // quick check parameters + ErrorMsg := CheckOptions('h', 'help'); + if ErrorMsg <> '' then + begin + ShowException(Exception.Create(ErrorMsg)); + Terminate; + Exit; + end; + + // parse parameters + if HasOption('h', 'help') then + begin + WriteHelp; + Terminate; + Exit; + end; + + { add your program here } + RunPuzzleSolutions; + + // stop program loop + Terminate; +end; + +constructor TAdventOfCode.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + StopOnException := True; +end; + +destructor TAdventOfCode.Destroy; +begin + inherited Destroy; +end; + +procedure TAdventOfCode.WriteHelp; +begin + { add your help code here } + writeln('Usage: ', ExeName, ' -h'); +end; + +var + Application: TAdventOfCode; +begin + Application := TAdventOfCode.Create(nil); + Application.Title := 'Advent of Code 2023'; + Application.Run; + Application.Free; +end. + diff --git a/puzzles/utrebuchet.pas b/puzzles/utrebuchet.pas new file mode 100644 index 0000000..0cd514e --- /dev/null +++ b/puzzles/utrebuchet.pas @@ -0,0 +1,81 @@ +unit UTrebuchet; + +{$mode ObjFPC}{$H+} + +interface + +uses + Classes, SysUtils; + +type + + { TTrebuchet } + + TTrebuchet = class(TObject) + private + FValue: Integer; + procedure RunSolution; + procedure ProcessDataLine(const ALine: string); + public + class procedure Solve; static; + constructor Create; + end; + +implementation + +{ TTrebuchet } + +procedure TTrebuchet.RunSolution; +var + data: TextFile; + s: string; +begin + 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; + WriteLn(FValue); +end; + +procedure TTrebuchet.ProcessDataLine(const ALine: string); +var + c: Char; + first, last: Integer; +begin + first := -1; + last := -1; + for c in ALine do + begin + if c in ['0'..'9'] then + begin + last := StrToInt(c); + if first < 0 then + first := last; + end; + end; + Inc(FValue, first * 10 + last); +end; + +class procedure TTrebuchet.Solve; +var + trebuchet: TTrebuchet; +begin + WriteLn('--- Day 1: Trebuchet?! ---'); + trebuchet := TTrebuchet.Create; + trebuchet.RunSolution; +end; + +constructor TTrebuchet.Create; +begin + FValue := 0; +end; + +end. +