# Build the smallest number

Given a non-empty list of digits 0 though 9, output the smallest number that can be produced by an expression formed by reordering these digits and introducing exponentiation signs ^, with adjacent digits getting concatenated as multi-digit numbers. Exponentiation is evaluated as right-associative.

For example, [4, 2, 3, 3] could become 2^34^3 which evaluates as 2^(34^3), but simply writing 2334 is smallest here. You can assume that any array containing a 0 gives a minimum of zero (a special case), or that it's achievable by an expression like 0^rest, or like 00 for just zeroes.

The input list can be taken as any ordered sequence, but not as an unordered (multi)set.

Test cases:

[2,3] => 8 (2^3)
[3,3] => 27 (3^3)
[4,3] => 34 (34)
[1,2,3] => 1 (1^2^3)
[2,2,2] => 16 (2^2^2)
[2,3,2] => 81 (2^3^2)
[3,0] => 0 (Special, or maybe 0^3)
[0,1,0] => 0 (Special, or maybe 0^10)


Shortest code in each language wins. I'll accept an answer just to give the +15 rep without any specific accepting standard.

• "Acception only act as a +15." Are you saying that when/if you accept an answer, it will only count as 15 reputation? May 11, 2020 at 12:05
• I like the challenge concept, but your text is hard to read and it took me a few tries to understand it.
– xnor
May 11, 2020 at 12:05
• @ouflak That means if someone is accept, it only mean he get 15 reputation, I don't claim any accepting standard
– l4m2
May 11, 2020 at 12:07
• For golfers who might want to write a non-brute-force solution, it looks like the minimal result is the digits concatenated in sorted order, except for smaller values of 0 for any list with a 0, 1 for any list with a 1, and these six values produced from power expressions: 2^2=4, 2^3=8, 2^4=16, 3^3=27, 2^2^2=16, 3^2^2=81.
– xnor
May 11, 2020 at 12:23
• Probably worth including [2,3,2] in the examples. May 11, 2020 at 17:16

# 05AB1E, 12 11 bytes

œε.œJ€.«m}ß


Explanation:

œ            # Get all permutations of the (implicit) input-list
ε           # Map each permutation to:
.œ         #  Get all its partitions
J        #  Join each inner-most list together
€       #  For each inner list:
.«     #   Right-reduce it by:
m    #    Taking the power
}ß  # After the map: pop and push the flattened minimum
# (after which it is output implicitly as result)


# Jelly, 10 bytes

Œ!*@/€;ḌƊṂ


Try it online!

### How?

Checks digital and power-wise evaluations of all permutations even though we only need to check forward-sorted-digital, forward-sorted-power-wise, and reverse-sorted-power-wise (and only for [3,2,2]), because it's far terser.

Note that there is no need to check any mixture of digital and power-wise evaluations (they can never be strictly smaller than one of the three previously mentioned evaluations)

Œ!*@/€;ḌƊṂ - Link: list, L
Œ!         - all permutations (of L) -> P
€     -   for each (p in P):
/      -     reduce with:
@       -       using swapped arguments:
*        -         exponentiation
Ḍ   -   un-decimal (vectorises across P)
;    -   concatenate (these two lists of numbers)
Ṃ - minimum

• Notice that you happen to use 0^1^0^0 on [0,0,0,1] and 0000 on [0,0,0,0]. Unexpected work :)
– l4m2
May 11, 2020 at 19:34

# Japt-g, 12 bytes

á Ër!p mDìÃn


Try it

• Fails for [2,2,3], which results in 64 instead of 81. May 11, 2020 at 16:30
• You don't write $2{^2} {^3}$, but $3^{2^2}$ is fine
– l4m2
May 11, 2020 at 16:33
• @KevinCruijssen, 2**3**2=64 May 11, 2020 at 17:04
• "and power must go from right to left" -> 2**(3**2) = 512 May 11, 2020 at 17:09
• It means that the precedence is to be treated as right-most first, i.e. 2**3**2 is to be $2^{(3^2)}=2^9=512$. May 11, 2020 at 17:14

# Io, 77 bytes

Based on xnor's observation in the comment section. -12 bytes thanks to Arnauld's idea.

method(x,y :=x sort;if(y==list(2,2,3),81,y join()asNumber min(y reduce(**))))


Try it online!

## Explanation

method(x,           // Anonymous method with param x
y := x sort     // Assign y to sorted x
if(y == list(2,2,3), // Edge case treatment
81,              // (It's a kinda weird case that goes right to left)
,y join()asNumber// Join the sorted digit-list into a single number.
min(                // Minimum with:
y reduce(**)))) // the sorted list reduced by the power function.


# JavaScript (ES7),  65  62 bytes

Fixed thanks to @xnor and @l4m2
Saved 3 bytes thanks to @l4m2

a=>Math.min(x=a.sort().join,a[0]&&eval(x-223?a.join**:81))


Try it online!

### How?

The general case is to look for the minimum of the concatenation of the digits in ascending order and an exponent chain, also in ascending order.

But as noticed by @xnor, there are a few special cases:

• if the list contains a $$\0\$$, the result is always $$\0\$$
• for [3,2,2], the minimum value is $$\3^{2^2}=81\$$
• Be careful with the [3,2,2] input
– xnor
May 11, 2020 at 13:31
• @xnor Thank you for the notification. Hopefully fixed now. May 11, 2020 at 13:46
• Fail on [0,0,1]
– l4m2
May 11, 2020 at 15:40
• @l4m2 Thanks. Updated. May 11, 2020 at 16:02
• Fail on [3,2,2,0,0]. I consider x*a[0] in numbers for min instead
– l4m2
May 11, 2020 at 16:08

# perl -alp, 83 bytes

%_=(22,4,23,8,24,16,33,27,222,16,223,81);$_=join"",sort@F;$_=/0/||/1/?$&:$_{$_}||$_


Try it online!

Reads a list of white space separated numbers from STDIN, writes the answer to STDOUT. If the input contains a 0, output 0, else, if the input contains a 1, output a 1. Else, if it's one of 6 special cases, output the corresponding value. Otherwise, output the sorted input.