7
\$\begingroup\$

Given six unique non-negative integers, print the two 3-digit integers comprised of those numbers that sum to the smallest amount.

Additionally,

  • If there is more than one set of numbers that meets the smallest-sum requirement, print the two that have the greatest difference. For example, 269 + 157 = 267 + 159 = 426; however, 269 - 157 > 267 - 159, so the first set should be printed.
  • Print the numbers on the same line largest -> smallest, separated by a single space.
  • Zero (0) cannot be used as a leading number.
  • Inputs will always be digits.

Testcases

input        | output
2 6 5 1 9 7  | 269 157
1 9 5 0 8 3  | 359 108
1 2 3 4 5 6  | 246 135

Scoring

As this is code golf, shortest entry wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ Are the inputs always going to be digits? If so, please specify that. \$\endgroup\$ – NoOneIsHere Aug 3 '16 at 20:13
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! This is a nice first challenge, but the output format is a little restrictive. Can that be relaxed to our standards for input/output? \$\endgroup\$ – AdmBorkBork Aug 3 '16 at 20:17
  • \$\begingroup\$ Feel free to edit as necessary. I was basing the question and input/output format on this post but am not married to it (codegolf.stackexchange.com/questions/49854/…) \$\endgroup\$ – thesnow Aug 3 '16 at 20:22
5
\$\begingroup\$

Jelly, 12 bytes

Ṣs3¬Þ€Fs2ZḌṚ

Try it online! or verify all test cases.

Background

Consider six digits a < b < c < d < e < f.

If a ≠ 0, a number pair of three-digit integers with minimal sum must clearly use a and b for the leftmost digits, c and d for the middle digits, and e and f for the rightmost digits.

That gives eight possible arrangements with identical sums (100(a + b) + 10(c + d) + (e + f)).

Since the difference should be as large as possible, all digits of the first integer should be larger than the corresponding digits of the second integer, leaving bdf10, ace10 as the optimal arrangement (difference 100(b - a) + 10(d - c)+ (f - e)).

Finally, if a = 0, a should still occur as early as possible (as middle digit), and a similar process reveals that the pair cdf10, bae10 is the correct solution.

How it works

Ṣs3¬Þ€Fs2ZḌṚ  Main link. Argument: <a, b, c, d, e, f> (in any order)

Ṣ             Sort; yield [a, b, c, d, e, f].
 s3           Split into triplets; yield [[a, b, c], [d, e, f]].
   ¬Þ€        Sort each triplet by logical NOT.
              If a ≠ 0, all digits have logical NOT 0, so this leaves the triplets
              unaltered. If a = 0, its logical NOT is 1, so the first triplet is
              sorted as [b, c, a], leaving [[b, c, a], [d, e, f]].
      F       Flatten; yield [a, b, c, d, e, f] or [b, c, a, d, e, f].
       s2     Split into pairs; yield [[a, b], [c, d], [e, f]] or
              [[b, c], [a, d], [e, f]].
         Z    Zip; yield [[a, c, e], [b, d, f]] or [[b, a, e], [c, d, f]].
          Ḍ   Undecimal; convert each triplet from base 10 to integer.
           Ṛ  Reverse the order of the generated integers.
\$\endgroup\$
1
\$\begingroup\$

Pyth, 17 bytes

_iRTChf*FhTcR2.pS

Try it online

How it works

_iRTChf*FhTcR2.pS

                S   sort input
              .p    permutations in lexicographic order
           cR2      chop each permutation into groups of 2
      f             filter for results T such that:
         hT           the first group
       *F             has a truthy (nonzero) product
     h              first result
    C               transpose
 iRT                convert both rows to base 10
_                   reverse
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 59 bytes

a=>a[b=!!+a.sort()[0],2-b]+a[3]+a[5]+' '+a[1-b]+a[b+b]+a[4]

Takes input as a character array (e.g. [..."195083"]) and returns two space-separated numbers in a string. 57 bytes if I can return the smaller number first:

a=>a[b=+!+a.sort()[0]]+a[2-b-b]+a[4]+' '+a[1+b]+a[3]+a[5]
\$\endgroup\$
1
\$\begingroup\$

Batch, 232 bytes

@if %1 gtr %2 %0 %2 %1 %3 %4 %5 %6
@if %2 gtr %3 %0 %1 %3 %2 %4 %5 %6
@if %3 gtr %4 %0 %1 %2 %4 %3 %5 %6
@if %4 gtr %5 %0 %1 %2 %3 %5 %4 %6
@if %5 gtr %6 %0 %1 %2 %3 %4 %6 %5
@if %1==0 (echo %3%4%6 %20%5)else echo %2%4%6 %1%3%5

Mostly bubble sort.

\$\endgroup\$
1
\$\begingroup\$

Perl, 88 66 64 66 64 Bytes

@a[0..2]=@a[1,2,0]if!(@a=sort@ARGV)[0];say@a[1,3,5],$",@a[0,2,4]

Prints the two numbers with no space between them :/

Needs -M5.01 flag

Thanks to @msh210 and @Dada for helping to reduce byte count!

