Competition

Okay, I lied, it's not a real competition, it's just a pretend one. Points are for coming up with the most needlessly complex program which should be simple. The language doesn't matter, you can enter whatever you want whenever you want as many times as you want. The only rules are (1) it must work (2) it must be needlessly complex and (3) it must do something simple. I might pick a winner at some point.

As an example, here's a hello world in Python which uses sockets, threading and metaprogramming:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python

import socket
import sys

HOST = "127.0.0.1"
PORT = 9001

def client():
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
	data = sock.recv(1024)
	for i in range(len(data)):
                if data[i] not in [ '\r', '\n' ]:
                        code = ScalableEnterpriseCharacterPrinterFactory(data[i]).code
                        exec(code)
	sock.close()
	print ''

class ScalableEnterpriseCharacterPrinterFactory():
	def __init__(self, character):
		self.code = """
import threading

class ScalableEnterpriseCharacterPrinter(threading.Thread):
	def __init__(self, character):
		threading.Thread.__init__(self)
		self.character = character
		self.start()
	def run(self):
		sys.stdout.write(self.character)

ScalableEnterpriseCharacterPrinter('""" + character + """')"""

message = 'hello, world'

def server():
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.bind((HOST, PORT))
	sock.listen(1)
	while True:
		conn, addr = sock.accept()
		conn.setblocking(1)
		conn.send(message)
		conn.close()

if __name__ == '__main__':
	if len(sys.argv) < 2:
		print("Usage: %s <client|server>" % sys.argv[0])
		exit(1)
	elif sys.argv[1] == "client":
		client()
	elif sys.argv[1] == "server":
		server()
	else:
		print("Usage: %s <client|server>" % sys.argv[0])
		exit(1)

My favourite thing about it is that occasionally it doesn't even print the letters in the correct order (sometimes the newline comes before the 'd' in 'world').
Have you checked this out?

The International Obfuscated C Code Contest
http://www.ioccc.org/

Yeah, I know about IOCCC.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>

class Node {
public:
    Node *next;

    Node(char);
    ~Node();
    char getValue() const;
    void setValue(char);

private:
    char value;
};

Node::Node(char value):
    next(0),
    value(value)
{
}

Node::~Node()
{
    delete next;
}

char Node::getValue() const
{
    return value;
}

void Node::setValue(char value)
{
    this->value = value;
}

class DumbList {
public:
    DumbList();
    ~DumbList();
    void push(const Node &);
    void print() const;

private:
    Node *head;
};

DumbList::DumbList():
    head(0)
{
}

DumbList::~DumbList()
{
    delete head;
}

void DumbList::push(const Node &node)
{
    if (head != 0)
    {
        Node *last = head;

        while (last->next != 0)
            last = last->next;

        last->next = new Node(node.getValue());
    }
    else
        head = new Node(node.getValue());
}

void DumbList::print() const
{
    Node *current = head;

    while (current != 0)
    {
        std::cout << current->getValue();
        current = current->next;
    }

    std::cout << std::endl;
}

int main()
{
    DumbList dl;

    dl.push('H');
    dl.push('e');
    dl.push('l');
    dl.push('l');
    dl.push('o');
    dl.push(',');
    dl.push(' ');
    dl.push('W');
    dl.push('o');
    dl.push('r');
    dl.push('l');
    dl.push('d');
    dl.push('!');
    dl.print();
}

Tried to slap something together that would be as few lines as possible:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <class T>
class Return
{
public:
    Return<T>(T* ptr) { m_x = reinterpret_cast<const void*>(const_cast<const T*>(ptr)); }

    template <class T>
    T Get() { return *(reinterpret_cast<T*>(const_cast<void*>(m_x))); }

private:
    const void* m_x;
};

template <class T>
int EndOfMain(Return<T> ret) { return static_cast<int>(ret.Get<T>()); }

#define RETURN EndOfMain

int main()
{
    return RETURN(Return<int>(new int(0)));
}
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
43
44
typedef void (*gadget)() __attribute__((fastcall));
gadget eax_from_esi_call_int;
gadget set_esi;
gadget set_ebx;
gadget set_edx;
 
void graceful_exit()
{
  set_ebx(0);
  set_esi(1);
  eax_from_esi_call_int(0);
}
 
volatile unsigned int something_wicked_this_way_comes(
    int a, int b, int c, int d) {
  a ^= 0xC3CA8900;  b ^= 0xC3CB8900;  c ^= 0xC3CE8900;  d ^= 0x80CDF089;
  graceful_exit();
  return a+b+c+d;
}
 
void* find_the_witch(unsigned short witch) {
  unsigned char *p = (unsigned char*)something_wicked_this_way_comes;
  int i;
  for(i = 0; i < 50; i++, p++) {
    if(*(unsigned short*)p == witch) return (void*)p;
  }
 
  return (void*)0;
}
 
int main(void) {
  eax_from_esi_call_int = (gadget)find_the_witch(0xF089);
  set_esi = (gadget)find_the_witch(0xCE89);
  set_ebx = (gadget)find_the_witch(0xCB89);
  set_edx = (gadget)find_the_witch(0xCA89);
 
  if(!eax_from_esi_call_int) return 1;
  if(!set_esi) return 3;
  if(!set_ebx) return 4;
  if(!set_edx) return 5;
 
  set_edx(12), set_ebx(1), set_esi(4);
  eax_from_esi_call_int("Hello World\n"); 
}


Look ma, no libraries and no headers used! (I'm not the original author of this code, but, hey, it was so cool I couldn't resist). Unfortunately works only in 32-bit mode.
Last edited on
1
2
3
4
5
#include <cstdlib>
int main()
  {
  std::system( "echo Hello world!" );
  }

