Word ladder: Difference between revisions
→{{header|Ada}}
Line 89:
=={{header|Ada}}==
Changed my solution to use Multiway_Trees.
<syntaxhighlight lang="ada">
pragma Ada_2022;
with Ada.Containers.Multiway_Trees;
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 103 ⟶ 104:
subtype LC_Chars is Character range 'a' .. 'z';
type
end record;
package Word_Vectors is new Ada.Containers.Vectors (Positive, Unbounded_String);
package
package
use Word_Trees;
Solution : Cursor;
function Load_Candidate_Words (Dict_Filename : String; Word_Len : Positive)
return
Dict_File : File_Type;
Read_Word : Unbounded_String;
Cands :
Valid : Boolean;
C : Character;
Line 137 ⟶ 143:
end Load_Candidate_Words;
function Mutate (Word : Unbounded_String;
Used : Used_Vectors.Vector;▼
begin▼
for i in 1 .. Vec_Len loop▼
▲ function Mutate (Word : Unbounded_String; Len : Positive; Dict : Word_Vectors.Vector)
return Word_Vectors.Vector is
Mutations : Word_Vectors.Vector;
Poss_Word : Unbounded_String;
begin
for Ix in 1 ..
for Letter in LC_Chars loop
if Letter /= Element (Word, Ix) then
Line 159 ⟶ 155:
if Dict.Contains (Poss_Word) then
Mutations.Append (Poss_Word);
Dict.Delete (Dict.Find_Index (Poss_Word));
end if;
end if;
Line 166 ⟶ 163:
end Mutate;
procedure
Level : Positive;
Dict : in out Dict_Vectors.Vector) is
▲ Word_Tree : Mutation_Arr;
Mutations : Word_Vectors.Vector;
▲ begin
if Element (Pos).Level = Level then
▲ Start, Target, Word : Unbounded_String;
if not Word_Vectors.Is_Empty (Mutations) then
if
end if;
if not Solved
Recurse_Tree (First_Child (Pos),
end
end Recurse_Tree;
procedure Ladder (Start_S, Target_S : String) is
Word_Node : Word_Node_T;
Start, Target : Unbounded_String;
begin
if Start_S'Length /= Target_S'Length then
Line 199 ⟶ 206:
end if;
Dictionary := Load_Candidate_Words (DICT_FILENAME, Start_S'Length);
Start := To_Unbounded_String (Start_S);
Target := To_Unbounded_String (Target_S);
Word_Node.Level :=
Word_Node.Word
▲ Mutations := Mutate (Start, Start_S'Length, Dictionary);
Word_Tree.Insert_Child (Word_Tree.Root, No_Element, Word_Node);
Start_Pos := Find (Word_Tree,
while Level <= MAX_DEPTH and then not Solved loop
Recurse_Tree (Start_Pos, Level, Target, Dictionary);
▲ Check_Mutations;
▲ end loop;
▲ end if;
▲ Word_Tree (Level) := Muts_Vec;
Level := @ + 1;
end loop;
if not
Put_Line (Start & " -> " & Target & " - No solution found at depth" & MAX_DEPTH'Image);
else
▲ Word := Word_Rec.Parent;
▲ exit;
end loop;
end if;
end Ladder;
Line 248 ⟶ 242:
As expected "ada" can become a "god", and "rust" can go to "hell" :-)
<pre>
child -> adult - No solution found at depth 50
</pre>
|