Task: ABC problem








VB: arraybase 1
blocks$   = "BO,XK,DQ,CP,NA,GT,RE,TG,QD,FS,JW,HU,VI,AN,OB,ER,FS,LY,PC,ZM"
makeWord$ = "A,BARK,BOOK,TREAT,COMMON,SQUAD,Confuse"
b         = int((length(blocks$) /3) +  1)
dim blk$(b)

for i = 1 to length(makeWord$)
	wrd$ = word$(makeWord$,i,",")
	dim hit(b)
	n = 0
	if wrd$ = "" then exit for
	for k = 1 to length(wrd$)
		w$ = upper(mid(wrd$,k,1))
		for j = 1 to b
			if hit[j] = 0 then
				if w$ = left(word$(blocks$,j,","),1) or w$ = right(word$(blocks$,j,","),1) then
					hit[j] = 1
					n += 1
					exit for
				end if
			end if
		next j
	next k
	print wrd$; chr(9);
	if n = length(wrd$) then print " True" else print " False"
next i
end

function word$(sr$, wn, delim$)
	j = wn
	if j = 0 then j += 1
	res$ = "" : s$ = sr$ : d$ = delim$
	if d$ = "" then d$ = " "
	sd = length(d$) : sl = length(s$)
	while true
		n = instr(s$,d$) : j -= 1
		if j = 0 then
			if n = 0 then res$ = s$ else res$ = mid(s$,1,n-1)
			return res$
		end if
		if n = 0 then return res$
		if n = sl - sd then res$ = "" : return res$
		sl2 = sl-n : s$ = mid(s$,n+1,sl2) : sl = sl2
	end while
	return res$
end function




Ada: with Ada.Characters.Handling;
use Ada.Characters.Handling;


package Abc is
    type Block_Faces is array(1..2) of Character;
    type Block_List is array(positive range <>) of Block_Faces;
    function Can_Make_Word(W: String; Blocks: Block_List) return Boolean;
end Abc;


package body Abc is

function Can_Make_Word(W: String; Blocks: Block_List) return Boolean is
    Used : array(Blocks'Range) of Boolean := (Others => False);
    subtype wIndex is Integer range W'First..W'Last;
    wPos : wIndex;
begin
    if W'Length = 0 then
        return True;
    end if;
    wPos := W'First;
    while True loop
        declare
            C : Character := To_Upper(W(wPos));
            X : constant wIndex := wPos;
        begin
            for I in Blocks'Range loop
                if (not Used(I)) then
                    if C = To_Upper(Blocks(I)(1)) or C = To_Upper(Blocks(I)(2)) then
                        Used(I) := True;
                        if wPos = W'Last then
                            return True;
                        end if;
                        wPos := wIndex'Succ(wPos);
                        exit;
                    end if;
                end if;
            end loop;
            if X = wPos then
                return False;
            end if;
        end;
    end loop;
    return False;
end Can_Make_Word;

end Abc;

with Ada.Text_IO, Ada.Strings.Unbounded, Abc;
use Ada.Text_IO, Ada.Strings.Unbounded, Abc;

procedure Abc_Problem is
    Blocks : Block_List := (
          ('B','O'), ('X','K'), ('D','Q'), ('C','P')
        , ('N','A'), ('G','T'), ('R','E'), ('T','G')
        , ('Q','D'), ('F','S'), ('J','W'), ('H','U')
        , ('V','I'), ('A','N'), ('O','B'), ('E','R')
        , ('F','S'), ('L','Y'), ('P','C'), ('Z','M')
    );
    function "+" (S : String) return Unbounded_String renames To_Unbounded_String;
    words : array(positive range <>) of Unbounded_String := (
          +"A"
        , +"BARK"
        , +"BOOK"
        , +"TREAT"
        , +"COMMON"
        , +"SQUAD"
        , +"CONFUSE"
        
        
        
    );
begin
    for I in words'Range loop
        Put_Line ( To_String(words(I)) & ": " & Boolean'Image(Can_Make_Word(To_String(words(I)),Blocks)) );
    end loop;
end Abc_Problem;