I don't know if this will count for you, LOL.
Duoas:
You forgot std::system("pause"); :(
Oh, yeah, thanks.

1
2
3
4
5
6
#include <cstdlib>
int main()
  {
  std::system( "echo Hello world!" );
  if (std::system( "read -rsn 1 -p \"Press any key to continue...\"" )) std::system( "pause" );  // works on *nix too!
  }

[edit] Oops! Fixed "cls"ism.
Last edited on
1
2
3
4
5
6
7
8
9
#include <cstdlib>

int main()
{
	std::system("echo Hello, World! > temp");
	std::system("type temp");
	std::system("pause");
	std::system("del temp");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
	if (argc == 1)
		std::system((std::string(argv[0]) + " !dlroWolleH~ ").c_str());
	else
	if (argv[1][0] != '~')
	{
		std::system((std::string(argv[0]) + ' ' + (argv[1]+1)).c_str());
		std::system((std::string("echo ") + argv[1][0]).c_str());
	}
}

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;

class Gate;

enum op_type_t
{
 op_nop,op_xor,op_or,op_and,op_not
};

struct wire_dest_t
{

  Gate *signal_dest;
  int input_wire;
};


class Gate
{
  bool in_a,in_b;
  bool is_a,is_b;
  op_type_t op;

  vector<wire_dest_t> signal_dest;

  virtual void Signal()
  {
      if(op==op_nop)
        return;
      if(signal_dest.size()==0)
          throw(logic_error("not connected"));

      bool gate_output=DoLogic();
      for(int i=0;i<signal_dest.size();i++)
      {
          if(signal_dest[i].input_wire==0)
              signal_dest[i].signal_dest->SetBitOne(DoLogic());
          else if(signal_dest[i].input_wire==1)
              signal_dest[i].signal_dest->SetBitTwo(DoLogic());
      }

  }

  bool DoLogic()
  {
    switch(op)
    {
      case op_xor:
          return in_a^in_b;
          break;
      case op_or:
          return in_a|in_b;
          break;
      case op_and:
          return in_a&in_b;
          break;
      case op_not:
          return !in_a;
          break;
      default: throw logic_error("Unknown logic gate operation");
    }
  }

public:
  Gate(op_type_t op)
  {
      this->op=op;
      is_a=false,is_b=false;
      in_a=false,in_b=false;

      if(op==op_not)
        is_b=true;

  }
  Gate()
  {
      this->op=op_nop;
      is_a=false,is_b=false;
      in_a=false,in_b=false;
  }
  virtual void SetBitOne(bool a)
  {
      in_a=a;
      is_a=true;
      if(is_b)
          Signal();
  }
  void SetBitTwo(bool b)
  {
      in_b=b;
      is_b=true;
      if(is_a)
          Signal();
  }
  void WireTo(Gate *g,int wire)
  {
      wire_dest_t tmp;
      tmp.signal_dest=g;
      tmp.input_wire=wire;
      signal_dest.push_back(tmp);
  }

  void SetType(op_type_t op){this->op=op;}
};

class Sender
{
  vector<wire_dest_t> signal_dest;
  bool bit;
public:
  void SetBit(bool b){bit=b;}
  void Start()
  {
      if(signal_dest.size()==0)
          throw(logic_error("not connected"));

      for(int i=0;i<signal_dest.size();i++)
      {
          if(signal_dest[i].input_wire==0)
              signal_dest[i].signal_dest->SetBitOne(bit);
          else if(signal_dest[i].input_wire==1)
              signal_dest[i].signal_dest->SetBitTwo(bit);
      }
  }
  void WireTo(Gate *g,int wire)
  {
      wire_dest_t tmp;
      tmp.signal_dest=g;
      tmp.input_wire=wire;
      signal_dest.push_back(tmp);
  }
};


class Receiver:public Gate
{
  bool recv;
  void Signal(){}
public:
  Receiver() : Gate(op_not){}

  void SetBitOne(bool a){recv=a;}
  bool GetResult(){return recv;}
};


int multiply(int a,int b)
{

Gate g[8];
for(int i=0;i<6;i++)
    g[i].SetType(op_and);
g[6].SetType(op_xor);
g[7].SetType(op_xor);

Sender snd[4];
Receiver rc[4];

// Connect the senders
snd[0].WireTo(&g[0],0);
snd[0].WireTo(&g[1],0);

snd[1].WireTo(&g[2],0);
snd[1].WireTo(&g[3],0);

snd[2].WireTo(&g[1],1);
snd[2].WireTo(&g[2],1);

snd[3].WireTo(&g[0],1);
snd[3].WireTo(&g[3],1);

// Connect the gates
g[0].WireTo(&g[6],0);
g[0].WireTo(&g[4],0);

g[1].WireTo(&rc[0],0);

g[2].WireTo(&g[6],1);
g[2].WireTo(&g[4],1);

g[3].WireTo(&g[5],1);
g[3].WireTo(&g[7],1);

g[4].WireTo(&g[5],0);
g[4].WireTo(&g[7],0);


g[5].WireTo(&rc[3],0);
g[6].WireTo(&rc[1],0);
g[7].WireTo(&rc[2],0);

// Set bits
snd[0].SetBit(a&1);
snd[1].SetBit(a&2);
snd[2].SetBit(b&1);
snd[3].SetBit(b&2);

for(int i=0;i<4;i++)
    snd[i].Start();


int result=0;
for(int i=0;i<4;i++)
    result+=rc[i].GetResult()<<i;

return result;
}

int main()
{
int a=2,b=2; // numbers must be in range of [0;3]

cout <<a<<" * "<<b<<" = "<<multiply(a,b)<<endl;

}
Topic archived. No new replies allowed.