|
Speed Challenge 26: Counting
-
06-13-2007 6:36 AM
|
|
-
Faxmachinen


- Joined on 03-19-2007
- Posts 199
|
Speed Challenge 26: Counting
Speed Challenge 26: Counting
Write a program that simply tells you how many times you've run it. It should at least work in Windows XP.
rpar PROTON all
|
|
-
-
too_many_usernames


- Joined on 12-09-2005
- (0,0,0) local
- Posts 213
|
Re: Speed Challenge 26: Counting
Hrm. As far as I know, this will work in any DOS based scripting system running under windows. I can't quite figure out how to do math in the file though. It is, however, limited by disk space, but it doesn't require any bizarre data files or other resources or anything like that.
selfcount.bat @echo this program was run one less time than shown below: @find /c "ebbeh" selfcount.bat @echo @rem ebbeh >> selfcount.bat
|
|
-
-
PuckeL


- Joined on 06-13-2007
- Posts 12
|
Re: Speed Challenge 26: Counting
I also used batch. This is my approach: @echo off echo Program run %num% times. echo set /a num=%num%+1>>%0 set /a num=1
It needs to be started with its full filename entered in Windows Console. After closing the Console and starting it again in a new one, %num% will be set to the correct value after first start. It is important, that there is always a new line at eof.
|
|
-
-
too_many_usernames


- Joined on 12-09-2005
- (0,0,0) local
- Posts 213
|
Re: Speed Challenge 26: Counting
PuckeL:I also used batch. This is my approach: @echo off echo Program run %num% times. echo set /a num=%num%+1>>%0 set /a num=1
It needs to be started with its full filename entered in Windows Console. After closing the Console and starting it again in a new one, %num% will be set to the correct value after first start. It is important, that there is always a new line at eof.
Ah, cool. I got errors trying to use a '+' inside, but I admit that I don't have that much knowledge of the batch commands. Cool.
|
|
-
-
Daid


- Joined on 01-30-2007
- Posts 348
|
Re: Speed Challenge 26: Counting
I tried to do this: #include<stdio.h> #include<stdlib.h>
int main(int argc, char** argv) { int Count = 0; FILE* f = fopen(argv[0], "rb"); fseek(f, 50 * 1024, SEEK_SET); fread(&Count, sizeof(int), 1, f); fclose(f);
printf("runned %i times\n", Count);
f = fopen(argv[0], "wb"); if (!f) { printf("open for writing failed.\n"); exit(-1); } fseek(f, 50 * 1024, SEEK_SET); fwrite(&Count, sizeof(int), 1, f); fclose(f); return 0; }
But no luck, atleast not with mingw. Opening for writhing always seems to fail. I know I did something simular in QBasic once, and there it worked ;) On linux it would be possible to delete yourself and then write a new version of yourself on your old spot. But I don't think that will work in windows.
|
|
-
-
PuckeL


- Joined on 06-13-2007
- Posts 12
|
Re: Speed Challenge 26: Counting
PHP: run.php <?php @$file = fopen("./run.php", 'r+') or die; fread($file, 158); $num = file("./run.php"); $num = $num[8]+1; fwrite($file, $num); fclose($file); ?> 0
|
|
-
-
too_many_usernames


- Joined on 12-09-2005
- (0,0,0) local
- Posts 213
|
Re: Speed Challenge 26: Counting
Daid:I tried to do this: ...
But no luck, atleast not with mingw. Opening for writhing always seems to fail. I know I did something simular in QBasic once, and there it worked ;) On linux it would be possible to delete yourself and then write a new version of yourself on your old spot. But I don't think that will work in windows.
Yeah; on DOS/Windows an executable that is running is "locked" and therefore cannot be modified (but a batch file isn't!). I don't know if there is a way using descriptors or something to be able to modify a running executable. In a way this is probably the *only* security feature on DOS/Windows that makes some sense - to prevent breaking already-loaded executables. (Except, of course, when that's exactly what you want to do...on UNIX, they trust you to do this if you want to :-) )
|
|
-
-
nerdydeeds


- Joined on 06-06-2007
- Posts 34
|
Re: Speed Challenge 26: Counting
PuckeL:
I also used batch. This is my approach:
@echo off echo Program run %num% times. echo set /a num=%num%+1>>%0 set /a num=1
It needs to be started with its full filename entered in Windows Console. After closing the Console and starting it again in a new one, %num% will be set to the correct value after first start. It is important, that there is always a new line at eof.
I think this is a slight improvement:
@echo off if "%num%"=="" set /a num=1 echo Program run %num% times. echo set /a num=%num%+1>>%0 set /a num=1
|
|
-
-
-
omega0


- Joined on 04-27-2007
- Posts 57
|
Re: Speed Challenge 26: Counting
Howmany.url
[InternetShortcut]
URL=http://omega0.xepher.net/stuff/many.php
|
|
-
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 199
|
Re: Speed Challenge 26: Counting
Sorry, don't have time to go through them all tonight. I'll see it done tomorrow.
rpar PROTON all
|
|
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 199
|
Re: Speed Challenge 26: Counting
too_many_usernames' entry is a straight forward approach, and works well.
I don't think PuckeL's first entry worked as it was supposed to, though. Restarting the shell restarted the count too. Here's what it looks like after a restart: @echo off
echo Program run %num% times.
echo set /a num=%num%+1>>%0
set /a num=+1
set /a num=1+1
set /a num=2+1
set /a num=3+1
set /a num=+1
set /a num=1+1
set /a num=2+1
set /a num=3+1
Did he perhaps mean to write num=%%num%%>>%0 instead? Well, now it counts triangular numbers.
Daid's entry doesn't work, as stated. However, it beats ss2's entry by not exploding.
PuckeL's second entry doesn't work propperly either. At least now I know it's not just my computer. Not only does it slap the 1s together the wrong way (1+1=11), but it doesn't do it more than 7 times either.
assufield implements an approximation of the Swedish counting algorithm in a nice one-liner. Unfortunately, his grasp of the Swedish language is not quite on par.
omega0 has a nice program, but as far as I can figure out, URL is not a programming language.
Therefore, too_many_usernames gets to do the next Speed Challenge. Congratulations!
rpar PROTON all
|
|
-
-
PuckeL


- Joined on 06-13-2007
- Posts 12
|
Re: Speed Challenge 26: Counting
Ok, ^^ My first solution doesn't really work as required. But I don't see anything wrong with my PHP solution. :D Saved in Windows ANSI format it works as supposed.
|
|
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 199
|
Re: Speed Challenge 26: Counting
Oh yeah, I forgot the server is actually running Linux.
rpar PROTON all
|
|
-
-
too_many_usernames


- Joined on 12-09-2005
- (0,0,0) local
- Posts 213
|
Re: Speed Challenge 26: Counting
*whew* I was at all-morning meeting, so I don't have a challenge ready yet. Unless there are terrible objections to a weekend challenge, I'll post one sometime this evening (GMT-5 time zone definition of "evening")
|
|
-
-
asuffield


- Joined on 05-31-2006
- Posts 2,137
|
Re: Speed Challenge 26: Counting
Faxmachinen:
assufield implements an approximation of the Swedish counting algorithm in a nice one-liner. Unfortunately, his grasp of the Swedish language is not quite on par.
It's a Zen solution: counting by not-counting.
|
|
-
-
too_many_usernames


- Joined on 12-09-2005
- (0,0,0) local
- Posts 213
|
Re: Speed Challenge 26: Counting
Good evening everyone! New challenge is here
|
|
Page 1 of 1 (18 items)
|
|
|