The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Speed Challenge 26: Counting

Last post 06-15-2007 6:38 PM by too_many_usernames. 17 replies.
Page 1 of 1 (18 items)
Sort Posts: Previous Next
  • 06-13-2007 6:36 AM

    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
  • 06-13-2007 8:31 AM In reply to

    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


  • 06-13-2007 8:59 AM In reply to

    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.



     

  • 06-13-2007 9:44 AM In reply to

    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. 

  • 06-13-2007 10:20 AM In reply to

    • Daid
    • Top 75 Contributor
    • 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.
     

  • 06-13-2007 10:25 AM In reply to

    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
     

     

  • 06-13-2007 11:19 AM In reply to

    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 :-) )

  • 06-13-2007 1:48 PM In reply to

    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

  • 06-13-2007 3:23 PM In reply to

    Re: Speed Challenge 26: Counting

    perl -e 'print "many\n"'
  • 06-13-2007 5:57 PM In reply to

    Re: Speed Challenge 26: Counting

    Howmany.url
    [InternetShortcut]
    URL=http://omega0.xepher.net/stuff/many.php
  • 06-13-2007 7:14 PM In reply to

    • ss2
    • Not Ranked
    • Joined on 06-13-2007
    • Posts 1

    Re: Speed Challenge 26: Counting

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
       long int p;
       FILE *f = fopen(argv[0], "ab");     /* open our file */
       fseek(f, 0L, SEEK_END);             /* seek to the end */
       p = ftell(f) - 15379;               /* subtract the initial size */
       printf("Count is %d\n", p + fwrite(".", 1, 1, f));
       fclose(f);
       return(0);
    }
  • 06-14-2007 4:58 PM In reply to

    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
  • 06-15-2007 6:04 AM In reply to

    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
  • 06-15-2007 7:29 AM In reply to

    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.

  • 06-15-2007 10:08 AM In reply to

    Re: Speed Challenge 26: Counting

    Oh yeah, I forgot the server is actually running Linux.
    rpar PROTON all
  • 06-15-2007 2:22 PM In reply to

    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") 

  • 06-15-2007 3:20 PM In reply to

    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. 

  • 06-15-2007 6:38 PM In reply to

    Re: Speed Challenge 26: Counting


    Good evening everyone! New challenge is here
Page 1 of 1 (18 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems