Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Golfing Colors

by iamcal (Friar)
on Jun 01, 2001 at 19:16 UTC ( [id://84951]=perlmeditation: print w/replies, xml ) Need Help??

For something i was working on, i needed to print all of the non-dithering web colors. These colors are 6 digit hex strings, prepended with a '#'. each pair of hex digits can either be '00','33','66','99','cc' or 'ff'. This makes 216 combinations (the 'web-safe' palette). First i came up with: my@a=map{$_ x2}split//,'0369cf';my(@b,@c);map{push@b,$_;map{push@c,$_;map{print"#$b[$#b]$c[$#b]$_\n";}@a}@a}@a; at 111 bytes. not too good. so i took another approach and shortened it to 82 bytes: my@a=split//,'0369cf';for my$a(@a){for my$b(@a){for(@a){print"#$a$a$b$b$_$_\n";}}} but this is still pretty big. can anyone do better?

update: and obviously you have to comply with strict and -w.

Replies are listed 'Best First'.
Re: Golfing Colors
by MeowChow (Vicar) on Jun 01, 2001 at 21:52 UTC
    I was inspired to take a rather unique approach. Not really golfing, but:
    $_ = "0369cf" x 3; print "#$1$1$2$2$3$3\n" while /.*(.).*(.).*(.)(?(?{$h{"$1$2$3"}++})^)/ +;
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Golfing Colors
by japhy (Canon) on Jun 01, 2001 at 19:35 UTC
    There's some useless doubling happening there. And if you're using $a and $b, they're ok by strict.
    # 65 chars sub colors { for$a(@_='00336699ccff'=~/../g){for$b(@_){print"#$a$b$_\n"for@_}} }


    japhy -- Perl and Regex Hacker
      64 chars:
      sub colors { for$a(@_='0369cf'=~/./g){for$b(@_){print"#$a$a$b$b$_$_\n"for@_}} }
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        And, replacing $a with $&, 60 chars:
        sub colors { map{for$b(@_){print"#$&$&$b$b$_$_\n"for@_}}@_="0369cf"=~/./g }
        Update: WRONG, as chipmunk pointed out immediately. (I did worse than not test, I ran it and failed to look closely at the output!)
          p

Re: Golfing Colors
by MeowChow (Vicar) on Jun 01, 2001 at 19:42 UTC
    The quintessence of this problem has been solved in (Golf) Ordered Combinations:
    # tilly's combinator sub c { @r='';@r=map{$c=$_;map$c.$_,@r}@_ for 1..shift;@r } print join $/, map "#$_", c qw(3 00 33 66 99 CC FF);
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Golfing Colors
by jmcnamara (Monsignor) on Jun 01, 2001 at 20:20 UTC

    This isn't really a problem of combinations, it's a printf problem. ;-)

    The direct approach works just as well:
    my $h = 0x33; for my $i (0..5) { for my $j (0..5) { for my $k (0..5) { printf"#%02x%02x%02x\n", $i*$h, $j*$h, $k*$h; } } }
    And the best I can golf that to is 77 chars: for $a(0..5){for $b(0..5){printf"#%02x%02x%02x\n",$a*51,$b*51,$_*51for 0..5}} As an aside, the common abuse of $a and $b in Golf isn't really in the spirit of strict even if it does give strict compliance.

    John.
    --

      75 if you drop those two spaces: for$a(0..5){for$b(0..5){printf"#%02x%02x%02x\n",$a*51,$b*51,$_*51for 0..5}}
        "As an aside, the common abuse of $a and $b in Golf isn't really in the spirit of strict even if it does
          give strict compliance."


      That's golf, brother. (Just picture a cloister full of monks quietly going about their business and all pausing to stick their tongues out at you.)

        p

Re: Golfing Colors -- direct, strict+w, no temps
by Zaxo (Archbishop) on Jun 02, 2001 at 04:19 UTC
    69 strokes, no $a,$b abuse.
    map{printf'#%02x%02x%02x ',$_%6*51,($_/=6)%6*51,($_/6|0)*51}(0..215);

    After Compline
    Zaxo

    Update:Doh, forgot to remove unneeded list parens. 67 strokes.

    map{printf'#%02x%02x%02x ',$_%6*51,($_/=6)%6*51,($_/6|0)*51}0..215;

    --Z
Re: Golfing Colors
by Masem (Monsignor) on Jun 01, 2001 at 19:50 UTC
    You can drop a few characters from
    my@a=split//,'0369cf';
    to
    my@a='0369cf'=~/./g;
    Shaves off two characters...
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://84951]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found