File: abc.d

package info (click to toggle)
haskell-skylighting-core 0.14.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,720 kB
  • sloc: xml: 124,686; haskell: 3,117; cs: 72; ada: 67; java: 37; ansic: 32; cpp: 31; php: 25; tcl: 19; lisp: 14; perl: 11; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,222 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import std.stdio, std.ascii, std.algorithm, std.array, std.range;

alias Block = char[2];

bool canMakeWord(immutable Block[] blocks, in string word) pure nothrow
in {
    assert(blocks.all!(w => w[].all!isAlpha));
    assert(word.all!isAlpha);
} body {
    bool inner(size_t[] indexes, in string w) pure nothrow {
        if (w.empty)
            return true;

        immutable c = w[0].toUpper;
        foreach (ref idx; indexes) {
            if (blocks[idx][0].toUpper != c &&
                blocks[idx][1].toUpper != c)
                continue;
            indexes[0].swap(idx);
            if (inner(indexes[1 .. $], w[1 .. $]))
                return true;
            indexes[0].swap(idx);
        }

        return false;
    }

    return inner(blocks.length.iota.array, word);
}

void main() {
    enum Block[] blocks = "BO XK DQ CP NA GT RE TG QD FS
                           JW HU VI AN OB ER FS LY PC ZM".split;

    foreach (w; "" ~ "A BARK BoOK TrEAT COmMoN SQUAD conFUsE".split)
        writefln(`"%s" %s`, w, blocks.canMakeWord(w));

    // Extra test.
    immutable Block[] blocks2 = ["AB", "AB", "AC", "AC"];
    immutable word = "abba";
    writefln(`"%s" %s`, word, blocks2.canMakeWord(word));
}