Skip to main content
2 of 2
edited tags
FlipTack
  • 14.7k
  • 3
  • 54
  • 101

Permanently self-modifying code

Now, we all know most languages have very simple ways to "self-modify" code. However, what if you were to actually modify the code and edit parts of it...on disk?

Your goal is to make code that prints a number, then edits its own file to replace the number with the next one in the Fibonacci sequence like so:

$ ./program
1
$ ./program
1
$ ./program
2
$ ./program
3
$ ./program
5
[etc...]

Rules

  1. You may not store the number(s) "outside" of the code. No comments, no telling the script to exit, no EOF, etc.
  2. If your code works with any filename, subtract 2 from your byte amount and write $BYTESNOW ($ORIGINALBYTES - 2) in your title. (Filenames are assumed to be within the range of any alphanumeric file path.)
  3. Your code must write the output to the file on it's own, without any external piping assistance.
  4. Your code can start from one or zero. It doesn't matter.