Thread: KAMIL challenge

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    KAMIL challenge

    Woohoo!

    This is a concerning a recent code-lenght thread in C board. The original challenge is here: KAMIL

    I finally managed to squeeze it into the 256 bytes. This would be 240 bytes, if you removed the comments and whitespace (230 if you removed cin.get(); too. (Although the filename probably should come from command line?) I get 16 points!!!

    Anyone care to score more?

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    
    int main(){
        string s;
        string t="TDLF";
        ifstream f("test.txt");
        while(f>>s){
            int c=1;    
            for(size_t i=0;i<s.size();i++)
                c*=t.find(s[i])==t.npos?1:2;
    /*man I'm PROUD of the t.npos thing, 
    hopefully legal for all std::string implementations?*/
    
            cout<<c<<endl;
        }
        cin.get(); //could remove that for extra 10 bytes
    }
    The test file I used and the output:
    FILIPEK
    FATLEAD
    NORMAL
    WOW
    DELFI
    WOOHOO

    output: 4 16 2 1 8 1
    #Edit: found ways to get rid of a few more bytes.
    Last edited by anon; 10-17-2006 at 02:52 PM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    change

    string s;
    string t="TDLF";
    to
    string s, t="TDLF";
    to get some more bytes off.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Some other modifications which reduce size (I removed the t variable and the references to it):

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    
    int main(){
        string s;
        ifstream f("test.txt");
        while(f>>s){
            int c=1;    
            for(int i=0;i<4;i++)
                c*=t.find("TDLF"[i])==t.npos?1:2;
    /*man I'm PROUD of the t.npos thing, 
    hopefully legal for all std::string implementations?*/
    
            cout<<c<<endl;
        }
        cin.get(); //could remove that for extra 10 bytes
    }
    Of course hardcoding the length of the string is less than ideal, but in the case of maximum shortness, hardcoding the 4 helps.
    Last edited by Cat; 10-17-2006 at 04:32 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Thanks. I'm too used to declare "large" things like strings separately.

    Cat: would your code work (if you changed the remaining t for s)? There may be more than one T, D, L or F in the string.
    Last edited by anon; 10-17-2006 at 04:33 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Oh, doh, I think I mixed up s and t. Yeah my way doesn't work.
    Last edited by Cat; 10-17-2006 at 04:38 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Not familiar with strings, but can't you use

    Code:
    int c=1,i=4;
    while (i) c*=t.find("TDLF"[--i])==t.npos?1:2;
    (assuming it doesn't matter which way you iterate?)

    Edit: Well, same idea applied to original code?
    Last edited by robatino; 10-17-2006 at 04:48 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Since the length of a word is at most 20, i only needs 1 byte, so you can make it an int instead of a size_t, and define it in the same line as c:

    Code:
        int c=1,i=s.size();
        while (i) c*=t.find(s[--i])==t.npos?1:2;

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Requirements of the challenge say you read from the std input stream so you can lop off quite a few bytes with that file stuff.

    It also just so happens that now, fully qualifying cout, cin, string (change endl to "\n") to std namespace takes less code.

    You can also be ultra ghetto and change while to for and declare strings there.

    >> t.find("TDLF"[--i])

    Would have to be

    Code:
    t.find(string("TDLF")[--i])
    Which is bigger than just declaring s and t in one line.

    >> int c=1,i=s.size();

    Okay if declared as size_t, which is okay. Warning if i is signed.

    So far combined suggestions:

    Code:
    #include<iostream>
    #include<string>
    int main(){for(std::string s,t="TDLF";std::cin>>s;){size_t c=1,i=s.size();while(i) c*=t.find(s[--i])==t.npos?1:2;std::cout<<c<<"\n";}}
    172 bytes.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Again, not familiar with strings, but the value of c becomes 1 left-shifted by the total number of elements for which the condition "t.find(s[--i])==t.npos?" is false. Is there a short way of computing this number of elements (maybe using some library function)?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Dirty tricks:
    1) I don't think compiler warnings violate the conditions, as long as it compiles and runs properly. So I think it's okay to use int instead of size_t.
    2) The conditions don't appear to require a trailing newline, so you can shave another 6 bytes off.

    Edit: Although the maximum value of c is 2^20, so if an int was 16 bits it wouldn't be big enough. It looks like one has to make some hardware assumptions to get the absolute best result.

    Edit: Using int instead of size_t, there is no warning compiling with gcc. I think in its present form, there's nothing to trigger one since it's not comparing a signed and unsigned type anymore.
    Last edited by robatino; 10-17-2006 at 11:47 PM.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Mindless... just what I need.

    Shortest I got:
    Code:
    main(b,a){while(a=getchar())b=(a=='\n'?!!printf("%d\n",b):b<<!!strchr("DFLT",a));}
    One variable only (implicit stack):
    Code:
    f(x){return(x=getchar())=='\n'?1:f(0)<<!!strchr("DFLT",x);}main(){for(;;)printf("%d\n",f(0));}
    I could try find a terse algebraic/bitwise mapping from DFLT|* to 1|0 or 2|1, but I doubt there is one to be found.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 23
    Last Post: 04-01-2008, 07:46 PM
  2. A Challenge in C++
    By Brad0407 in forum C++ Programming
    Replies: 38
    Last Post: 07-18-2007, 12:56 PM
  3. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:56 AM
  4. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM
  5. Speed Challenge, my solution:
    By RoD in forum C++ Programming
    Replies: 11
    Last Post: 03-17-2003, 09:12 PM