# Playing Pickomino

In the game Pickomino, there are several tiles lying in the middle of the table, each with a different positive integer on them. Each turn, the players roll dices in a certain way and get a score, which is a nonnegative integer.

Now the player takes the tile with the highest number that is still lower or equal to their score, removing the tile from the middle and adding it to their stack. If this is not possible because all numbers in the middle are higher than the player's score, the player loses the topmost tile from their stack (which was added latest), which is returned to the middle. If the player has no tiles left, nothing happens.

## The challenge

Simulate a player playing the game against themselves. You get a list of the tiles in the middle and a list of the scores that the player got. Return a list of the tiles of the player after all turns have been evaluated.

## Challenge rules

• You can assume that the list with the tiles is ordered and doesn't contain any integer twice.
• You can take both lists of input in any order you want
• The output has to keep the order of the tiles on the stack, but you can decide whether the list is sorted from top to bottom or from bottom to top.

## General rules

• This is , so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
• Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs.
• Default Loopholes are forbidden.

## Example

(taken from the 6th testcase)

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: [22, 22, 22, 23, 21, 24, 0, 22]

First score is 22, so take the highest tile in the middle <= 22, which is 22 itself.

Middle: [21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Stack: [22]
Remaining scores: [22, 22, 23, 21, 24, 0, 22]

Next score is 22, so take the highest tile in the middle <= 22. Because 22 is already taken, the player has to take 21.

Middle: [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Stack: [22, 21]
Remaining scores: [22, 23, 21, 24, 0, 22]

Next score is 22, but all numbers <= 22 are already taken. Therefore, the player loses the topmost tile on the stack (21), which is returned into the middle.

Middle: [21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Stack: [22]
Remaining scores: [23, 21, 24, 0, 22]

Next scores are 23, 21 and 24, so the player takes these tiles from the middle.

Middle: [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Stack: [22, 23, 21, 24]
Remaining scores: [0, 22]

The player busts and scores zero. Therefore, the tile with the number 24 (topmost on the stack) is returned into the middle.

Middle: [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Stack: [22, 23, 21]
Remaining scores: [22]

The last score is 22, but all tiles <= 22 are already taken, so the player loses the topmost tile on the stack (21).

Middle: [21, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Final Stack and Output: [22, 23]

## Test cases

(with the topmost tile last in the output list)

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: [26, 30, 21]
Output: [26, 30, 21]

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: [35, 35, 36, 36]
Output: [35, 34, 36, 33]

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: [22, 17, 23, 19, 23]
Output: [23]

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: []
Output: []

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: [22, 17, 23, 19, 23, 0]
Output: []

Tiles: [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
Scores: [22, 22, 22, 23, 21, 24, 0, 22]
Output: [22, 23]

Tiles: [1, 5, 9, 13, 17, 21, 26]
Scores: [6, 10, 23, 23, 23, 1, 0, 15]
Output: [5, 9, 21, 17, 13, 1]

Tiles: []
Scores: [4, 6, 1, 6]
Output: []

Sandbox

• Can we assume there are no tiles with a value of zero in the middle? Commented Feb 6, 2019 at 23:06
• @EmbodimentofIgnorance It says "positive integer", so yes. Commented Feb 6, 2019 at 23:10
• Since the tiles are unique, would it be acceptable to take them as a bitmask? Commented Feb 7, 2019 at 0:10
• @TRITICIMAGVS Yes, if the middle pile is empty, the player cannot take a tile from the middle, so they lose a tile (if they have one) Commented Feb 7, 2019 at 9:13
• @Arnauld That is acceptable Commented Feb 7, 2019 at 9:13

1 byte saved thanks to Ørjan Johansen

(#)=span.(<)
(a%(b:c))d|(g,e:h)<-b#d=(e:a)%c$g++h|g:h<-a,(i,j)<-g#d=h%c$i++g:j|1>0=a%c$d (a%b)c=a ([]%) Try it online! Assumes the tiles are sorted in descending order. Not much fancy going on here. The first argument is the players pile, the second their scores and the third is the pile in the middle. • This cannot be right because sort is ascending. The TIO test case never hits that branch, though. I strongly recommend testing all cases every time when iterating like this. Commented Feb 6, 2019 at 23:21 • @ØrjanJohansen Thanks! Fixed now. At least I don't have to import any longer! Commented Feb 6, 2019 at 23:27 • Save a byte with (#)=span.(<). Commented Feb 6, 2019 at 23:49 • @ØrjanJohansen Change made. Funny thing is I tried that earlier and thought that it added a byte. Commented Feb 7, 2019 at 0:02 # Japt, 24 bytes Oof! That didn't work out as well as I thought it would! Takes input in reverse order. ®=Va§Z)Ì?NpVjZ:VpNo)nÃN¤ ®=Va§Z)Ì?NpVjZ:VpNo)nÃN¤ :Implicit input of N=[U=scores, V=tiles] ® :Map each Z in U = : Reassign to Z Va : 0-based index of last element in V (-1 if not found) §Z : Less than or equal to Z ) : End reassignment Ì : Sign of difference with -1 (1 if found, 0 if not) ? : If truthy (not zero) Np : Push to N VjZ : Remove and return the element at index Z in V : : Else Vp : Push to V No : Pop the last element of N ) : End Push n : Sort V Ã :End map N¤ :Slice the first 2 elements (the original inputs) off N # Perl 6, 89 bytes {my@x;@^a;@^b>>.&{@x=|@x,|(keys(@a∖@x).grep($_>=*).sort(-*)[0]//(try [email protected]&&()))};@x}

Try it online!

I think there's a few more bytes to be golfed off this...

# C# (Visual C# Interactive Compiler), 159158 154 bytes

Called as f(tiles)(scores)

If only System.Void is actually a return type and not just a placeholder for reflection. I would be able to replace if(a<1)m.Add(s.Count<1?0:s.Pop());else s.Push(a); with var t=a>1?m.Add(s.Count<1?0:s.Pop()):s.Push(a);, saving two bytes.

Try it online!

//Function taking in a list and returning
//another function that takes in another list and returns a stack
n=>m=>{
//Initialize the stack
var s=new Stack<int>();
//Add a zero to the tiles, to ensure no exceptions appear due to accessing
//non-existent elements in an empty collection later
//when we try to filter it later and getting the biggest element
//Iterate through our scores
n.ForEach(k=>{
//Create a variable called a, which we will use later
var a=
//Get all the elements in the middle that haven't appeared in our stack
m.Except(s).
//And throw away all elements that are bigger than our current score
Where(x=>x<=k).
//And get the biggest element there, and that is now the value of a
//Without the m.Add(0), we would get an exception here
Max();
//Self-explanatory, if a is less than 1 aka if a equals 0
//Checks if all elements in the middle are bigger than our score
//Except for our self added 0, of course
if(a<1)
//Add 0 to the middle if the stack is empty
//Remember, zeros don't affect the list
//Else pop the stack and add that to the middle
s.Pop());
//If a isn't 0, add a to the stack
else s.Push(a);});
//Afterwards, return the stack
return s;}

# Ruby, 77 bytes

->t,s,r=[]{s.map{|i|(x=(t-[p]).reject{|j|j>i}.max)?(t-=[x];r<<x):t<<r.pop};r}

Try it online!

# JavaScript (Node.js), 80 bytes

Same logic as the ES6 version, but takes the tiles as a BigInt bitmask and the scores as an array of BigInts.

m=>s=>s.map(g=x=>!x||m>>x&1n?m^=1n<<(x?r.push(x)&&x:r.pop()||~x):g(--x),r=[])&&r

Try it online!

# JavaScript (ES6),  100 98 94  87 bytes

Takes input as (tiles)(scores). The tiles can be passed in any order.

t=>s=>s.map(g=x=>m[x]?m[x?r.push(x)&&x:r.pop()]^=1:g(x-1),t.map(x=>m[x]=1,m=[r=[]]))&&r

Try it online!

### Commented

t => s =>                 // t[] = tiles; s[] = scores
s.map(g = x =>          // for each score x in s[]:
m[x] ?                //   if m[x] is set:
m[                  //     update the 'middle':
x ?               //       if x is not equal to 0:
r.push(x) && x  //         push x in the stack r[] and yield x
:                 //       else:
r.pop()         //         pop the last value from the stack
//         (may be undefined if the stack is empty)
] ^= 1              //     toggle the corresponding flag in m[]
:                     //   else:
g(x - 1),           //     try again with x - 1
t.map(x =>            //   initialization of the 'middle': for each value x in t[]:
m[x] = 1,           //     set m[x]
m = [r = []]        //     the stack r[] is stored as the first entry of m[],
//     which ensures that g will always stop when x = 0
)                     //   end of initialization
) && r                  // end of main loop; return r[]

# Charcoal, 35 bytes

Ｆη«≔⌈Φθ¬›κιι¿ι«≔Φθ⁻κιθ⊞υι»¿υ⊞θ⊟υ»Ｉυ

Try it online! Link is to verbose version of code. Explanation:

Ｆη«

Loop over the scores.

≔⌈Φθ¬›κιι

Look for the highest available tile.

¿ι«

If it exists then...

≔Φθ⁻κιθ

... remove the tile from the middle...

⊞υι

... and add it to the stack.

»¿υ

Otherwise, if the stack is not empty...

⊞θ⊟υ

Remove the latest tile from the stack and return it to the middle.

»Ｉυ

Print the resulting stack from oldest to newest.

# Python 2, 120 bytes

m,s=input()
t=[]
for n in s:
i=[v for v in m if v<=n]
if i:v=max(i);t+=[v];m.remove(v)
else:m+=t and[t.pop()]
print t

Try it online!

# 05AB1E, 27 22 bytes

Explanation:

v            # Loop y over the (implicit) input-list of scores:
Ð           #  Triplicate the tiles list (takes it as implicit input in the first iteration)
y>‹        #  Check for each if y <= the value in the tiles list
Ï       #  Only leave the values at the truthy indices
D           #  Duplicate the remaining tiles
¯Êi        #  If this list is not empty:
à       #   Pop the list, and push its maximum
©      #   Store it in the register, without popping
K     #   Remove it from the tiles list
®    #   Push the maximum again
s   #   Swap the maximum and tiles-list on the stack
ë        #  Else:
\       #   Remove the duplicated empty tiles-list from the stack
sª     #   Add the last tile to the tiles-list
]            # Close the if-else and loop
)           # Wrap everything on the stack into a list
¨          # Remove the last item (the tiles-list)
# (and output the result implicitly)

# Pyth, 32 bytes

VE ?JeS+0f!>TNQ=-QeaYJaQ.)|Y]0;Y

Try it online here, or verify all the test cases at once here.

There must be room for improvement here somewhere - any suggestions would be much appreciated!

VE ?JeS+0f!>TNQ=-QeaYJaQ.)|Y]0;Y   Implicit: Q=input 1 (middle), E=input 2 (scores), Y=[]
VE                            ;    For each score, as N, in the second input:
f    Q                      Filter Q, keeping elements T where:
!>TN                         T is not greater than N
(less than or equal is the only standard inequality without a token in Pyth, grrr)
+0                            Prepend 0 to the filtered list
eS                              Take the largest of the above (_e_nd of _S_orted list)
J                                Store the above in J
?                                 If the above is truthy:
aYJ                 Append J to Y
e                    Take last element of Y (i.e. J)
=-Q                     Remove that element from Q and assign the result back to Q
Else:
|Y]0         Yield Y, or [0] if Y is empty
.)             Pop the last element from the above (mutates Y)
aQ               Append the popped value to Q
Y   Print Y

# Perl 5 -apl -MList:Util=max, 97 bytes

$_=$".<>;for$i(@F){(($m=max grep$_<=$i,/\d+/g)&&s/ $m\b//?$s:$s=~s/ \d+$//?$_:$G).=$&}$_=$s;s/ // TIO reads scores and tiles on next line and prints output. How • -apl : -p to loop over lines and print, -a autosplit, -l to chomp from input and add newline character to output •$_=$".<> : to read next line (tiles) and prepend a space into default var$_
• for$i(@F){ ... } loop$i over @F fields of current line (scores)
• ( .. ? .. : .. ).=$& append previous match to ternary l-value • ($m=max grep$_<=$i,/\d+/g)&&s/ $m\b//?$s in case max value found and removed from tiles ($_) l-value is scores ($s)
• $s=~s/ \d+$//?$_ otherwise if last number could be removed from scores it's tiles • :$G finally it's garbage because can't occur
• $_=$s;s/ // to set scores to default var, and remove leading space