http://qs1969.pair.com?node_id=1174699


in reply to Dungeons and Dragons die roller (Golf)

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.

Replies are listed 'Best First'.
Re^2: Dungeons and Dragons die roller (Golf)
by Anonymous Monk on Nov 04, 2016 at 18:51 UTC
    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($'))

        I may be missing something very obvious, but could someone step through this one step at a time? The way I'm reading it, something like the below happens, and it doesn't make much sense to me.
        Input: 2d4+10 Code: s/d/q(+~~rand($')+d)x$`/e; 2d4+10 2(q(+~~rand($')+d)x$`)4+10 2(q(+int(rand($'))+d)x$`)4+10 2(q(+int(rand(4+10))+d)x2)4+10 2('+int(rand(4+10))+d'x2)4+10 2('+int(rand(4+10))+d''+int(rand(4+10))+d')4+10 =?????