10
\$\begingroup\$

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

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Pedantic note: your real-world example isn't a puzzle per se. It's more of an amusing eye-game. \$\endgroup\$
    – Zach Gates
    Commented Mar 2, 2016 at 1:59
  • \$\begingroup\$ How is this scored? \$\endgroup\$ Commented Mar 2, 2016 at 2:54
  • \$\begingroup\$ It's a code golf, so the shortest program that successfully outputs the answer to each of the examples. \$\endgroup\$
    – AMACB
    Commented Mar 2, 2016 at 2:57
  • 2
    \$\begingroup\$ 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). \$\endgroup\$
    – user81655
    Commented Mar 2, 2016 at 4:27
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 2, 2016 at 8:16

3 Answers 3

6
\$\begingroup\$

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

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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]]] & \$\endgroup\$
    – A Simmons
    Commented Mar 2, 2016 at 14:07
  • \$\begingroup\$ Oops, forgot about Split....And it relies on Mathematica's built in text recognizer...And you can never trust it. \$\endgroup\$ Commented Mar 2, 2016 at 15:16
  • \$\begingroup\$ I'll post mine as a separate solution then. \$\endgroup\$
    – A Simmons
    Commented Mar 2, 2016 at 15:28
3
\$\begingroup\$

Mathematica, 46 bytes

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

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

\$\endgroup\$
3
  • \$\begingroup\$ Bytesave: Length@First@Split@Characters@TextRecognize@#& \$\endgroup\$ Commented Mar 2, 2016 at 15:37
  • \$\begingroup\$ @CatsAreFluffy Cheers \$\endgroup\$
    – A Simmons
    Commented Mar 2, 2016 at 15:38
  • \$\begingroup\$ Too bad it's just long enough that @* doesn't save. \$\endgroup\$ Commented Mar 2, 2016 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.