Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Code Golf: Four is magic

by duelafn (Priest)
on Jul 13, 2010 at 03:45 UTC ( #849154=note: print w/ replies, xml ) Need Help??


in reply to Code Golf: Four is magic

Well, I'm not much of a golfer, but here is a proof of concept (at a whopping 258 strokes) that goes up to 999 nonillion... using spelling in Lingua::EN::Numbers (limiting to 0..99 is too boring)

# Update: 222 strokes by using a bareword and (for the fun of it) supp +ort up to undecillion 10^36 (costs no extra chars due to bareword tri +ck) for f in 0 4 12 99 1024 1000024 999999999999999999999999999999999; do +echo $f | \ perl -E'@x=a33544355436688779880066555766=~/./g;$_=<>;chop;while(($a=$ +_)-4){split//,b999aabb87780;$x=4*!$_;$x+=hex(pop@_)*!!-$&+!!$1*($x[$1 +]+7)+$x[$2+20]+$x[$3]while s/(.??)([^1]?)(1?.)$//;say"$a is $x.";$_=$ +x}say"4 is magic."' ; \ echo; done # Update: 224 strokes by including the suggested =~/./g shortcut and m +oving output around a bit perl -E'@x="033544355436688779880066555766"=~/./g;$_=<>;chop;while(($a +=$_)-4){split//,"99aabb87780";$x=4*!$_;$x+=hex(pop@_)*!!-$&+!!$1*($x[ +$1]+7)+$x[$2+20]+$x[$3]while s/(.??)([^1]?)(1?.)$//;say"$a is $x.";$_ +=$x}say"4 is magic."' # Update: 229 strokes by moving some conditionals into the regex perl -E'@x=split//,"033544355436688779880066555766";$_=<>;chop;print;w +hile($_-4){split//,"99aabb87780";$x=4*!$_;$x+=hex(pop@_)*!!-$&+!!$1*( +$x[$1]+7)+$x[$2+20]+$x[$3]while s/(.??)([^1]?)(1?.)$//;say" is $x.";p +rint$_=$x}say" is magic."' # Update: Ah, ha! 239 strokes: !!-$& will do it perl -E'@x=split//,"0335443554366887798866555766";$_=<>;chop;print;whi +le($_-4){split//,"99aabb87780";$x=4*!$_;$x+=hex(pop@_)*!!-$&+!!$1*($x +[$1]+7)+($2>1?$x[$2+18]+$x[$3]:$x[$2.$3])while s/(.??)(.?)(.)$//;say" + is $x.";print$_=$x}say" is magic."' # Update: 242 strokes, and works :), stealing some ideas from haarg & +thundergnat perl -E'@x=split//,"0335443554366887798866555766";$_=<>;chop;print;whi +le($_-4){split//,"99aabb87780";$x=4*!$_;$x+=hex(pop@_)*!!(0+$&)+!!$1* +($x[$1]+7)+($2>1?$x[$2+18]+$x[$3]:$x[$2.$3])while s/(.??)(.?)(.)$//;s +ay" is $x.";print$_=$x}say" is magic."' # Update: Down to 258 strokes perl -E'@x=split//,"0335443554366887798866555766";$_=<>;chop;print;whi +le($_!=4){split//,"99aabb87780";$_?do{$x=0;while(s/(.??)(.?)(.)$//){$ +x+=hex(pop@_)*(0+$&?1:0)+($1?$x[$1]+7:0)+($2>1?$x[$2+18]+$x[$3]:$x[$2 +.$3])}}:($x=4);say" is $x.";print$_=$x}say" is magic."' # 261 strokes: perl -E'@x=split//,"0335443554366887798866555766";$_=<>;chop;print;whi +le($_!=4){@m=split//,"99aabb87780";$_?do{$x=0;while(s/(.??)(.?)(.)$// +){$x+=hex(pop@m)*(0+$&?1:0)+($1?$x[$1]+7:0)+($2>1?$x[$2+18]+$x[$3]:$x +[$2.$3])}}:($x=4);say" is $x.";print$_=$x}say" is magic."' 999999999999999999999999999999999 is 321. 321 is 21. 21 is 9. 9 is 4. 4 is magic.

Good Day,
    Dean


Comment on Re: Code Golf: Four is magic
Download Code
Re^2: Code Golf: Four is magic
by Haarg (Chaplain) on Jul 13, 2010 at 17:25 UTC
    A couple small improvements:
    perl -E'@x=split//,"0335443554366887798866555766";$_+=<>;print;while($ +_!=4){split//,"99aabb87780";$_?do{$x=0;$x+=hex(pop)*!!$&+($1&&$x[$1]+ +7)+($2>1?$x[$2+18]+$x[$3]:$x[$2.$3])while s/(.??)(.?)(.)$//}:($x=4);s +ay" is $x.";print$_=$x}say" is magic."'

      That actually has problems with large numbers (> native integer ?) Once it gets over 64 bits on my machine it converts to scientific notation... which throws off the conversion.

      Never mind. Broken

      Here's my whack at it - 232 strokes

      perl -E'@x=split//,"4335443554366887798866555766";$_=<>;chop;print;whi +le($_-4){split//,"99aabb87780";$x=0;$x+=hex(pop)*!!$&+($1&&$x[$1]+7)+ +($2>1?$x[$2+18]+$x[$3]:$x[$2.$3])while s/(.??)(.?)(.)$//;say" is $x." +;print$_=$x}say" is magic."' __END__ 999999999999999999999999999999999 999999999999999999999999999999999 is 231. 231 is 19. 19 is 8. 8 is 5. 5 is 4. 4 is magic.

        Doesn't seem to work right -- ($1&&$x[$1]+7) adds 7 too often and !!$& has issues with "000".

        # 123456789_123456789_1 # onemilliontwentyfour # onethousandtwentyfour echo 1000024 | perl duelafn.pl 1000024 is 20. 20 is 6. 6 is 3. 3 is 5. 5 is 4. 4 is magic. echo 1024 | perl duelafn.pl 1024 is 21. 21 is 9. 9 is 4. 4 is magic. echo 1000024 | haarg.pl 1000024 is 13. 13 is 8. 8 is 5. 5 is 4. 4 is magic. echo 1024 | haarg.pl 1024 is 13. 13 is 8. 8 is 5. 5 is 4. 4 is magic. echo 1000024 | perl thundergnat.pl 1000024 is 17. 17 is 9. 9 is 4. 4 is magic. echo 1024 | perl thundergnat.pl 1024 is 13. 13 is 8. 8 is 5. 5 is 4. 4 is magic.

        Update tracked doiwn more details of the bugs.

        Good Day,
            Dean

Re^2: Code Golf: Four is magic
by thundergnat (Deacon) on Jul 14, 2010 at 17:27 UTC

    A modest improvement on your 229:
    227 strokes

    perl -E'@x="033544355436688779880066555766"=~/./g;$_=<>;chop;print;whi +le($_-4){split//,"99aabb87780";$x=4*!$_;$x+=hex(pop@_)*!!-$&+!!$1*($x +[$1]+7)+$x[$2+20]+$x[$3]while s/(.??)([^1]?)(1?.)$//;say" is $x.";pri +nt$_=$x}say" is magic."'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://849154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2015-07-03 21:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls