# Find the different letter

You may have seen puzzles like this:

Find the 0:
OOOOOOOOOOOOOOOOOOOO0OOOOOOOOOOOOOOOOOO

The challenge is to write a program that finds the index of the different letter given an image.

### Input

Input will be an Image. The image will consist of one line of black text in the Helvetica 24 pt. font on a white background. The text will consist from a selection of two characters: one character that is repeated, and one character that appears only once. For instance:

### Output

Output will be an Integer, the index of the different character. In the example above, the output will be 4. (Note that indices of a string start at 0)

### Program Specifications

As per usual for code golf, the shortest program wins.

## Test Cases

=> 10
=> 11
=> 5
=> 16
=> 10
=> 21
=> 20
=> 13
=> 11
=> 4
=> 7

• Pedantic note: your real-world example isn't a puzzle per se. It's more of an amusing eye-game. – Zach Gates Mar 2 '16 at 1:59
• How is this scored? – intboolstring Mar 2 '16 at 2:54
• It's a code golf, so the shortest program that successfully outputs the answer to each of the examples. – AMACB Mar 2 '16 at 2:57
• Note that the shortest answer for some languages may actually be hard-coding the results and choosing one based on something like the size of the input image (which I assume is not allowed). – user81655 Mar 2 '16 at 4:27
• @AMACB "although none are included in the examples", then it might be a good idea to change that ;). Also what's the minimum number of characters we have to handle? (At least 3 I guess, or can we assume it's more than that?) There should also be a test case for that minimum. – Martin Ender Mar 2 '16 at 8:16

# Dyalog APL, 31 32 bytes

{1⍳⍨+⌿∘.≡⍨{⍵/⍨~∧⌿⍵}¨⍵⊂⍨2>/∧⌿1,⍵}


⎕IO←0 to get indices starting with 0 (per OP), and which is anyway default in many APL systems.

1,⍵ prepend a column of white pixels (to ensure margin)
∧⌿ boolean for each column if all-white (vertical AND-reduction)
2>/ boolean at each character's left edge (pair-wise greater-than)
⍵⊂⍨ split into blocks beginning at each TRUE.
{}¨ for each block
∧⌿⍵ boolean for each column if all-white (vertical AND-reduction)
⍵/⍨~ columns that are not [all-white]
∘.≡⍨ match each element to all elements
+⌿ number of blocks identical to each block (vertical plus-reduction)
1⍳⍨ index of first one (i.e. unique element)

Assumes the image is black (0) and white (1) pixels in the matrix I, and that there is at least one all-white pixel column between characters.

      f←{1⍳⍨+⌿∘.≡⍨{⍵/⍨~∧⌿⍵}¨⍵⊂⍨2>/∧⌿1,⍵}


"!I!!":

      ⊢I←6 12⍴(13/1),(22⍴0 1 1),(5/1),0,(8/1),(10⍴0 1 1),13/1
1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 1 1 0 1 1 0 1
1 0 1 1 0 1 1 0 1 1 0 1
1 1 1 1 0 1 1 1 1 1 1 1
1 0 1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1
f I
1


"mmnmm":

      ⊢I←7 31⍴(94/1),0 0,(∊0 1⌽¨2/⊂12⍴6↑1 0 1 1),0 1,(62⍴1 1 1,(⊢,⌽)(14⍴0 1)),33/1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 1 1 0 0 1 0 1 1 0 0 0 1 1 0 0 1 0 1 1 0 0 1 0 1 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
f I
2

• Can't you completely remove the {⍵/⍨~∧⌿⍵}¨ and remain with only {1⍳⍨+⌿∘.≡⍨⍵⊂⍨2>/∧⌿1,⍵}? – lstefano Jun 27 '16 at 12:52
• @lstefano Then it would stop working if there was varying amounts of white-space. – Adám Jun 27 '16 at 13:05
• I see what you mean. – lstefano Jun 27 '16 at 13:13

# Mathematica, 125 bytes

StringCases[#,x:Except[StringCases[#~StringTake~3,x_~~___~~x_:>x][[1]]]:>Position[Characters@#,x]][[1,1,1]]-1&@*TextRecognize


Ahh, Mathemeatica builtins. So amazing. (And so long...) Blows up on |/! :/; ,/. /' and blows up differently on m/n.

• How many inputs does this work for? My copy won't recognise any text in the first example (pipes and exclamation mark) for example. Unless I'm missing something I have identical performance with Length[Split[Characters@TextRecognize@#][[1]]] & – A Simmons Mar 2 '16 at 14:07
• Oops, forgot about Split....And it relies on Mathematica's built in text recognizer...And you can never trust it. – CalculatorFeline Mar 2 '16 at 15:16
• I'll post mine as a separate solution then. – A Simmons Mar 2 '16 at 15:28

# Mathematica, 46 bytes

Length@First@Split@Characters@TextRecognize@#&


Same failures as the other mathematica solution as it relies on the same TextRecognize function.

• Bytesave: Length@First@Split@Characters@TextRecognize@#& – CalculatorFeline Mar 2 '16 at 15:37
• @CatsAreFluffy Cheers – A Simmons Mar 2 '16 at 15:38
• Too bad it's just long enough that @*` doesn't save. – CalculatorFeline Mar 2 '16 at 15:52