{$APPTYPE CONSOLE}
program GuardPatrolGrid_Using_Optional;
{ Input: the puzzle's published sample (AoC 2024 day 6, part 1). https://adventofcode.com/2024/day/6
The map shows the current position of the guard with ^ (to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as #.
Guards follow a very strict patrol protocol which involves repeatedly following these steps:
- If there is something directly in front of you, turn right 90 degrees.
- Otherwise, take a step forward.
Output (Claude, 23 Oct, before compiling):
start: (4, 6)
turns: 10
visited: 41
No warnings on Win32, Win64 }
uses
System.SysUtils, System.Types, Demo.OptionalGrids;
const
Lines: TArray<string> = [
'....#.....',
'.........#',
'..........',
'..#.......',
'.......#..',
'..........',
'.#..^.....',
'........#.',
'#.........',
'......#...'];
type
TDir = (Up, Right, Down, Left);
TCell = (Open, Wall);
TStep =
| Moved (Pos: TPoint) // rust-style enum with payloads
| Turned(Facing: TDir)
| Exited;
function Step(Dir: TDir): TPointDelta;
begin
Exit( match Dir of
| Up : (dx: 0; dy: -1) // spontaneous records
| Right : (dx: 1; dy: 0)
| Down : (dx: 0; dy: 1)
| Left : (dx:-1; dy: 0)
end
);
end;
function Turn(Dir: TDir): TDir;
begin
Exit( match Dir of
| Up : Right
| Right : Down
| Down : Left
| Left : Up
end
);
end;
function Start(const Input: TGrid<Char>): Optional<TPoint>;
begin
for var Row in Input.Rows do
match Row.Find('^') of
| Present(x) : Exit( Present(Point(x, Row.Index)) )
| Absent : {skip row}
end;
Exit( Absent );
end;
function Advance(const Grid: TGrid<TCell>;
const GuardPos: TPoint;
Facing: TDir) : TStep;
begin
const Next := GuardPos + Step(Facing);
Exit( match Grid.TryGet(Next) of
| Present(Wall) : Turned(Turn(Facing))
| Present(Open) : Moved(Next)
| Absent : Exited
end
);
end;
// In Delphi,
// const CharToCell: TFunc<Char, TCell> =
// function(Ch: Char): TCell begin
// exit( if Ch = '#' then Wall else Open );
// end;
// and λch -> False
// function(ch: char): Boolean begin exit( False ); end
//
begin
const FnCharToCell := λ(Ch: Char) -> if Ch = '#' then Wall else Open;
const Input := TGrid<Char>.Create(Lines, Id);
const Grid := Input.Map(FnCharToCell);
var Seen := Input.Map(λch -> False);
match Start(Input) of
| Present(Pos) : var GuardPos := Pos
| Absent : begin writeln('no start point found'); Exit; end
end;
Seen[GuardPos] := True;
var Facing := Up;
var Turns := 0;
var Visited := 1;
writeln('start: ', GuardPos.ToString);
while True do
begin
match Advance(Grid, GuardPos, Facing) of
| Moved(Pos) : begin
if not Seen[Pos] then begin Inc(Visited); Seen[Pos] := True; end;
GuardPos := Pos;
end
| Turned(Dir) : begin Inc(Turns); Facing := Dir; end
| Exited : Break
end;
end;
writeln('turns: ', Turns);
writeln('visited: ', Visited);
end.
{$APPTYPE CONSOLE}
program Hangman;
{ A small Delf demo for a forum post
- match on strings
- for … in with Enumeration.
The guesses are pre-decided, so every run prints the same.
Output :
e _ E _ _ _ _
hint a language, and an oracle
x no, 4 chances left
l _ E L _ _ _
p _ E L P _ _
? a language, and an oracle
z no, 3 chances left
d D E L P _ _
h D E L P H _
i D E L P H I
solved, with 3 chances left }
uses
System.SysUtils;
const
Word = 'DELPHI';
Guesses : TArray<string> = ['e', 'hint', 'x', 'l', 'p', '?', 'z', 'd', 'h', 'i', 'quit'];
Lives = 5;
type
TMatches = TInitArray<Boolean>;
function ToString(const Found: TMatches): string;
begin
Result := '';
for var (i, ch) in Word.Enumerate do
Result := Result + ( if Found[i] then ch else '_' ) + ' ';
end;
function Reveal(Letter: Char; var Found: TMatches): Boolean;
begin
Result := False;
for var (i, ch) in Word.Enumerate do
if ch = Letter then begin Found[i] := True; Result := True; end;
end;
function Solved(const Found: TMatches): Boolean;
begin
for const b in Found do if not b then Exit( False );
Exit( True );
end;
begin
var Found := TMatches.create( Length(Word), False );
var Left := Lives;
for const Guess in Guesses do
begin
Write(Guess.PadRight(6));
match Guess.ToUpper of
| 'HINT', '?' : writeln('a language, and an oracle')
| 'QUIT' : begin writeln('bye'); Break; end
| _ : if Reveal(Guess.ToUpper.Chars[0], Found) then
writeln(Found.ToString)
else
begin Dec(Left); writeln('no, ', Left, ' chances left'); end
end;
if Solved(Found) then begin writeln('solved, with ', Left, ' chances left'); Break; end;
if Left = 0 then begin writeln('hanged'); Break; end;
end;
end.
Equivalent
function Start(const Input: TGrid<Char>): Optional<TPoint>;
begin
for var Row in Input.Rows do
match Row.Find('^') of
| Present(x) : Exit( Present(Point(x, Row.Index)) )
| Absent : {skip}
end;
Exit( Absent );
end;
function Start(const Input: TGrid<Char>): Optional<TPoint>;
begin
for var Row in Input.Rows do
if Row.Find('^') is Present(x) then Exit( Present(Point(x, Row.Index)) );
Exit( Absent );
end;
I saw on a Delphi-Praxis post that @davidberneda had a repo with a somewhat similar goal …
(hmmm .. maybe I’ve linked it before on the Forum somewhere
)
// back arrow means Const x := ...
begin
FnCharToCell ← λ(ch: Char) -> if ch = '#' then Wall { Type Assertion }
else Open; { ::t Char -> TCell }
// function(ch:Char): TCell begin ... end;
Input ← TGrid<Char>.Create(Lines, Id); // convert const stringgrid input to TGrid<Char>
Grid ← Input.Map(FnCharToCell); // create identical Grid of TCells
var Seen := Input.Map(λch -> False); // create identical Grid of Booleans
end;
// 'normal'
begin
const FnCharToCell := λ(ch: Char) -> if ch = '#' then Wall { Type Assertion }
else Open; { ::t Char -> TCell }
// function(ch:Char): TCell begin ... end;
const Input = TGrid<Char>.Create(Lines, Id); // convert const stringgrid input to TGrid<Char>
const Grid = Input.Map(FnCharToCell); // create identical Grid of TCells
var Seen := Input.Map(λch -> False); // create identical Grid of Booleans
end;














