language-agnosticcode-golfrosetta-stone

Code Golf: Diamond Blackjack


The challenge

The shortest code by character count to output a best-case blackjack hand from the list of number-cards given.

Input is a list of numbers from 1 to 10 (inclusive) separated by space.

Output will be the best blackjack hand formed from that list of cards - the closest available combo to reach 21 by the sum of all card values without surpassing it. Cards can be removed but not added.

If a case where removing two or more cards in favor of one card to accomplish the same result (removing 5 or 4,1 to gain 21) removing the least cards is required. If the equal amount of cards is to be removed (removing 1,4 or 3,2) the group with the minimum of the minimum will be removed (in the previous example, 1,4 will be removed as min(min(3,2), min(1,4)) belongs to that pair). In the case of duplicate cards, the first encounter should be removed.

Output will be formed as diamond cards, with input order preserved:

 __________  __________ 
|          ||          |
|          || /\       |
|          || \/       |
|    /\    ||          |
|    \/    ||          |
|          ||       /\ |
|          ||       \/ |
|__________||__________|
 __________  __________ 
|          ||          |
| /\       || /\    /\ |
| \/       || \/    \/ |
|    /\    ||          |
|    \/    ||          |
|       /\ || /\    /\ |
|       \/ || \/    \/ |
|__________||__________|
 __________  __________ 
|          ||          |
| /\    /\ || /\    /\ |
| \/    \/ || \/    \/ |
|    /\    || /\    /\ |
|    \/    || \/    \/ |
| /\    /\ || /\    /\ |
| \/    \/ || \/    \/ |
|__________||__________|
 __________  __________ 
|          ||          |
| /\    /\ || /\    /\ |
| \/ /\ \/ || \/ /\ \/ |
| /\ \/ /\ || /\ \/ /\ |
| \/    \/ || \/ /\ \/ |
| /\    /\ || /\ \/ /\ |
| \/    \/ || \/    \/ |
|__________||__________|
 __________  __________ 
| /\    /\ || /\    /\ |
| \/    \/ || \/ /\ \/ |
| /\    /\ || /\ \/ /\ |
| \/ /\ \/ || \/    \/ |
| /\ \/ /\ || /\    /\ |
| \/    \/ || \/ /\ \/ |
| /\    /\ || /\ \/ /\ |
|_\/____\/_||_\/____\/_|

Test cases

Input:
    1 5 7 8
Output:
     __________  __________  __________  __________ 
    |          ||          ||          ||          |
    |          || /\    /\ || /\    /\ || /\    /\ |
    |          || \/    \/ || \/ /\ \/ || \/ /\ \/ |
    |    /\    ||    /\    || /\ \/ /\ || /\ \/ /\ |
    |    \/    ||    \/    || \/    \/ || \/ /\ \/ |
    |          || /\    /\ || /\    /\ || /\ \/ /\ |
    |          || \/    \/ || \/    \/ || \/    \/ |
    |__________||__________||__________||__________|

Input: 
    10 3 4 2 6
Output:
     __________  __________  __________  __________ 
    | /\    /\ ||          ||          ||          |
    | \/ /\ \/ || /\       || /\       || /\    /\ |
    | /\ \/ /\ || \/       || \/       || \/    \/ |
    | \/    \/ ||    /\    ||          || /\    /\ |
    | /\    /\ ||    \/    ||          || \/    \/ |
    | \/ /\ \/ ||       /\ ||       /\ || /\    /\ |
    | /\ \/ /\ ||       \/ ||       \/ || \/    \/ |
    |_\/____\/_||__________||__________||__________|

Input 
    5 10 5 2 3
Output:
     __________  __________  __________  __________ 
    | /\    /\ ||          ||          ||          |
    | \/ /\ \/ || /\    /\ || /\       || /\       |
    | /\ \/ /\ || \/    \/ || \/       || \/       |
    | \/    \/ ||    /\    ||          ||    /\    |
    | /\    /\ ||    \/    ||          ||    \/    |
    | \/ /\ \/ || /\    /\ ||       /\ ||       /\ |
    | /\ \/ /\ || \/    \/ ||       \/ ||       \/ |
    |_\/____\/_||__________||__________||__________|

Code count includes input/output (i.e full program).


Solution

  • Golfscript - 228 Characters

    ' ':x/{~}%:h;9,{:r;h,2\?,{:m;h,,{2\?m&},{h\=}%}%1>{:j[{+}*.22<\j,j$j]}$)\;{:c;[x]3,{4\?'  \//\__'2/[11'?'*8'@'*'bb@BBbbbbbQY@AAQQQYYbfH@H@HbffYQD@D@DQQYfb@``bbbbfQY@PPQQQQQbf'8'?'*]']]'+10r*c+=@/3&=}%[x]++' _'1/7r&!=*}%'|':x;n}%
    
    $ echo 10 9 8 7 6 5 | ../golfscript.rb black.gs
     __________  __________  __________ 
    |          ||          ||          |
    | /\    /\ || /\    /\ || /\    /\ |
    | \/ /\ \/ || \/ /\ \/ || \/    \/ |
    | /\ \/ /\ || /\ \/ /\ || /\    /\ |
    | \/ /\ \/ || \/    \/ || \/    \/ |
    | /\ \/ /\ || /\    /\ || /\    /\ |
    | \/    \/ || \/    \/ || \/    \/ |
    |__________||__________||__________|
    

    231 Characters

    ' ':x/{~}%:h,2\?,{:m;h,,{2\?m&},{h\=}%}%1>{:j[{+}*.22<\j,j$j]}$-1=:h;9,{:r;h{:c;[x]3,{4\?'  \//\__'2/11'?'*8'@'*'bb@BBbbbbbQY@AAQQQYYbfH@H@HbffYQD@D@DQQYfb@``bbbbfQY@PPQQQQQbf'8'?'*']]'++++10r*c+=@/3&=}%[x]++' _'1/7r&!=*}%'|':x;n}%
    

    how it works

    # parse input into a list 
    ' ':x/{~}%:h
    # create the powerset
     ,2\?,{:m;h,,{2\?m&},
    # map the powerset onto the cards
    {h\=}%}%
    # trim the empty set from the powerset
    1>
    # sort the hands. most preferred hand will be last
    {:j[{+}*.22<\j,j$j]}$
    # take the preferred hand from the end of the list
    -1=:h;
    # for r in 0..8
    9,{:r
    ...more to follow