Find the different letter

10

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:

Sample Input

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

AMACB

Posted 2016-03-02T01:52:07.957

Reputation: 657

2Pedantic note: your real-world example isn't a puzzle per se. It's more of an amusing eye-game. – Zach Gates – 2016-03-02T01:59:11.670

How is this scored? – intboolstring – 2016-03-02T02:54:34.817

It's a code golf, so the shortest program that successfully outputs the answer to each of the examples. – AMACB – 2016-03-02T02:57:46.697

Will the difference ever be at the beginning? – CalculatorFeline – 2016-03-02T03:01:33.510

Yes, it can be there, although none are included in the examples. – AMACB – 2016-03-02T03:13:38.543

2Note 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 – 2016-03-02T04:27:25.923

1@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 – 2016-03-02T08:16:27.490

(Pity that the input is an image and not a string...) – Luis Mendo – 2016-03-02T10:12:22.210

will there always be at least one completely white column of pixels between characters? – Adám – 2016-03-02T12:06:01.963

Answers

6

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

Adám

Posted 2016-03-02T01:52:07.957

Reputation: 37 779

Can't you completely remove the {⍵/⍨~∧⌿⍵}¨ and remain with only {1⍳⍨+⌿∘.≡⍨⍵⊂⍨2>/∧⌿1,⍵}? – lstefano – 2016-06-27T12:52:20.263

@lstefano Then it would stop working if there was varying amounts of white-space. – Adám – 2016-06-27T13:05:50.387

I see what you mean. – lstefano – 2016-06-27T13:13:20.807

3

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.

CalculatorFeline

Posted 2016-03-02T01:52:07.957

Reputation: 2 608

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 – 2016-03-02T14:07:11.983

Oops, forgot about Split....And it relies on Mathematica's built in text recognizer...And you can never trust it. – CalculatorFeline – 2016-03-02T15:16:04.397

I'll post mine as a separate solution then. – A Simmons – 2016-03-02T15:28:03.243

3

Mathematica, 46 bytes

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

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

A Simmons

Posted 2016-03-02T01:52:07.957

Reputation: 4 005

Bytesave: Length@First@Split@Characters@TextRecognize@#& – CalculatorFeline – 2016-03-02T15:37:39.460

@CatsAreFluffy Cheers – A Simmons – 2016-03-02T15:38:43.370

Too bad it's just long enough that @* doesn't save. – CalculatorFeline – 2016-03-02T15:52:07.560