9
\$\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\$
4
  • \$\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\$ Commented Sep 3, 2014 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
    Commented Sep 3, 2014 at 7:22
  • \$\begingroup\$ Looking at the examples, leading spaces are forbidden too. Are they? \$\endgroup\$
    – edc65
    Commented Sep 3, 2014 at 13:39
  • \$\begingroup\$ @edc65 yes, thanks for pointing that out. \$\endgroup\$
    – overactor
    Commented Sep 3, 2014 at 13:40

3 Answers 3

4
\$\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\$
0
1
\$\begingroup\$

PowerShell, 229 bytes

$n="$args"
0..4|%{$l=$_;$h=$n.Length%2
($n|% t*y|%{(' '*7+'|   | |  ||| || '+'|'*7+'_'*7+'|__')|% S*g(5*(('0123456666'+'0123451234'*4+'00000611110000661117000666117600666617660666667666')[50*($h=!$h)+10*$l+"$_"]-48))5})-join'  '}

Try it online!

Where this string present digits:

# 0       1       2       3       4       5       6       7
# '     '.'  |  ',' | | ',' ||| ','|| ||','|||||','_____','__|__'

vertical rods:

"       |   | |  ||| || |||||||____________________"+   # 0123456666
"       |   | |  ||| || |||||||  |   | |  ||| || ||"+   # 0123451234
"       |   | |  ||| || |||||||  |   | |  ||| || ||"+   # 0123451234
"       |   | |  ||| || |||||||  |   | |  ||| || ||"+   # 0123451234
"       |   | |  ||| || |||||||  |   | |  ||| || ||"    # 0123451234

horizontal rods:

"                         _____  |    |    |    |  "+   # 0000061111
"                    __________  |    |    |  __|__"+   # 0000661117
"               _______________  |    |  __|_______"+   # 0006661176
"          ____________________  |  __|____________"+   # 0066661766
"     ___________________________|_________________"    # 0666667666

Unrolled script:

$digits=' '*7+'|   | |  ||| || '+'|'*7+'_'*7+'|__'
$positions = '0123456666'+'0123451234'*4+'00000611110000661117000666117600666617660666667666'

$n="$args"
0..4|%{
    $line=$_
    $horizontal=$n.Length%2
    $chunks=$n|% toCharArray|%{
        $horizontal=!$horizontal
        $startFrom = 5*($positions[50*$horizontal+10*$line+"$_"]-48)
        $digits|% Substring $startFrom 5
    }
    $chunks-join'  '
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 227 if you take it as a string \$\endgroup\$
    – Veskah
    Commented Jun 12, 2019 at 13:20
  • \$\begingroup\$ What is 'any way' in the rule Write a function or full program that takes an integer value as input in any way? I did not dare to interpret 'an integer' as 'a string'. :) \$\endgroup\$
    – mazzy
    Commented Jun 12, 2019 at 13:29
  • \$\begingroup\$ Yeah, it's a bit vague and probably not what OP intended but figured I'd point out the easy golf. \$\endgroup\$
    – Veskah
    Commented Jun 12, 2019 at 13:34

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.