Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Dungeons and Dragons die roller (Golf)

by zsl (Novice)
on Sep 30, 2016 at 20:18 UTC ( #1173026=obfuscated: print w/replies, xml ) Need Help??

60 byte solution, not counting hashbang, to take strings that look like '2d4' or '2d4+10' and give you the result:

-n ($d,$s,$=)=split/d|\+/;$=+=1+rand $s for 1..$d;print$=,$/

Anyone wants to try and beat my record?

$thanks++

Replies are listed 'Best First'.
Re: Dungeons and Dragons die roller (Golf)
by choroba (Chancellor) on Sep 30, 2016 at 22:41 UTC
    Without really thinking:
    -p ($d,$s,$=)=split/[d+]/;map$=+=1+rand$s,1..$d;$_=$=.$/ 1 2 3 4 5 12345678901234567890123456789012345678901234567890123

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Dungeons and Dragons die roller (Golf)
by Grimy (Pilgrim) on Oct 25, 2016 at 16:06 UTC
    Straightforward 39 bytes (37 + 2 flags) solution :
    #!perl -pl /\d+\+/;$-+=rand$&for 1..$_;$_+=$'+$-
    More complex 35 bytes solution :
    #!perl -pl s/d/q(+~~rand($')+d)x$`/e;$_=eval
    Due to the use of $', they can’t easily be ran from the command-line. Just store them in a file.
      After staring at this for a while, I'm not sure how exactly this works (mainly, what does +~~ do?). Would it be possible to explain how the 35 bytes solution works?

        ~~rand($') is the same as int(rand($'))

Re: Dungeons and Dragons die roller (Golf)
by stevieb (Abbot) on Sep 30, 2016 at 20:30 UTC

    That produces the same result every 'roll' it seems...

      That's strange, I tested it out and it worked fine. I'm using v5.18.2, what are you using?

      $thanks++
Re: Dungeons and Dragons die roller (Golf)
by Tanktalus (Canon) on Oct 24, 2016 at 17:25 UTC

    To steal choroba's post:

    echo 2d12+13 | \ perl -ple ' ($d,$s,$_)=split/[d+]/;map$_+=1+rand$s,1..$d ' 1 2 3 4 12345678901234567890123456789012345678901234

      This always prints 13, which is not a possible result for 2d12+13.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://1173026]
Approved by stevieb
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-08-19 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (312 votes). Check out past polls.

    Notices?