Not so long ago, while reading an article on hacker news, I came across a strange little programming puzzle that seemed silly and trivial. I was sure I could solve it in a matter of minutes if I just sit down and think about it a little. Yet, the problem haunted me for weeks, and in one moment I just had to sit down and solve it. And what can I say, it was really hard, way harder than I tough. Here are the rules of the challenge.
You have to write a program that will output itself. You can write it in any programming language, but you have to make sure that it has at least one character in its source file i.e it can’t be an empty file.
Of course, it is considered cheating if you just open the file and print it out. So you can’t do that either.
I will not give you the answer, it is way more fun to try it out personally.
Why would I do this?
When you become a programmer that can actually earn a living by writing code you will probably have the skills to solve many of the programming challenges you get on a daily basis. The only things that stop you are time, and the quality of code you are writing, but if you ignore that, you can probably just write an ugly, unmaintainable, brute force, yet working solution for any problem.
When you start solving this problem, you will soon realize that your usual brute force approach only leads to trouble.
If you consider yourself a hacker or a programming geek you must feel the urge to solve puzzles. I can’t guarantee great insight, or some groundbreaking learning experience, only a heart warming feeling when you finally get your program to output itself.