# Implement the iOS 11 Calculator

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

• 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.
• Also, iOS 11 doesn't work like that. It should be like this instead. (code explanation) – user202729 Oct 24 '17 at 9:39
• @user202729 The question is probably inspired by iOS 11. I don't think the OP is asking you to replicate it entirely. – Okx Oct 24 '17 at 9:42
• @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. – Hauleth Oct 24 '17 at 9:45
• Are the inputs integer? – xnor Oct 24 '17 at 15:46
• 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. – NH. Oct 24 '17 at 22:05

# Kotlin, 46 44 bytes

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


Try it online!

# Edits

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

# C++, 109 bytes

#import<list>
int f(std::list<int>l){l.sort();int s=0;for(int i:l)s+=i;return l==std::list<int>{1,2,3}?24:s;}


Try it online!

# Perl 5, 25 bytes

[sort@_]~~[1..3]?24:sum@_


Try it online!

• As it stands, this is a code fragment. You need to include all of the bytes in your score, including command line options. – Xcali Oct 30 '17 at 22:30

# Racket, 49 bytes

(λ x(if(equal?(sort x <)'(1 2 3))24(apply + x)))


Try it online!

[sort@_]~~[1..3]?24:0+map{1.._}@_  Try it online! This is similar to my other answer, but does not use the import sum function. Instead it creates an array whose size is the sum of the number. But only works for non-negative integers. # Ruby, 29 bytes ->a{a.sort==[*1..3]?24:a.sum}  # APL (Dyalog), 20 15 bytes 5 bytes saved thanks to @EriktheOutgolfer {4*⍵[⍋⍵]≡⍳3}×+/  Try it online! • {4*⍵[⍋⍵]≡⍳3}×+/ – Erik the Outgolfer Oct 24 '17 at 19:08 # q/kdb+, 24 bytes Solution: (sum x;24)1 2 3~x:asc(),  Try it online! Note: Link is to a K (oK) port of the K4 version below as there is no TIO for q/kdb+. Examples: q)(sum x;24)1 2 3~x:asc(),1 1 q)(sum x;24)1 2 3~x:asc(),1 2 3 q)(sum x;24)1 2 3~x:asc(),1 2 3 24 q)(sum x;24)1 2 3~x:asc(),1 2 3 4 10 q)(sum x;24)1 2 3~x:asc(),1 3 2 24  Explanation: q is interpreted right-to-left: (sum x;24)1 2 3~x:asc(), / the solution (), / convert to list if not already a list (cannot sort atom) asc / sort list ascending x: / store input in variable x 1 2 3~ / is sorted list equal to list 1 2 3? boolean 0 or 1 ( ; ) / two item list which we index into 24 / index 1 (true), the fake result 24 sum x / index 0 (false), sum the input list  Notes: • K4 version that aligns with TIO link: (+/x;24)1 2 3~x@<x:(), for 22 bytes • Q is just syntactic sugar on top of K4, sum is +/, asc is x@<x # C (gcc), 111 153 bytes #define A(x)if(v[x]>v[x+1]&c>2)t=v[x],v[x]=v[x+1],v[x+1]=t; i;s;t;f(c,v)int*v;{for(s=i=0;i<c;s+=v[i++]);A(0)A(1)A(0)return c!=3|*v-1|v[1]-2|v[2]-3?s:24;}  Try it Online! +42: Fixed {3,2,3}, {1,1,1}, {2,2,2}, etc. I will try to golf more later. f is a function which takes the length of the array as an int and a pointer to the first element of the array (of unsigned ints) and returns the 'IOS 11 Calculator Sum' of the array. Ungolfed (Old, broken version): #define not_one_two_or_three(index) (arr[index] > 3 || arr[index] == 0) int counter; int sum; int ios_sum(int length, unsigned int *arr) { sum = 0; for (counter = 0; counter < length; counter++) { sum += arr[counter]; } if (length != 3 || not_one_two_or_three(0) || not_one_two_or_three(1) || not_one_two_or_three(2)) return sum; else return 24; }  • This doesn't work for input 3, 2, 3. – cleblanc Oct 26 '17 at 12:55 • 148 bytes – ceilingcat Nov 2 '19 at 9:00 # Ruby, 28 bytes ->s{s&[*1..3]==s&&24||s.sum} # Haskell, 47 bytes import Data.List f l=suml++[18|sort l==[1..3]]


Try it online!

# Japt-x, 14 11 bytes

n e3õ)?24:U


Try it online!

• Nice technique, but I think this would fail on inputs [1,23] and [123]. – ETHproductions Oct 24 '17 at 20:02
• @ETHproductions Thanks. I wish there were a better way to compare arrays. – Oliver Oct 24 '17 at 20:59

L,dB#3R=''1$o$b+*

• OP says negative numbers don't have to be supported, implying that non-negative numbers do... however on this answer 1 + 2 + 3 + 0 = 24 – FlipTack Oct 30 '17 at 19:52
• Given 3, 2, 1, this outputs 6. – FlipTack Oct 30 '17 at 20:26