Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge #9002

Name: Anonymous 2011-05-02 20:12

The task:

Print strings from "a" to "zzzzz" without using any loop or conditional statements. Don't just write all 1000 permutations out by hand. The output should look like this:
a
b
c
...
aa
ab
ac
...
zzzzx
zzzzy
zzzzz


inb4 lipthfags and dead dogs using some obscure functionality of their obscure languages.

Name: Anonymous 2011-05-03 1:22

Pretty sure this uses absolutely no loops or conditionals... But it requires a huge stack to run without crashing. I haven't run it to the end so I'm not even sure if a stack size of 100,000,000 is big enough.


#pragma comment(linker, "/stack:100000000")

#include <iostream>
#include <cstdlib>

template<typename T>
struct true_define
{
    typedef T (*is_true)(T);
};

template<typename T>
T is_true1(T x)
{
    return x;
}

template<typename T>
T is_true2(T x)
{
    return 1;
}

template<typename T, int I>
struct check_bits
{
    static T check(T x, T cur, typename true_define<T>::is_true tests[2])
    {
        cur = tests[cur]((x >> I) & 1);
        return check_bits<T, I-1>::check(x, cur, tests);
    }
};

template<typename T>
struct check_bits<T, 0>
{
    static T check(T x, T cur, typename true_define<T>::is_true tests[2])
    {
        cur = tests[cur](x & 1);
        return cur;
    }
};

template<typename T>
T set_true(T x)
{
    true_define<T>::is_true tests[2];
    tests[0] = is_true1;
    tests[1] = is_true2;
    return check_bits<T, (sizeof(T)*8)-1>::check(x, 0, tests);
}

class if_object;

void call_then_(if_object* obj);
void call_else_(if_object* obj);

class if_object
{
public:
    friend void call_then_(if_object*);
    friend void call_else_(if_object*);

    if_object()
    {
        calls[0] = call_else_;
        calls[1] = call_then_;
    }

    template<typename T>
    void if_(T x)
    {
        calls[set_true(x)](this);
    }
protected:
    virtual void then_() = 0;
    virtual void else_() = 0;
private:
    typedef void (*if_call)(if_object*);
    if_call calls[2];
};

void call_then_(if_object* obj)
{
    obj->then_();
}

void call_else_(if_object* obj)
{
    obj->else_();
}

class exit_if_not : public if_object
{
public:
    template<typename T>
    exit_if_not(T x)
    {
        this->if_(x);
    }
private:
    void then_()
    {
    }

    void else_()
    {
        std::cin.get();
        exit(0);
    }
};

class incrementer2 : public if_object
{
public:
    incrementer2(char* c_) : c(c_) {}
private:
    void then_()
    {
        ++(*c);
    }
    void else_()
    {
        *c = 'a';
    }
    char* c;
};

class incrementer : public if_object
{
public:
    incrementer(char* c_, int depth_, int* fin_) : c(c_), depth(depth_), fin(fin_) {}
private:
    void then_()
    {
        exit_if_not ex(depth);
        *c = 'a';
        incrementer inc(c-1, depth-1, fin);
        inc.if_(*(c-1) / 'z');
    }

    void else_()
    {
        exit_if_not ex(depth);
        incrementer2 inc2(c);
        inc2.if_(*c);
    }

    char* c;
    int depth;
    int* fin;
};

class string_printer : public if_object
{
public:
    string_printer(char c_) : c(c_)
    {
        this->if_(c);
    }
private:
    void then_()
    {
        std::cout << c;
    }

    void else_()
    {
    }

    char c;
};

void print_string(char* s)
{
    string_printer s1(s[0]);
    string_printer s2(s[1]);
    string_printer s3(s[2]);
    string_printer s4(s[3]);
    string_printer s5(s[4]);
    std::cout << std::endl;
}

inline void recursive_increment(char* p)
{
    int fin = 0;
    incrementer inc(p+4, 5, &fin);
    inc.if_(p[4] / 'z');
    print_string(p);
    recursive_increment(p);
}

int main()
{
    char sbegin[5];
    sbegin[0] = 0;
    sbegin[1] = 0;
    sbegin[2] = 0;
    sbegin[3] = 0;
    sbegin[4] = 0;
    recursive_increment(sbegin);
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List