13
\$\begingroup\$

Courier Ception

The program must accept any string as input and output an pixel image that shows the input string in Courier. All the letters that contain a 'hole' (like abdegopqABDPQR etc) that is surrounded by black pixels must be also filled black.

Input

The Program must be able to accept any ASCII string as input. The input can be any way you want, as long as the program code itself does not have to change in order to accept a different input. (Except for e.g. the filename of the file that is to be read.) No standard loopholes. You can assume that each input contains at least one printable letter.

Output

The output must be a black and white (no gray) pixel graphic that shows the string written in Courier (in black, background white), with the specified 'holes' filled. The fontsize of the whole string must be constant (that means no different scaling for each different letters) so that the full sized letters (e.g. ABCDEFGHIJKLMNOPRSTUVWXYZ but j and Q are bigger) must be at least 10px in height. (You do not have to write it to a file, any kind of display is ok as long as it is generated as pixelgraphics, as e.g. canvas in JavaScript.) Please post this graphic with your answer.

Access to courier.ttf / font libraries is allowed.

The program must as well count the number of black pixels and write it to console or what ever output method prefered in the end.

Score

The score is evaluated as follows: The full program code must be used as input string to your program. The number of black pixels will be your score. Program code that contains non-printable or non-ASCII letters is not allowed. (As well as standard loopholes.) The lower the score the better.

\$\endgroup\$
  • \$\begingroup\$ Output is specified as b/w, so no grey pixels allowed. And thank you for asking about the encoding, this is gonna be limited to ASCII because of the font. \$\endgroup\$ – flawr Jul 18 '14 at 19:40
  • 1
    \$\begingroup\$ There are at least two serious problems which need clarification. Firstly, it's not clear whether the program is allowed access to a Courier.ttf and to font libraries which can use it. Secondly, do you seriously intend to prohibit programs which don't contain non-ASCII characters? \$\endgroup\$ – Peter Taylor Jul 18 '14 at 19:59
  • \$\begingroup\$ Yes, access to Courier.ttf and font libraries is allowed - it wouldn't make sense otherwise, thank you. I was not able to come up with any major language that does rely on non-ASCII characters - do you know any languages that need non-ASCII characters? \$\endgroup\$ – flawr Jul 18 '14 at 21:51
  • 1
    \$\begingroup\$ @flawr APL. And in Mathematica you could shorten a few things using Unicode characters. \$\endgroup\$ – Martin Ender Jul 18 '14 at 21:53
  • \$\begingroup\$ Is ImageMagick allowed? \$\endgroup\$ – Digital Trauma Jul 18 '14 at 22:20
12
\$\begingroup\$

Mathematica, 4864 pixels

l = ImageData[Binarize[Rasterize[Style[j, FontSize -> 15]], .71]]
i = {{1, 1}}
While[Length[i] > 1 - 1,
 {r, c} = j = i[[1]]; l[[r, c]] = 2; i = i[[2 ;; -1]];
 If[FreeQ[i, {r, c} = J = j + #] && l[[r, c]] == 1, 
    i = i~Join~{J}] & /@
  {{1, 1 - 1}, {1 - 1, 1}, {-1, 1 - 1}, {1 - 1, -1}}
 ]
Image[l = l /. 1 -> 1 - 1 /. 2 -> 1]
Count[l, 1 - 1, {2}]

Here is the picture:

enter image description here

In Mathematica when you write a "program" you just write a snippet. So this expects the input stored in j and the last thing it returns is the image and the count. This also spits out a bunch of errors, because I don't do bounds checking on l, but it produces the desired result anyway.

where % refers to said last output.

Thanks to Geobits for the idea for the algorithm. I'm flood-filling the image from the top-left corner with an invalid intensity, then I replace all remaining white pixels with black pixels, and the invalid ones with white ones.

Note that the FreeQ check isn't actually necessary for the program to work correctly, but for it to finish in a reasonable amount of time. If I'd leave it out, I'd actually score about 300 pixels less.

\$\endgroup\$
  • \$\begingroup\$ The counting should be part of the program itself! Other than that it's a nice solution, did you calculate which letters (as variable names) use the least number of pixels? \$\endgroup\$ – flawr Jul 18 '14 at 21:55
  • \$\begingroup\$ @flawr yes :) (now for all of them) (fixing the counting in a second) \$\endgroup\$ – Martin Ender Jul 18 '14 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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