7
\$\begingroup\$

Some background

Counting rods are small bars (3-14 cm long) that were used by mathematicians from many asian cultures for more than 2000 years to represent any whole number or fraction. (In this chqllenge we'l focus on unsigned integers though) There was also a written version, called rod numerals.

Here's how it works:

(If at any point you get confused, go check out the ascii representation of each digit and some examples I have included at the bottom)

Rod numerals are a true positional numeral system with digits for 1-9 and blank for 0. The digits consist of horizontal and vertical lines; the more lines, the higher the digit. Once you get past five, you put a horizontal line on top to add 5 to the numer of lines below. One vertical line is 1, two vertical lines 2, five vertical lines is 5, one vertical line with a horizontal line on top is 6, four vertical lines with a horizontal line on top is 9 (the highest digit).

A vertical 3 digit:

 ||| 
 ||| 
 ||| 
 ||| 
 ||| 

To make reading rod numerals easier though, they used different notation for each alternating digit. The second notation swaps the role of the horizontal and vertical lines. so that 3 is represented by three horizontal lines and 8 by three horizontal lines with a vertical line on top.

A horizontal 8 digit:

  |  
  |  
__|__
_____
_____

Knowing which notation to use is easy, as previously said, tehy are used alternatingly and Sun Tzu wrote that "one is vertical, ten is horizontal". So the rightmost digit is vertical and we alternate from there on.

The challenge

These rods were used to represent negative numbers and fractions (as explained in the wikipedia article on them. For the purpose of this challenge we'll only focus on positive integers though. The objective is simple:

Write a function or full program that takes an integer value as input in any way and prints the rod numeral representation of this integer to STDOUT (you may also write to a file if that works out better). Shortest code in bytes wins.

Every digit will be represented by 5x5 ascii characters and seperated by two collumns of 5 spaces. The exact representation you'll use for each digit is as follows:

space between two digits (two colums):





0 digit, both vertical and horizontal (five columns):






1 digit, vertical:
  |  
  |  
  |  
  |  
  |  

2 digit, vertical:  
 | | 
 | | 
 | | 
 | | 
 | | 

3 digit, vertical:
 ||| 
 ||| 
 ||| 
 ||| 
 ||| 

4 digit, vertical:
|| ||
|| ||
|| ||
|| ||
|| ||

5 digit, vertical:
|||||
|||||
|||||
|||||
|||||

6 digit, vertical:
_____
  |  
  |  
  |  
  |  

7 digit, vertical:
_____
 | | 
 | | 
 | | 
 | | 

8 digit, vertical:
_____
 ||| 
 ||| 
 ||| 
 ||| 

9 digit, vertical:
_____
|| ||
|| ||
|| ||
|| ||

1 digit, horizontal:




_____

2 digit, horizontal:



_____
_____

3 digit, horizontal:


_____
_____
_____

4 digit, horizontal:

_____
_____
_____
_____

5 digit, horizontal:
_____
_____
_____
_____
_____

6 digit, horizontal:
  |  
  |  
  |  
  |  
__|__

7 digit, horizontal:
  |  
  |  
  |  
__|__
_____

8 digit, horizontal:
  |  
  |  
__|__
_____
_____

9 digit, horizontal:
  |  
__|__
_____
_____
_____

The digits are to be printed next to each other. Trailing spaces beyond the bounding box of the last digit are not allowed. Trailing spaces to complete the bounding box of the last digit(s) are required. You should end the output with a single trailing newline. Leading spaces that do not belong to the bounding box of the first digit are also forbidden.

Standard loopholes apply.

Example output

Lines starting with > are to be interpreted as input.

>12
        | | 
        | | 
        | | 
        | | 
_____   | | 

>8037
  |                  _____
  |                   | | 
__|__         _____   | | 
_____         _____   | | 
_____         _____   | | 

>950
_____  _____       
|| ||  _____       
|| ||  _____       
|| ||  _____       
|| ||  _____       
\$\endgroup\$
  • \$\begingroup\$ by "trailing spaces", do you mean only those past the bounding box of the last digit, or also those at the end of the last digit? \$\endgroup\$ – John Dvorak Sep 3 '14 at 7:17
  • \$\begingroup\$ @JanDvorak only those past the bounding box of the last digit. If trailig spaces are needed to represent the entire bounding box of a digit, they are required. \$\endgroup\$ – overactor Sep 3 '14 at 7:22
  • \$\begingroup\$ Looking at the examples, leading spaces are forbidden too. Are they? \$\endgroup\$ – edc65 Sep 3 '14 at 13:39
  • \$\begingroup\$ @edc65 yes, thanks for pointing that out. \$\endgroup\$ – overactor Sep 3 '14 at 13:40
3
\$\begingroup\$

Python 2 - 216

My first shot, might be some stuff to take out, but my brain hurts, so it's good enough for now

x=raw_input()
for l in range(5):print'  '.join((' '*7+'|   | |  ||| || '+'|'*7+'__|'+'_'*7)[[7*(4-l<n%6+n/6)+(n>5)*(l<10-n)-(l==10-n),n%6+n/6+(l<1)*(n>5)*(12-n)][(len(x)-i)%2]*5:][:5]for i,n in enumerate(map(int,x)))
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6) 223

Function with numeric parameter, output to console. NB If the input parameter could be a string, the code would be 5 char shorter and without the limit of 17 significative digits of JS numbers.

F=n=>{
  for(r=s='',n+=s;r<5;r++,s+=q)
    for(f=q='\n',p=n.length;f=!f,p--;q=(p?'  ':'')+'     1  |  1 | | 1 ||| 1|| ||1|||||1_____1__|__'.split(1)[d]+q)
      if(d=~~n[p])e=d+r,d=d>5?f?e<10?1:e>10?6:7:r?d-5:6:f?e>4?6:0:d;
  console.log(s)
}

Test

Test in Firefox console.

F(12)

Output

        | | 
        | | 
        | | 
        | | 
_____   | | 

F(8037)

Output

  |                  _____
  |                   | | 
__|__         _____   | | 
_____         _____   | | 
_____         _____   | | 

F(950)

Output

_____  _____       
|| ||  _____       
|| ||  _____       
|| ||  _____       
|| ||  _____       

Ungolfed

F=n=>{
  z='     1  |  1 | | 1 ||| 1|| ||1|||||1_____1__|__'.split(1);
  s='';
  n+=s;

  for (r = 0; r < 5; r++)
  {
    for(q='\n',f=1,p=n.length;f=!f,p--;)
    {
      d = ~~n[p];
      if (d)
      {
        e=d+r;
        if (d > 5)
        {
          if (f)
          {
            d = e < 10 ? 1 : e >10 ? 6 : 7;
          }
          else
          {
            d = r ? d-5 : 6;
          }
        }
        else
        {
          if (f)
            d = e > 4 ? 6 : 0;
        }
      }
      q = (p ? '  ' : '') + z[d] + q;
    }
    s+=q
  }

  console.log(s)
}
\$\endgroup\$

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.