List Commands Library for 50g
08-06-2018, 05:59 PM (This post was last modified: 08-06-2018 06:04 PM by pier4r.)
Post: #377
 pier4r Senior Member Posts: 2,075 Joined: Nov 2014
RE: List Commands Library for 50g
I don't know if what I am going to post belongs here or in the little explorations (n1). I post it here to raise the chance to get even more better commands. Read it as "I want commands that think for you!".

Let's expose the problem. Last week I played a cooperative board game, massive darkness. The game is not bad, although it may be too in favor of the players after level 3. The dice of the game, though, are not the common d6. They have elements that cannot be added to the others, therefore I wanted to know their distribution.

For example pick the Yellow dice for attacks. It has:
Blank
1 Sword
1 Sword
1 Sword
1 Sword
2 Swords / 1 Bam
(n3)

Of course I said "I can do it! Pen and paper and the sharp el506w and a bit of theory will not fail me". Of course they did not fail me, I failed myself once I tried to compute the distribution for 3 dice. Until 2 dice I got it right.

As usual when I fail I resort to automation to save my situation. Good math people solve everything on their paper sheets, poor ones use their calculators. That's my motto (n2), but hey at least I get an answer!

Thinking a bit how could I model quantities that cannot be added together, I ended up with a list of lists. So a yellow dice is modeled as follows
Code:
   {     { 0 0 0 } @blank     { 1 0 0 } @1 sword     { 1 0 0 } @1 sword     { 1 0 0 } @1 sword     { 1 0 0 } @1 sword     { 2 1 0 } @2 swords 1 bam   }

I was relatively happy thinking I could roll the two dice, add the results, and then use the awesome LRPCT . LRPCT: it gets a list in input and the result are two sublists. The first with the elements of the list in input, without duplicates (well, it is not exactly that, see below), and the second list with the occurrences of the elements of the first list in the input list.

But then I discovered it didn't work as the list in input should be sorted first, otherwise LRPCT doesn't achieve what I want.

So I tried SORT or LSORT on the result list, result list being something like the list describing the yellow dice.
Well both failed me (I also had no big expectations being my case an edge case), as for example the sublist { 2 1 0 } was sorted before { 2 0 0 } and then again {2 1 0} was listed.

Therefore I couldn't use LRPCT. I was in total despair.

What I did was a workaround. First getting the elements of the result list, without duplicates (it works), and then counting the occurrences of each element with LCNT (it works).
Below there is the code. The interesting part is at the end.

What I ask is: Could we have a LRPCT that does first LDDUP and then LCNT , to avoid sorting problems in the input list?

If I would have used vectors instead of sublist, would it have worked? (I didn't test)
Ok I tested, vectors are unsortable. Another point for a LRPCT where a LDDUP is done first and then LCNT.

Code:
   gvDiceSize   6   gvYellowDice   {     { 0 0 0 } @blank     { 1 0 0 } @1 sword     { 1 0 0 } @1 sword     { 1 0 0 } @1 sword     { 1 0 0 } @1 sword     { 2 1 0 } @2 swords 1 bam   }   gp2YellowArg   \<<   \>>   gp2Yellow   @worked 2018-08-06   \<<     {}  "lvResultList"       DROP     {}  "lvResultListDDUP"   DROP          {}  "lvFirstDiceRoll"   DROP          \->     lvResultList     lvResultListDDUP          lvFirstDiceRoll          \<<       1 gvDiceSize       FOR lvPos1                gvYellowDice         lvPos1         GET 'lvFirstDiceRoll' STO                  1 gvDiceSize         FOR lvPos2           gvYellowDice           lvPos2           GET                      lvFirstDiceRoll           ADD @add the two lists                      lvResultList SWAP            LPSHR 'lvResultList' STO             @add the result as sublist to the result list.         NEXT       NEXT              lvResultList LDDUP @results without duplicates       1       \<<         DUP @duplicate L1         lvResultList SWAP @bring the single element on L1         LCNT @count the value         \->TAG @tag the element with its frequency        \>>       DOSUBS              @output:       @results without duplicates and their frequency in a list     \>>   \>>