43
\$\begingroup\$

iOS 11 has a bug that makes the result of 1+2+3 to be 24. This is related to the animation speed, but anyway:

The task is to make 1 + 2 + 3 == 24. But only that. So you should provide a function that correctly sums most sequences but returns 24 when the arguments are 1, 2 and 3 in any order.

Example inputs:

1 2 => 3
3 4 => 7
1 2 3 4 5 6 7 8 9 => 45
3 2 1 => 24
2 1 3 => 24
1 1 => 2
1 2 3 => 24
40 2 => 42
1 2 2 4 => 9
1 2 3 4 1 2 3 => 16
1 => 1
1 23 => 24
0 1 2 => 3
3 2 3 => 8

Input can be in any format as long as your code accepts any number of arguments.

  • Support for negative numbers isn't required (all non negative numbers are required to work, that includes 0)
  • We assume correct input

Differences from another similar question: "What do you get when you multiply 6 by 9? (42)":

  • In this case your function is required to accept any number of arguments. The old question specifies exactly 2.
  • In this case order doesn't matter, while the old question specified that order 6 9 is required and 9 6 should be evaluated correctly.
\$\endgroup\$
  • 23
    \$\begingroup\$ Also, iOS 11 doesn't work like that. It should be like this instead. (code explanation) \$\endgroup\$ – user202729 Oct 24 '17 at 9:39
  • 3
    \$\begingroup\$ @user202729 The question is probably inspired by iOS 11. I don't think the OP is asking you to replicate it entirely. \$\endgroup\$ – Okx Oct 24 '17 at 9:42
  • 2
    \$\begingroup\$ @Okx exactly. This is for fun, not to implement it 1 to 1. Of course this could be changed to the user202729 proposal, but if he want he can create new challenge with such task. \$\endgroup\$ – Hauleth Oct 24 '17 at 9:45
  • 3
    \$\begingroup\$ Are the inputs integer? \$\endgroup\$ – xnor Oct 24 '17 at 15:46
  • 9
    \$\begingroup\$ One reason this is a beautiful challenge is because of the property linked to wherein this combination of numbers is very special. The other reason this is a beautiful challenge is that it pokes fun at Apple for prioritizing (their idea of) UX over functionality. \$\endgroup\$ – NH. Oct 24 '17 at 22:05

43 Answers 43

11
\$\begingroup\$

MATL, 11 10 bytes

St3:X=6*+s

Try it online! or verify all test cases

Explanation

        % implicit input [3,1,2]
S       % sort
        % STACK: [1,2,3]
t       % duplicate elements
3:      % push range 1..3
        % STACK: [1,2,3], [1,2,3], [1,2,3]
X=      % true if arrays are numerically equal
        % STACK: [1,2,3], 1
6*+     % multiply result of comparison by 6 and add to the original array
        % STACK: [7,8,9]
s       % sum
        % (implicit) convert to string and display
\$\endgroup\$
11
\$\begingroup\$

05AB1E, 9 bytes

