# Finite Cantor's Diagonal

Given a list of N integers, each with N digits, output a number which differs from the first number because of the first digit, the second number because of the second digit, etc.

### Example

Given this list:

1234
4815
1623
4211


The number 2932's first digit is different from the first number's first digit, its second digit is different from the second number's second digit, etc. Therefore it would be a valid output.

### Inputs

• You may take both the list and N as input, or only the list if you wish.
• Each integer in the list will necessarily have as many digits as the length of the list (N)
• Numbers will not have any leading zeroes
• The input list must contain numbers and not strings.
• You may take inputs as function arguments, through STDIN, or anything similar.
• You may assume that the list will not be longer than 10 elements (and no number in the list will be bigger than 2147483647)

### Outputs

• It is not sufficient that the output is not in the list. The digits must differ as explained above.
• You can use any digit selection strategy that respects the constraint of different digits.
• The number cannot have leading zeroes
• You may output the number through STDOUT, return it from a function, etc.

### Test cases

Input:
12345678
23456789
34567890
45678901
56789012
67890123
78901234
89012345

Possible output: 24680246

Input:
1

Possible output: 2


### Scoring

This is , so the shortest answer in bytes wins.

• So we have to convert the STDIN to integer before parsing? May 5 '16 at 12:48
• @KennyLau input has to be numbers, if you language can parse digits directly you don't have to convert anything. May 5 '16 at 12:49
• But STDIN is string by default... May 5 '16 at 12:50
• Then convert the input string to a list of integers. May 5 '16 at 12:51
• Can we output a list of digits? May 5 '16 at 12:53

# Jelly, 8 7 bytes

1 byte saved thanks to Dennis.

DŒDḢỊ‘Ḍ


Try it online!

### Explanation

DŒDḢỊ‘Ḍ    Main link. Takes list as argument.
D          Convert each integer to decimal.
ŒD        Get the diagonals.
Ḣ       Get the first diagonal.
Ị      Check if every digit <= 1.
‘     Increment every digit.
Ḍ    Convert back to integer from decimal.


Converts each digit to 1, except 0 and 1 becomes 2.

## CJam (15 14 bytes)

qN/ee{:=i2%)}%


Online demo

Thanks to Adnan for a one-byte saving.

### Dissection

qN/    e# Split input on newlines
ee{    e# Label each line with its index and map:
:=i  e#   Get the character at the same index (i.e. diagonalise)
2%)  e#   Compute (ASCII value mod 2) + 1
e#   Maps 0 1 2 3 4 5 6 7 8 9
e#     to 1 2 1 2 1 2 1 2 1 2
}%

• @Adnan, that does work, thanks. Alternatively i2%) could be 49&), working directly with characters rather than ASCII codes. May 5 '16 at 14:17

# Python 2, 47 45 bytes

lambda x,n:int(x[1::n+3])%(10**n/2)+10**n/9


Thanks to @xnor for golfing off 2 bytes!

Test it on Ideone.

### How it works

x yields a string representation of the list x.

For the first test case, this gives the string

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]


[1::n+3] retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for ,, we retrieve the first digit of the first number, the second digit of the second number, etc.

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
^          ^          ^          ^          ^          ^          ^          ^


We now take the number modulo 10n ÷ 2 to map the first digit in the range [0, 4].

For 93579135, we get 93579135 % 50000000 = 43579135.

Finally, we add 10n ÷ 9 to the last result, which increments – wrapping around from 9 to 0 – all digits by 1 (no carry) or 2 (with carry).

For 43579135, we get 43579135 + 11111111 = 54690246.

# MATL, 1110 9 bytes

VXd9\QV!U


Takes only a column vector of integers as input. N is not provided.

Try it Online

Explanation

    % Implicity grab input as column vector of numbers
V   % Convert the input column vector into a 2D character array
Xd  % Grab the diagonal elements of the character array
9\  % Take the modulus of each ASCII code and 9
Q   % Add 1 to remove all zeros
V   % Convert the result to a string
!   % Transpose to yield a row vector of characters
U   % Convert back to an integer (as per the rules)
% Implicitly display result

