17
2
In a programming language of your choice, write a full program that, when run, prints a positive integer N and then modifies its own source file such that the next time it is run it will print N+1.
For example, this (ungolfed) Python 3 program satisfies this behavior:
N = 1
print(N)
with open(__file__, 'r+') as f:
N = int(f.readline()[4:]) + 1
code = 'N = ' + str(N) + '\n' + f.read()
f.seek(0)
f.write(code)
Right away it declares and prints N, then it opens itself and rewrites its first line so the declared value of N is incremented. Then it copies the rest of its code verbatim and writes the new code to the same file, resulting in a program that will print N+1 when run again.
If the file were named incr.py
, running it on the command line would look something like this:
C:\somepath> python incr.py
1
C:\somepath> python incr.py
2
C:\somepath> python incr.py
3
C:\somepath> python incr.py
4
At this point, incr.py
would read:
N = 5
print(N)
with open(__file__, 'r+') as f:
N = int(f.readline()[4:]) + 1
code = 'N = ' + str(N) + '\n' + f.read()
f.seek(0)
f.write(code)
Your program should behave essentially the same as this Python example, though of course your program structure may be very different, even changing as N increases. (You don't need to define N as a plain integer on the first line, for example.)
Give the program that prints 1
in your answer. Programs for all higher N can then be inferred. The shortest 1
-printing program in bytes wins.
Notes
The output should be the sole number N in decimal (or your language's most natural base), followed optionally by a trailing newline.
Your program must actually rewrite its own source code file, not simply output its modified source like a quine.
The file name and/or path of the source file may be hardcoded into your program (e.g.
'incr.py'
could have replaced__file__
in the Python example) but the program's behavior should not depend on them. If the file is moved or renamed it should be easy enough to rewrite the program accordingly.The source file's name and path should not change during execution.
Temporary files may be made (to copy code to or whatever) but they should be removed before the main program ends. Specifically, the only side effects of running your program should be:
Printing N to stdout.
Changing the source file so it will print N+1 when next run.
Related (but that one was a pop con so not a duplicate) – trichoplax – 2016-08-30T01:09:27.777
1In Excel it's just 5 bytes by setting maximum iterations in Enable Iterative Calculation to 1 and then just enter
= A1 + 1
in cell A1. – Anastasiya-Romanova 秀 – 2016-08-30T02:43:05.250@matsjoyce No. Did you try the example? It does work beyond 9. – Calvin's Hobbies – 2016-08-30T07:13:47.147
@Anastasiya-Romanova秀, the spec specifically requires you to modify the source file. There are probably relatively few languages which can do this running under Windows, because of its tendency to lock files. – Peter Taylor – 2016-08-30T07:35:22.677
3Helka, the spec also says "*modifies its own source file such that the next time it is run*" (my emphasis). If your intention is that only interpreted languages may be used, you should state that explicitly. If you intend to allow compiled languages as well, the wording is a bit unclear. – Peter Taylor – 2016-08-30T07:38:26.847
@HelkaHomba Sorry, missed the f.read(). Too early it seems... – matsjoyce – 2016-08-30T07:50:44.397
1@PeterTaylor That's why I didn't post my answer :D – Anastasiya-Romanova 秀 – 2016-08-30T07:54:16.347
Can the program receive as input it's own filename? (another way of hardcoding it). I ask because I want to solve it in sed and it absolutely requires an input to start. I will count the name in the total bytes ofcourse. – seshoumara – 2016-08-30T12:42:21.530
I don't see how this is not a duplicate of the challenge that @manatwork linked. – AdmBorkBork – 2016-08-30T12:51:24.037
@TimmyD It sadly definitely is. In my defense there were two conversations about this possibly being a dupe in chat and that challenge wasn't mentioned.
– Calvin's Hobbies – 2016-08-30T14:10:33.147