Minecraft inventory management is hard. You have 17 diamonds, but you need 7 to craft an enchantment table, a pickaxe, and a sword. Do you pick them up and right click 7 times? Or do you right click once and right click twice and take the 7 left? It's so confusing!
for those of you who are now confused, don't worry, I'll explain it all in a sec
Challenge
Given the size of a stack of items and a desired amount, determine the least number of clicks to get that amount. You only need to handle up to 64 for both inputs and you may assume you have infinite inventory slots. You cannot use the drag-to-distribute trick.
Definitions
The inventory is a collection of slots where you can store items.
A slot is a storage space in your inventory where you can place up to one type of item.
A stack is a number of items placed in the same group. For the purposes of this challenge, a stack is simply a bunch of items in the same place (so ignore stack-size)
The cursor is your pointy thingy. That cursor. It can have items "on it"; in other terms, if you clicked on a slot and picked up items, the items you picked up are "on the cursor" until you put them down.
Specifications
There are four possible situations. Either you have an item on your cursor or you don't, and either you left click or you right click.
If you don't have an item on your cursor and you left-click on a slot, you pick up the entire stack.
If you don't have an item on your cursor and you right-click on a slot, you pick up half the stack, rounded up.
If you have an item on your cursor and you left-click on a slot, you place all of the items into that slot. (For all you Minecraft players, you won't have >64 items for this challenge and they're all 64-stackable, and you only have one type so the item swap does not apply here)
If you have an item on your cursor and you right-click on a slot, you place one item into that slot.
So, you start with all of the items given (first input, or second; you may choose the order) in a slot, and you want to finish with having the desired amount (other input) in your cursor.
Let's run through an example. Say you start with 17 items and you want 7. First, you right-click on the stack, which means you have picked up 9 and there are 8 in that slot. Then, if you right-click on the stack again, you place one item back into the slot, leaving you with 8 and the slot with 9. Finally, you right-click again and you have 7 and the slot has 10. Thus, you would return 3
(the number of clicks).
If you manage to out-click-golf me, please tell me and I will edit the example :P
Test Cases
These are manually generated, so please tell me if there are any errors. I do inventory management through jitter-clicking right click so I don't have experience with optimal inventory management :P
Given, Desired -> Output
17, 7 -> 3
64, 8 -> 5
63, 8 -> 5
10, 10 -> 1
10, 0 -> 0 # note this case
25, 17 -> 7
Explanations
This challenge might be tricky for non-Minecraft players, I have no idea. Here are some explanations.
64, 8 -> 5
because you pick up 32 using right click, place it down, pick up 16, place it down, then pick up 8.
63, 8 -> 5
for the same reason.
25, 17 -> 7
because you pick up 13, place it down, pick up 6 from the leftover 12, place 2 back into the leftover stack, and then place the 4 in the cursor into the 13, and then pick those up.
Rules
- Standard loopholes apply
- You may assume that
0 <= desired <= given <= 64
- You may take input in either order and do I/O in any reasonable format
0,[n]
, may transition: (1) from0,[a,b,...]
toa,[b,...]
,b,[a,...]
,ceil(a/2),[floor(a/2),b,...]
, orceil(b/2),[a,floor(b/2),...]
; or (2) fromx,[a,b,...]
(x>0
) tox-1,[a+1,b,...]
,x-1,[a,b+1,...]
,x-1,[a,b,...,1]
,0,[a+x,b,...]
,0,[a,b+x,...]
,0,[a,b,...,x]
. The challenge is then to find the minimum possible transitions from0,[g]
where g is the given tot,L
wheret
is the desired target andL
is any list? \$\endgroup\$