• @LuisMendo Oh crud. It looks like it has a leading zero issue though when the first digit is 2: matl.tryitonline.net/… May 5 '16 at 13:51
• Maybe VXd9\QV!U May 5 '16 at 13:51
• Oh, I hadn't thought of leading zero... May 5 '16 at 13:52
• @LuisMendo Does it matter that they aren't unique? As long as they aren't the same value as the input it shouldn't matter. May 5 '16 at 13:55
• You're totally right. I was thinking about it the wrong way May 5 '16 at 13:56

# Pyth, 11 bytes

jk.eh!ts@b


Simple loop, change every digit to 1, except 1 becomes 2.

• Nice usage of the implicit Q and k! You can save one byte with during the digit-transformation though: s.eh-12@b May 6 '16 at 7:50

# Retina, 3938 37

(?<=(.*¶)*)(?<-1>.)*(.).*¶
$2 Td121  Saved 1 byte thanks Martin! Requires a trailing linefeed in the input. Gets diagonals and translates 0 and 2-9 to 1 and 1 to 2. The basic idea for getting the diagonals is to push a capture for each row above the current row and then consume a capture to match a character, then keep the next character. Try it online # J, 26 22 bytes 1+1>:i.@#{"_1"."0@":"0  Similar approach to the others using the <= 1 and increment method of the diagonal. ## Usage Only requires the list of integers as an argument.  f =: 1+1>:i.@#{"_1"."0@":"0 f 1234 4815 1623 4211 2 1 1 2 f 92345678 23456789 34567890 45678901 56789012 67890123 78901234 89012345 1 1 1 1 1 2 1 1  • Sorry to break the 1 streak... May 6 '16 at 5:06 # Python 2, 54 bytes d,r=1,"" for n in input():r+=1+-~n/d%9;d*=10 print r  # Java, 94 bytes int c(int[]d){int p=1,r=0,l=d.length,i=0;for(;i<l;p*=10)r+=(d[l-++i]/p%10==1?2:1)*p;return r;}  Pure numeric operations for the win! :) ### Sample input/output: 8 <-- size 12345678 <-- start of list 23456789 34567890 45678901 56789012 67890123 78901234 89012345 <-- end of list 21111211 <-- result from ungolfed code 21111211 <-- result from golfed code  ### Full program (with ungolfed code): import java.util.Scanner; public class Q79444 { int cantor_ungolfed(int[] data){ int power = 1; int result = 0; for(int i=0;i<data.length;i++){ result += (((data[data.length-i-1]/power))%10==1? 2 : 1)*power; power *= 10; } return result; } int c(int[]d){int p=1,r=0,l=d.length,i=0;for(;i<l;p*=10)r+=(d[l-++i]/p%10==1?2:1)*p;return r;} public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; for(int i=0;i<n;i++){ input[i] = sc.nextInt(); } System.out.println(new Q79444().cantor_ungolfed(input)); System.out.println(new Q79444().c(input)); sc.close(); } }  # Java, 93 bytes String k(int n,int[]a){String s="";for(int i=0;i<n;)s+=(a[i]+s).charAt(i++)<57?9:1;return s;}  ### Ungolfed String k(int n, int[] a) { String s = ""; for (int i = 0; i < n; ) s += (a[i] + s).charAt(i++) < 57 ? 9 : 1; return s; }  ### Output Input: 12345678 23456789 34567890 45678901 56789012 67890123 78901234 89012345 Output: 99991999 Input: 1234 4815 1623 4211 Output: 9999  # J, 37 bytes f@(9&-@"."0@((>:@#*i.@#){f=:[:,":"0))  Can probably be golfed, but I forgot if there was a command for "diagonals". • I've seen it used before, Martin used the anti-diagonals here. May 5 '16 at 13:31 • @FryAmTheEggman Yeah, that's close. I'm still looking, but there might not be. May 5 '16 at 15:11 • This seems to produce a leading zero if the diagonal begins with a 9. May 5 '16 at 15:59 • You can create the digits table if you take the input n using <list> (#:~#&10) <n>. The first diagonal can be found with (< 0 1) |: <list> where (< 0 1) is a box for the axes to select, using both, with |: May 5 '16 at 16:37 # Reng v.3.3, 60 bytes k1-#kaiír1ø ~; !nb$<
1[å{$}k*$k1-#k)9(-#oa$;]o)ks^$


For Reng, that was rather simple. Try it here! Input is a space-separated list of numbers.

## 1: init

k1-#kaiír1ø


k is the number of inputs (number of numbers), and we decrement by 1 and restore for the loop stage. aií takes all input. r reverses the stack for the processing of input. 1ø goes to the next line.

## 2: loop

1[å{$}k*$k1-#k)9(-#oa$;]o)ks^$


1[ takes the top item off the stack and into a new stack. å splits it into digits. {$} pushes a code block containing the operation "drop"; this is repeated k times (k*) and the code block is dropped ($. k1-#k decrements k. )9( puts 9 at the STOS, and - subtracts TOS from the STOS. #o stores this number in o, and a$; drops all the members of the stack. ] closes the parent stack. o puts o back at the top; this is our digit we're saving. ) moves it to the bottom so that we can continue our looping. s usually checks for non-input (i.e., equality to -1), but we can use it to break out of our loop when k == -1. So s^ goes up when k == -1. $ drops k from the stack, and our loop begins again.

## 3: final

                     ~; !nb$<  < directs the pointer left, and $ drops k from the stack. b is a leftway mirror, so we enter through it, but it bounce back when hitting ;, a stack-condition mirror. !n prints a digit if and only if we're going left. ~ ends the program when we're done printing.

# Mathematica 52 bytes

FromDigits[Mod[#,2]+1&/@Diagonal[IntegerDigits/@#]]&


This follows the approach of Peter Taylor and others (without using Ascii codes).

Example

FromDigits[Mod[#,2]+1&/@Diagonal[IntegerDigits/@ #]]&[{1234,4815,1623,4211}]


2112

# ClojureScript, 58 chars

#(int(apply str(map-indexed(fn[i x](- 9(get(str x)i)))%)))


Type requirements made this a little longer than necessary, and map-indexed being so many chars didn't help.

Often my submissions are valid Clojure as well, but this is using some of ClojureScript's leakiness with JavaScript. Subtraction of a number and string coerces the string to a number--that is, (- 9 "5") equals 4.

PHP, 46/41/40 bytes

while($a=$argv[++$i])echo($b=9-$a[$i-1])?$b:1; while($a=$argv[++$i])echo$a[$i-1]==7?6:7;

while($a=$argv[++$i])echo($a[$i-1]%2)+1;  Various digit selectors for comparison. I thought "9-digit" would be shortest, but the special case needed to keep a zero out of the first digit overwhelms it. Fed from CLI arguments: php -r "while($a=$argv[++$i])echo($b=9-$a[$i-1])?$b:1;" 12345678 23456789 34567890 45678901 56789012 67890123 78901234 89012345
86421864


$><<$_[$.-1].hex%2+1  A full program. Run with the -n flag. Uses the following mapping: n -> n%2+1. # JavaScript (ES6), 41 The %9+1 trick is borrowed from Suever's answer. For once, .reduce beats .map. Note, the += operator is used to avoid parentheses. a=>+a.reduce((t,n,d)=>t+=(n+t)[d]%9+1,'')  # Perl, 18 bytes Includes +1 for -p Run with the input lines on STDIN. Output is 1 except 2 when the diagonal is 1 cantor.pl #!/usr/bin/perl -p pos=$.;\$_=/1\G/+1


# Pyth, 14 bytes

s.e|-\1@bk\2M
`

Try it online!