Os{3LQi4*

Explanation:

Os{         Get the sum of the input, and then get the sorted input list
     Qi     If it is equal to...
   3L       [1, 2, 3]
       4*   Then multiply the sum by four.

Try it online!

\$\endgroup\$
11
\$\begingroup\$

Java 8, 109 106 101 90 75 74 71 66 bytes

a->{int s=0,p=0;for(int i:a){s+=i;p|=1<<i;}return s<7&p==14?24:s;}

-12 bytes thanks to @OlivierGrégoire.
-31 bytes thanks to @Nevay.

Explanation:

Try it here.

a->{                  // Method with integer-array parameter and boolean return-type
  int s=0,            //  Sum-integer, starting at 0
      p=1;            //  Product-integer, starting at 1
  for(int i:a){       //  Loop over the input-array
    s+=i;             //   Add current item to sum
    p|=1<<i;          //   Take 1 bitwise left-shifted with `i`, and bitwise-OR it with `p`
  }                   //  End of loop
  return p==14        //  If `p` is now exactly 14 (`0b1110`)
    &s<7?             //  and the sum is 6 or lower:
     24               //   Return 24
    :                 //  Else:
     s;               //   Return the sum
}                     // End of method

(Inefficient) proof that only [1,2,3] (in any order) will be the possible results when p is 0b1110 (p==14) and the sum is below 6 or lower (s<7): Try it here.

p==14 (0b1110) evaluates to true iff the input values modulo 32 cover the values 1, 2 and 3 and contain no other values (p|=1<<i) (each value has to occur 1+ times). The sum of input that matches p==14 will be larger than 6 for any input except 1,2,3 (s=a*1+b*2+c*3+u*32 with a>0,b>0,c>0,u>=0).
@Nevay


Old 71 bytes answer:

a->{int s=0,p=1;for(int i:a){s+=i;p*=i;}return a.length==3&p==s?s*4:s;}

Proof that for any three given non-zero natural numbers, only [1,2,3] (in any order) will have a sum equal to its product (1+2+3 == 1*2*3) (with a positive sum):
When the sum equals the product by Leo Kurlandchik & Andrzej Nowicki

(Inefficient) proof that only [1,2,3] (in any order) and [0,0,0] will be the possible results with non-negative numbers and a length of 3: Try it here.
So s*4 will becomes 6*4 = 24 for [1,2,3], and 0*4 = 0 for [0,0,0].

\$\endgroup\$
7
\$\begingroup\$

Jelly, 8 bytes

3R⁼Ṣ4*×S

Try it online!

\$\endgroup\$
7
\$\begingroup\$

MATL, 13 bytes

stGp=18*Gda*+

Try it online!

It's two bytes longer than the other MATL answer, but it uses a completely different (and IMO more interesting) approach, so I figured it's worth posting.

Explanation:

This solution uses the fact that:

The sum and product of an array with three elements are only equal if the array is a permutation of 1,2,3.

This takes the input, calculates the sum s, and duplicates it t. It then checks if the sum equals the product Gp=. We multiply the boolean 1/0 by 18, 18*, and checks if there are non-identical values in the vector da* (again, multiply by a boolean any(diff(x)). We then multiply the two add the last number to the original sum.

A step by step explanation:

Assume the input is [1, 2, 3]:

s                              % Implicit input, calculate the sum
                               % 6
 t                             % Duplicate the sum:
                               % 6, 6
  G                            % Grab input
                               % 6, 6, [1,2,3]
   p                           % Calculate the product
                               % 6, 6, 6
    =                          % Check if the last two elements are equal
                               % 6, 1 (true)
     18*                       % Push 18, multiply by the last number
                               % 6, 18
        G                      % Grab input
                               % 6, 18, [1,2,3]
         d                     % Calculate the difference between each element
                               % 6, 18, [1,1]
          a                    % any non zero elements?
                               % 6, 18, 1 (true)
           *                   % Multiply last two numbers
                               % 6, 18
            +                  % Add numbers. Result: 24
\$\endgroup\$
6
\$\begingroup\$

Python 2, 39 bytes

lambda*a:sum(a)+18*(sorted(a)==[1,2,3])

Try it online!

Uses an alternate method of adding 18 if the sorted input is [1, 2, 3] to beat the other Python answer by a byte.

\$\endgroup\$
  • \$\begingroup\$ sorted(a)==[1,2,3] can become set(a)=={1,2,3} to save 3 bytes. \$\endgroup\$ – mypetlion Oct 24 '17 at 19:30
  • 1
    \$\begingroup\$ @mypetlion Unfortunately that would yield true for lists with duplicates such as [1, 2, 3, 3] \$\endgroup\$ – FlipTack Oct 24 '17 at 19:31
  • \$\begingroup\$ Woops. I though we were restricted to exactly 3 inputs. \$\endgroup\$ – mypetlion Oct 24 '17 at 19:32
6
\$\begingroup\$

Haskell, 37 bytes

f[a,b,c]|2^a+2^b+2^c==14=24
f l=sum l

Try it online!

We use pattern matching to catch the exceptional case.

Haskell doesn't have sorting built-in. The equality 2^a+2^b+2^c==14 is satisfied only by [a,b,c] a permutation of [1,2,3] among non-negative integers. A shorter a+b+c=a*b*c almost works, but is satisfied by [0,0,0], and appending the check ,a>0 makes it 1 byte longer.

\$\endgroup\$
4
\$\begingroup\$

Octave, 34 bytes

@(x)sum(x)+isequal(sort(x),1:3)*18

Try it online!

or

@(x)(s=sum(x))+18*~(s-6|prod(x)-6)

Try it online!

or

@(x)(s=sum(x))+18*~(s-prod(x)|s-6)

This is shorter than the approach others use: @(x){24,sum(x)}{2-isequal(sort(x),1:3)}.

Explanation:

It takes the sum of the vector, and adds 18 if the sorted vector is equal to 1,2,3. This will give 6+18=24 if the vector is a permutation of 1,2,3, and just the sum of the vector if not.

\$\endgroup\$
4
\$\begingroup\$

PHP, 116 bytes

This is my first attempt at a golfing challenge ever, AND it's PHP, a language which apparently sucks at golfing since I rarely see it here, so ... uhm, I tried?

<?php
//call as var/www/html/cg_ios.php --'1 2 3 4 5'
$i=$argv[1];$a=(explode(' ',$i));echo((($b=array_sum($a))==6&&count($a)==3&&in_array(3,$a)&&!in_array(0,$a)?24:$b));

Note: I did not include the comment into the bytecount.

Ungolfed

It's nothing special tbh:

$i=$argv[1];             //Read the input from the command line
$a=array_filter($c=explode(' ',$i)) //Split the input string into an array, use Whitespace as delimiter
                         //also, remove all 0 from the array, since they are not important at all
echo(                    //print the result
    ($b=array_sum($a) == 6  //If The SUM of the Array is 6 ...
        &&               //... AND ...
    (count($c) == 3)     //... the array has exactly 3 values ...
        &&               //... AND ...
    in_array(3,$a)       // ... the array contains the value 3 ...
        &&               // ... AND ...  
    !in_array(0,$a)      //... the array contains no zeros
        ?
    24                   //print 24
        :
    $b));     //print the sum of the array values we saved earlier

If you want to test this in PHPFiddle and not on console, you can obviously replace $i with anything you'd like.

Thanks to Olivier Grégoire who made me aware of the string combination [0,3,3] which returned 24 before and also helped me saving a few chars by storing the array_sum and returning that instead of running the function again.

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site, and nice first post! \$\endgroup\$ – caird coinheringaahing Oct 24 '17 at 16:11
  • \$\begingroup\$ What about input values [0, 3, 3]? Also, can't you save the result of array_sum($a) in a variable and reuse it? \$\endgroup\$ – Olivier Grégoire Oct 25 '17 at 8:45
  • \$\begingroup\$ @OlivierGrégoire That's fixed too now, I obviously missed that case. I probably could think of a better solution tho, this is - even for my standards - really ... messy. \$\endgroup\$ – Y U NO WORK Oct 25 '17 at 9:45
  • \$\begingroup\$ Golfed code is missing $ on argv[1] \$\endgroup\$ – manassehkatz Oct 26 '17 at 3:18
4
\$\begingroup\$

R, 47 bytes 34 bytes 36 bytes

x=scan();all(sort(x)==1:3)*18+sum(x)

Try it online!

Sum the input and add 18 if the input set is 1:3.
Thanks to @mlt for golfing off 11 bytes. Thanks to @Ayb4btu for identifying an error with the overgolfed code

\$\endgroup\$
3
\$\begingroup\$

Javascript ES6, 39 bytes

Thanks to @Herman Lauenstein

a=>a.sort()=="1,2,3"?24:eval(a.join`+`)

f=a=>a.sort()=="1,2,3"?24:eval(a.join`+`)

console.log(f([1,2,3]));
console.log(f([1,2,3,4]));

Previous answer

Javascript ES6, 66 bytes

a=>(n=[1,2,3],a.every(_=>n.includes(_))?24:a.reduce((x,y)=>x+y,0))

Try it

f=a=>(n=[1,2,3],a.every(_=>n.includes(_))?24:a.reduce((x,y)=>x+y,0))

console.log(f([1,2,3]));
console.log(f([1,3,2]));
console.log(f([1,2,3,4]));

\$\endgroup\$
  • \$\begingroup\$ 58 bytes: a=>(Array.sort(a).join()=="1,2,3"?24:a.reduce((x,y)=>x+y)) \$\endgroup\$ – Okx Oct 24 '17 at 10:24
  • \$\begingroup\$ a.sort()=="1,2,3" works. \$\endgroup\$ – Neil Oct 24 '17 at 10:36
  • \$\begingroup\$ 39 bytes: a=>a.sort()=="1,2,3"?24:eval(a.joinBT+BT) (replace BT with backticks) \$\endgroup\$ – Herman L Oct 24 '17 at 10:46
3
\$\begingroup\$

Swift, 67 Bytes

func z(i: [Int])->Int{return i.sorted()==[1,2,3] ?24:i.reduce(0,+)}

Could make it to 27 bytes with extensions on [Int], but that would be cheating :(

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site! This is a code golf competition, please can you golf you code as much as possible, such as by removing spaces. Also, I don't know Swift, but if I'm correct, this saves the input in a variable, which isn't allowed. You are however, allowed to turn this into a function. \$\endgroup\$ – caird coinheringaahing Oct 24 '17 at 14:33
  • 1
    \$\begingroup\$ Golfed down a little: func z(i:[Int]){print(i.sorted()==[1,2,3] ?24:i.reduce(0,+))}. \$\endgroup\$ – Mr. Xcoder Oct 24 '17 at 14:46
  • 1
    \$\begingroup\$ Or 55 bytes (-12): {$0.sorted()==[1,2,3] ?24:$0.reduce(0,+)}as([Int])->Int, as anonymous functions are allowed by our standard rules. You can see how it works here. \$\endgroup\$ – Mr. Xcoder Oct 24 '17 at 14:53
  • \$\begingroup\$ @Mr.Xcoder you could omit the casting and add it to the f constant you are declaring. or put the type inside the closure and you get rid of as :) \$\endgroup\$ – Dominik Bucher Oct 24 '17 at 14:59
  • 2
    \$\begingroup\$ +1 for Swift. Is this the original source code of the iOS calculator? \$\endgroup\$ – G B Oct 25 '17 at 12:08
2
\$\begingroup\$

Mathematica, 28 bytes

If[Sort@#=={1,2,3},24,Tr@#]&

Try it online!

\$\endgroup\$
2
\$\begingroup\$

J, 17 bytes

-6 bytes thanks to Frowny Frog

+/*1+3*1 2 3-:/:~

Sum all the numbers +/ and multiply the result by (pseudocode) 1 + 3*(is123 ? 1 : 0). That is, return the results unchanged unless the sorted list is 1 2 3 in which case we multiply the result by 4.

Try it online!

original answer

+/`(24"_)@.(1 2 3-:/:~)

Check if the sorted input is 1 2 3 -- if yes, invoke the constant function 24 (24"_); if not, return the sum +/

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I don't really know J, but can 1 2 3i.3? \$\endgroup\$ – Uriel Oct 24 '17 at 20:46
  • \$\begingroup\$ @Uriel, i.3 produces 0 1 2, so you'd have to do 1+i.3 which saves no chars but is less clear. \$\endgroup\$ – Jonah Oct 24 '17 at 20:53
  • \$\begingroup\$ right, I forgot J is 0 indexed \$\endgroup\$ – Uriel Oct 24 '17 at 21:19
  • \$\begingroup\$ [:+/8"0^:(1 2 3-:/:~) \$\endgroup\$ – FrownyFrog Oct 24 '17 at 23:01
  • \$\begingroup\$ +/@,[*3*1 2 3-:/:~ \$\endgroup\$ – FrownyFrog Oct 24 '17 at 23:40
2
\$\begingroup\$

C# (.NET Core), 57+18=75 bytes

a=>a.OrderBy(x=>x).SequenceEqual(new[]{1,2,3})?24:a.Sum()

Try it online!

+18 for using System.Linq;

\$\endgroup\$
2
\$\begingroup\$

Lua, 116 81 bytes

-7 bytes thanks to Jonathan

Takes input as command line arguments

Z=0S={}for i=1,#arg do
j=arg[i]+0S[j]=0Z=Z+j
end
print(#S>2 and#arg<4 and 24or Z)

Try it online!

Explanation:

Works by creating an sparse array S and adding zeroes in the indices corresponding to the input values. If parameters are 3, 4, 7 the sparse array will only have numbers at those indices. With that array, we get it's length with the operator # that counts from index 1 up to the higher index that has a value in it, if this length is exactly 3, it means that there were elements in the position 1, 2 and 3 wich is what we're looking for. The length of the sparse array will be always between 0 and N where N is the number of parameters. So we just have to check if the length of both the parameters array and the sparse array is 3.

\$\endgroup\$
  • 1
    \$\begingroup\$ 80 bytes \$\endgroup\$ – Kevin Cruijssen Oct 24 '17 at 13:39
  • \$\begingroup\$ You saw nothing (I edited my comment, since it wasn't after 5 minutes yet. ;P) \$\endgroup\$ – Kevin Cruijssen Oct 24 '17 at 13:43
  • \$\begingroup\$ Yeah, just found the same flaw in my own answer.. I fixed it by using array-length==3 AND A==S AND S>0. But I guess checking the length of the #args in Lua is a bit too byte-heavy? In that case you can rollback to your 90-bytes answer I guess.. :( \$\endgroup\$ – Kevin Cruijssen Oct 24 '17 at 14:22
  • \$\begingroup\$ 83 bytes \$\endgroup\$ – Jonathan S. Oct 25 '17 at 17:18
  • \$\begingroup\$ @JonathanS. nice one \$\endgroup\$ – Felipe Nardi Batista Oct 25 '17 at 23:18
2
\$\begingroup\$

R, 55 45 54 49 57 54 48 bytes

Saved many bytes and incorrect solutions thanks to Ayb4btu.

Saved 3 9 bytes thanks to Giuseppe. I keep learning new ways to abuse the fact that F==0.

"if"((s=sum(x<-scan()))-prod(x)|sum(x|1)-3,s,24)

Try it online!

The other R answer won in the end.

\$\endgroup\$
  • \$\begingroup\$ Fails for [0,0,0]: returns 24 instead of 0. \$\endgroup\$ – Olivier Grégoire Oct 25 '17 at 8:37
  • \$\begingroup\$ I apparently missed both 'non-negative' and 'can be a single number' in the spec. Hold on. \$\endgroup\$ – BLT Oct 25 '17 at 16:13
  • \$\begingroup\$ c(1,1,2,3) returns 28 instead of 7 \$\endgroup\$ – Ayb4btu Oct 26 '17 at 2:06
  • \$\begingroup\$ @Ayb4btu Thank you, nice catch. \$\endgroup\$ – BLT Oct 26 '17 at 2:16
  • 1
    \$\begingroup\$ x=scan();s=sum(x);"if"(s-prod(x)|s-6|length(x)-3,s,24) is 54 bytes swapping the condition and using | instead of & so we can subtract. \$\endgroup\$ – Giuseppe Oct 27 '17 at 14:26
2
\$\begingroup\$

C (gcc), 136 131 125 97 91 bytes

i,r,t;main(c,v)char**v;{for(;c-->1;)i=atoi(v[c]),r+=i,t|=1<<i;printf("%d",r<7&t==14?24:r);}

Try it online!

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

Retina, 21 bytes

O`
^1¶2¶3$
24
.+
$*
1

Try it online!

Input is linefeed-separated, but the test suite uses comma-separation for convenience.

Explanation

O`

Sort the numbers (lexicographically, actually, but we only care about the case that the inputs are 1, 2, 3 in some order, where that doesn't make a difference).

^1¶2¶3$
24

If the input is 1,2,3 (in some order), replace it with 24.

.+
$*

Convert each number to unary.

1

Count the number of 1s, which adds the unary numbers and converts them back to decimal.

\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, based on your explanation I understand the final line counts all occurrences of the match (all 1s in this case). Does Retina always do this for a single final line? Or is it also possible to count all 1s somewhere in between, and then after that continue with the result to do something else (which uses two lines again for replace-actions)? Also, another related question: which functions in Retina require only a single line? The sorting (O`) is one of them, and the other functions as well; but any other? Just trying to understand Retina a bit more. :) \$\endgroup\$ – Kevin Cruijssen Oct 24 '17 at 12:26
  • 1
    \$\begingroup\$ @KevinCruijssen Yes you can use a (counting) match stage somewhere in the middle, but you'll have to explicitly mark it as a match stage with M`. It's only if there is a single trailing line that Retina defaults to Match instead of Replace. AGMTS are all single-line stages, R is two lines, O and D are one or two lines depending on whether the $ option is used (which turns them into sort/deduplicate-by stages). Feel free to ping me in the Retina chatroom if you have more questions: chat.stackexchange.com/rooms/41525/retina \$\endgroup\$ – Martin Ender Oct 24 '17 at 12:31
1
\$\begingroup\$

Haskell, 44 bytes

f a|sum a==6,product a==6,a<[6]=24|1<2=sum a

Try it online!

The permutations of [1,2,3] are the only partitions of 6 whose product is 6, barring 6 itself. (This assumes the inputs are non-negative, which seems to be the case for all the test cases… I’ve asked the OP about this.)

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

PL/SQL - 135 123 Bytes

Assuming i as an integer array input of any size:

if (select sum(s) = exp(sum(ln(s))) from unnest(i) s) then
    return 24;
else
    return (select sum(s) from unnest(i) s);
end if;
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Could you try to golf it by removing all the extra spaces, even if it makes the answer unreadable (as long as it compiles, though)? \$\endgroup\$ – Olivier Grégoire Oct 25 '17 at 8:43
1
\$\begingroup\$

C++17, 56 54 bytes

[](auto...i){return(-i&...)+4|(~i*...)+24?(i+...):24;}

Try it online!

Note that the function object created is usable at compile time, so the tests are performed by the compiler without having to run a program.

Explanation:

[]             // Callable object with empty closure,
(auto...i)     // deduced argument types,
{              // and deduced return type
  return       //
      (-i&...) //   If the fold over the bitwise AND of the negation of each argument
    +4|        // is unequal to -4, or
      (~i*...) //   if the product of the bitwise complements of the arguments
    +24?       // is unequal to -24, then
      (i+...): //   return the sum of the arguments, otherwise
      24;}     //   return 24.

Proof that the only nonnegative i... for which (-i&...) equals -4 and (~i*...) equals -24 are the permutations of 1, 2, 3:

We first observe that since -0 = 0, if any i = 0 then (-i&...) = 0, so we conclude that all the i are positive.

Now, note that in 2's complement, -i is equivalent to ~(i - 1), and ~i is equivalent to -(i + 1). Applying De Morgan's rule, we find that (-i & ...) = ~((i - 1) | ...) = -(((i - 1) | ...) + 1), so ((i - 1) | ...) = 3; similarly, -1 ** n * ((i + 1) * ...) = -24, so n is odd and ((i + 1) * ...) = 24.

The prime factors of 24 are 2**3 * 3, so n <= 4. If n = 1, we have i - 1 = 3 and i + 1 = 24, so n = 3. Write the i wlog as a <= b <= c, then clearly a = 1 as otherwise (a + 1)(b + 1)(c + 1) >= 27. Also c <= 4 as otherwise (a - 1)|(b - 1)|(c - 1) >= 4. c cannot be 4 as 5 is not a factor of 24, so c <= 3. Then to satisfy (a - 1)|(b - 1)|(c - 1) = 3 c = 3, b = 2 as required.

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

Husk, 9 bytes

?K24Σ=ḣ3O

Try it online!

Explanation

?K24Σ=ḣ3O
        O    Sort the input
?    =ḣ3     If it is equal to [1,2,3]:
 K24           Return 24
             Else:
    Σ          Return the sum of the input

Previous solution

Gives the wrong result to [2,2], and probably other inputs too, but it was more interesting.

?ṁD→E§eΠΣ
     §eΠΣ    Build a two-element list with the product and sum of the input
?   E        If the two elements are equal:
             (true for any permutation of [1,2,3] and the list [0,0,0]
 ṁD            Double both elements and sum them
               (This is 4 times the sum: 24 for permutations of [1,2,3], 0 for [0,0,0])
             Else:
   →          Return the last element (the sum)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This gives 24 for 2,2 \$\endgroup\$ – recursive Oct 26 '17 at 16:38
  • \$\begingroup\$ @recursive actually it gives 16, but you are right. And probably this gives wrong results for some longer arrays too... Damn, I need to switch to a boring solution \$\endgroup\$ – Leo Oct 26 '17 at 22:12
0
\$\begingroup\$

Pushy, 12 bytes

gF3RFx?18;S#

Try it online!

This works by sorting the input and, if it is equal to [1, 2, 3], appending 18. Then, the sum is calculated and printed, yielding 24 is 18 was appended, and the normal answer otherwise.

         \ Implicit: Input on stack.
g        \ Sort input ascendingly
F3RF     \ On auxiliary stack, push range(3) -> [1, 2, 3]
x?       \ If the stacks are equal:
  18     \    Append 18 to the input
;
S#       \ Print sum of input.
\$\endgroup\$
0
\$\begingroup\$

Pyth, 9 bytes

?qS3SK24s

Verify all the test cases.

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

Python 2, 41 39 bytes

-1 byte thanks to caird

-1 by inspiration from FlipTack's answer

lambda*a:sum(a)*4**(sorted(a)==[1,2,3])

Try it online!

Alternative 39 bytes solution

lambda*a:sum(a)<<2*(sorted(a)==[1,2,3])
\$\endgroup\$
0
\$\begingroup\$

Jelly, 10 9 bytes

Ṣ24S⁼?3R¤

Try it online!

-1 byte thanks to Erik

Alternative (by Mr. Xcoder), also for 9 bytes:

3R⁼Ṣ×18+S

Try it online!

How it works

Ṣ24S⁼?3R¤ - Main link. Argument: l (list)

Ṣ         - Sort
     ?    - Ternary if statement
    ⁼     -  Condition: Is l equal to...
      3R¤ -    [1, 2, 3]
 24       -  If condition: Return 24          
   S      -  Else: Return the sum of the list
\$\endgroup\$
  • \$\begingroup\$ You can do Ṣ24S⁼?3R¤ for 9 bytes. \$\endgroup\$ – Erik the Outgolfer Oct 24 '17 at 11:32
  • \$\begingroup\$ Or 3R⁼Ṣ×18+S for 9 bytes too. \$\endgroup\$ – Mr. Xcoder Oct 24 '17 at 11:37
0
\$\begingroup\$

Pyth, 9 bytes

Different aproach from the other Pyth answer.

*sQ^4qS3S

Explanation:

A port from my Python answer

*sQ^4qS3SQ  # Full program (Q at the end is implicit and represents the input)

*           # A * B where A and B are the next two lines
  sQ        # Sum elements of input
  ^4        # 4 to the power of:
    qS3SQ   # Compares sorted input to [1, 2, 3] and returns 0 or 1

Try it online!

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

PowerShell, 44 bytes

param($a)($a-join'+'|iex)+18*!(diff(1..3)$a)

Try it online!

Similar algorithm to the Python and JavaScript answers. Takes input as a literal array $a. Then immediately sums $a together, which forms the left-hand operator of the +.

The right-hand is the diff (alias for Compare-Object) of 1,2,3 and $a -- this is either an empty array if they are equal, or a non-empty array of the different items if they are not equal -- enclosed in a Boolean-not. So, if they are equal, that makes the empty array (a falsey value) into $true.

That's then multiplied by 18 which implicitly casts $true to 1 and $false to 0. So the right-hand side will be 18 if the arrays are the same, and 0 otherwise. That gives the correct result of 24 if the input array is 1,2,3 in any permutation, and the summation of the input array otherwise.

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

Kotlin, 46 44 bytes

if(i.sorted()==listOf(1,2,3))24 else i.sum()

Try it online!

Edits

\$\endgroup\$
  • \$\begingroup\$ Could you use listOf(1,2,3) to save 2 bytes? I don't know Kotlin, so I'm not sure. \$\endgroup\$ – Mr. Xcoder Oct 24 '17 at 12:56

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.