\$\endgroup\$
  • \$\begingroup\$ Nice. But say@a[1,3,5],@a[0,2,4] is shorter. (It requires -M5.01, which is free according to site rules.) \$\endgroup\$ – msh210 Aug 3 '16 at 20:57
  • \$\begingroup\$ Thanks! I realized using the indexes was was shorter right after I posted and have corrected now. \$\endgroup\$ – theLambGoat Aug 3 '16 at 20:59
  • \$\begingroup\$ You're welcome! And welcome to PPCG.SE. Note also that it's pretty much customary here to express your appreciation, if any, for those who saved you bytes by mentioning them in the post. And note that you can another byte by using 0..2 instead of 0,1,2. \$\endgroup\$ – msh210 Aug 3 '16 at 21:08
  • \$\begingroup\$ It's -M5.10, and it is only free because you can use the -E flag instead of the -e flag on the command line. (afaik) \$\endgroup\$ – Brad Gilbert b2gills Aug 3 '16 at 21:09
  • \$\begingroup\$ @BradGilbertb2gills, no, it's -M5.01 (short for -M5.010). \$\endgroup\$ – msh210 Aug 3 '16 at 21:09
0
\$\begingroup\$

Perl 6, 82 bytes

*.permutations.map({my@a=.join.comb(3);@a if @a.all>100}).min({.sum,[R-] |$_}).put

Usage:

my &small = *.permutations.map({my@a=.join.comb(3);@a if @a.all>100}).min({.sum,[R-] |$_}).put;

small [2,6,5,1,9,7]; # 269 157
small [1,9,5,0,8,3]; # 359 108
small [1,2,3,4,5,6]; # 246 135

Explanation:

# Whatever lambda with input 「*」
*.permutations

  .map({

    # join and split into 3 character chunks
    my @a = $_.join.comb(3);

    # return the values if neither start with 「0」
    @a if @a.all > 100

  })

  # find the minimum
  .min({

    # by sum first
    $_.sum,

    # and reversed subtraction second
    # ( biggest difference in reversed order )
    [R-] |$_

  })

  # print it on a line, space separated
  .put
\$\endgroup\$
0
\$\begingroup\$

Ruby, 113 bytes

Try it online!

->a{(a.permutation(6).map{|c|c[0]*c[3]>0?[eval(c[0,3]*''),eval(c[3,3]*'')]:p}-[p]).min_by{|x,y|[x+y,-(x-y).abs]}}
\$\endgroup\$
0
\$\begingroup\$

PowerShell v2+, 90 68 bytes

$a,$b=(($n=$args|sort)-ne0)[0,1];"$b{1}{3} $a{0}{2}"-f($n-ne$a-ne$b)

Not quite as short as others, but showcases some nifty tricks.

Takes input as command-line arguments $args, sorts them, saves that into $n. That's encapsulated in parens and used as the left-hand argument to -ne0, which pulls out all elements not equal to zero (in PowerShell, array -comparator scalar functions as a filter). That resultant array is re-captured in parens, and the 0th(Hi Dennis) and 1st elements plucked out, and uses multiple-assignment to store them into $a and $b. This ensures that the smallest non-zero element is in $a and the next smallest non-zero element is in $b, and those form the basis for our output string.

The next portion is a string "..." using the -format operator. We specify locations in the string {0} for where that particular item goes. We feed to the -f operator the original (sorted) array $n, except without elements $a and $b, so four elements, which correspond to the 1,3,0,2 of the brackets. That string is left on the pipeline and output is implicit.


For a worked out example, let's assume that the OP meant non-negative input numbers, to match the test case of 1 9 5 0 8 3. This starts by sorting the input array, so we have 0 1 3 5 8 9 and re-saving that into $n. We pluck out the non-zero elements, so 1 3 5 8 9, and choose the [0,1] indices thereof. That means that $a=1 and $b=3, and thus we'll always know a < b and neither a nor b are 0.

Those are replaced in the string, so we have "3{1}{3} 1{0}{2}" to start. The -f operator takes the result of our array operation. Since $n is already still sorted, the pair of -ne operators result in 0 5 8 9 being fed to -f. As we're wanting the smallest sum, we obviously want the smaller elements to be used first. But, as we're wanting the largest difference, we want the larger elements to be associated to b. Hence, the {1}{3}{0}{2} arrangement for the indexing. This results in the string "359 108", the proper answer.

\$\endgroup\$
0
\$\begingroup\$

Python3 97 bytes

def x(*s):d=sorted(s);i=(0,1)[not d[0]];print(d[1+i],d[3],d[5],' ',d[0+i],d[(2+i)%3],d[4],sep='')

The approach is based on the narrative of @Dennis answer.

First try on Ideone with the code (I hope I did save it): Try it!

\$\endgroup\$
  • \$\begingroup\$ d=sorted(input())[-6:];i=d[0]=='0';print(d[1+i]+d[3]+d[5],d[i]+d[2-2*i]+d[4]) - simple improvement of your code (now only 77 bytes required) \$\endgroup\$ – Nikita Sivukhin Aug 6 '16 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.