1
\$\begingroup\$

Years ago I read the "Best of Ruby Quiz" book by James Edward Gray II (also available online by now). For problem 2 in the book (which is problem 14 on his original RubyQuiz site), he mentions a golfed solution in under 300 bytes, even though he is not a big fan of golfing.

So I did wonder if I could beat that and maybe even find a solution of under 200 bytes. So once I beat the current best solution of 256 bytes I sent him a 210 bytes solution. Shortly thereafter I found a solution around 190 bytes, but didn't bother to send him that. But over the years I lost these 190 bytes somehow and couldn't find a backup anymore. In any case I know that a under 200 byte solution is possible in ruby (at least with ruby 1.8.7) so I hope some of you will be able to recreate it or maybe even find something better.

Even though I'm mostly interested in ruby solution other language solutions are of course also welcome!

The Challenge

Create a program which is called on the commandline and takes a non-negative integer as an argument and an optional -s argument followed by a positive integer. The default value for -s is 2.

The output should be the input argument in LCD-style adjusted by size by the -s parameter.

For example if the program is called like this

$ lcd.rb 012345

the correct display is this:

 --        --   --        --
|  |    |    |    | |  | |
|  |    |    |    | |  | |
           --   --   --   --
|  |    | |       |    |    |
|  |    | |       |    |    |
 --        --   --        --

And for this:

$ lcd.rb -s 1 6789

your program should print this:

 -   -   -   -
|     | | | | |
 -       -   -
| |   | | |   |
 -       -   -

Note the single column of space between digits in both examples. For other values of -s, simply lengthen the - and | bars.

Of course because this is code golf I have to be more specific than the original quiz about some rules:

  • It's ok if your program requires to be explicitly called by an interpreter (e.g. ruby lcd.rb 1234), so a shebang line is not required
  • It's not required to handle other than the above specified arguments in a sane way (e.g. negative numbers, no space between -s and the following number, extra arguments, or no arguments at all)
  • It should in principle be able to handle any number of digits and any positive size for the scale parameter -s. So only external factors like the maximum command line length of the shell should limit these parameters.
  • Non-printable characters are allowed, but your program must consist of the 128 characters of the ASCII set.

Have fun!

\$\endgroup\$
  • \$\begingroup\$ Thanks for the reference. But I can't see a ruby solution under <200 bytes there, so my question is still open. And doing the parameter parsing is an important aspect of the problem. \$\endgroup\$ – David Ongaro Feb 19 '15 at 7:13
  • \$\begingroup\$ This should have tag ruby. \$\endgroup\$ – freekvd Feb 19 '15 at 8:27
  • \$\begingroup\$ I thought about that but thought it might discourage other language submissions. But I added it now anyway, because of my ruby specific question. But I still hope people don't think answers should be limited to ruby. \$\endgroup\$ – David Ongaro Feb 19 '15 at 8:31
  • \$\begingroup\$ I'm sorry, but I think the input parsing is not a sufficiently substantial difference to not make this a duplicate. The core challenge is encoding the digits in a scalable way, and that is the same in both challenges. Since you're specifically looking for a short Ruby answer, I'd say the way to go is to offer a bounty on the old question instead of asking it again. And lastly, I think you had it right, the Ruby tag is misplaced here, as it suggests that you're looking for Ruby answers only. \$\endgroup\$ – Martin Ender Feb 19 '15 at 11:00
  • \$\begingroup\$ @MartinBüttner the biggest difference isn't the input parsing, it's that the other one is a popularity contest whereas this is codegolf. However, bounty awarders get to decide their own criteria, distinct from the OP's, so I guess your suggestion is right. \$\endgroup\$ – Level River St Feb 19 '15 at 14:40