32
\$\begingroup\$

Write the shortest program that takes a single integer as input and prints out a Suanpan abacus

Testcases

Input:

314159

Output:

|\======================================/|
||  (__)  (__)  (__)  (__)  (__)  (__)  ||
||  (__)  (__)  (__)  (__)   ||    ||   ||
||   ||    ||    ||    ||    ||    ||   ||
||   ||    ||    ||    ||   (__)  (__)  ||
|<======================================>|
||  (__)  (__)  (__)  (__)   ||   (__)  ||
||  (__)   ||   (__)   ||    ||   (__)  ||
||  (__)   ||   (__)   ||    ||   (__)  ||
||   ||    ||   (__)   ||    ||   (__)  ||
||   ||    ||    ||    ||    ||    ||   ||
||   ||    ||    ||    ||   (__)   ||   ||
||   ||   (__)   ||   (__)  (__)   ||   ||
||   ||   (__)   ||   (__)  (__)   ||   ||
||  (__)  (__)   ||   (__)  (__)   ||   ||
||  (__)  (__)  (__)  (__)  (__)  (__)  ||
|/======================================\|

Input:

6302715408

Output:

|\==============================================================/|
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  ||
||   ||   (__)  (__)  (__)   ||   (__)   ||   (__)  (__)   ||   ||
||   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   ||
||  (__)   ||    ||    ||   (__)   ||   (__)   ||    ||   (__)  ||
|<==============================================================>|
||  (__)  (__)   ||   (__)  (__)  (__)   ||   (__)   ||   (__)  ||
||   ||   (__)   ||   (__)  (__)   ||    ||   (__)   ||   (__)  ||
||   ||   (__)   ||    ||    ||    ||    ||   (__)   ||   (__)  ||
||   ||    ||    ||    ||    ||    ||    ||   (__)   ||    ||   ||
||   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   ||
||   ||    ||   (__)   ||    ||    ||   (__)   ||   (__)   ||   ||
||  (__)   ||   (__)   ||    ||   (__)  (__)   ||   (__)   ||   ||
||  (__)   ||   (__)  (__)  (__)  (__)  (__)   ||   (__)   ||   ||
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)   ||   (__)  (__)  ||
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  ||
|/==============================================================\|
\$\endgroup\$
7
  • \$\begingroup\$ Can the number be given on the command-line? \$\endgroup\$
    – Joey
    Commented Mar 12, 2011 at 1:04
  • \$\begingroup\$ Any restrictions on input length? \$\endgroup\$
    – Joey
    Commented Mar 12, 2011 at 2:07
  • \$\begingroup\$ Similar to golf.shinh.org/p.rb?Soroban+Fixed if anyone needs some ideas on how to golf more. \$\endgroup\$
    – Nabb
    Commented Mar 12, 2011 at 4:02
  • 1
    \$\begingroup\$ So the top row and the bottom row are always completely filled? Why did they invent such a redundant abacus? :) \$\endgroup\$
    – Timwi
    Commented Mar 13, 2011 at 16:44
  • \$\begingroup\$ @Timwi, the same abacus can be used for hexidecimal. When used for decimal the extra rows are mostly used when performing multiplications and divisions \$\endgroup\$
    – gnibbler
    Commented Mar 14, 2011 at 4:44

14 Answers 14

15
+100
\$\begingroup\$

J, 126 124 121 119 116 115 113 105 116 115 112 characters

'|'s":(' 's[:,.(_6[\'  ||   (__)'){~(,-.))&.>,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1[(s=:[,.~,.)9!:7'\=/<=>/=\|='

Takes input from the keyboard. Example:

   '|'s":(' 's[:,.(_6[\'  ||   (__)'){~(,-.))&.>,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1[(s=:[,.~,.)9!:7'\=/<=>/=\|='
6302715408
|\==============================================================/|
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  ||
||   ||   (__)  (__)  (__)   ||   (__)   ||   (__)  (__)   ||   ||
||   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   ||
||  (__)   ||    ||    ||   (__)   ||   (__)   ||    ||   (__)  ||
|<==============================================================>|
||  (__)  (__)   ||   (__)  (__)  (__)   ||   (__)   ||   (__)  ||
||   ||   (__)   ||   (__)  (__)   ||    ||   (__)   ||   (__)  ||
||   ||   (__)   ||    ||    ||    ||    ||   (__)   ||   (__)  ||
||   ||    ||    ||    ||    ||    ||    ||   (__)   ||    ||   ||
||   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   ||
||   ||    ||   (__)   ||    ||    ||   (__)   ||   (__)   ||   ||
||  (__)   ||   (__)   ||    ||   (__)  (__)   ||   (__)   ||   ||
||  (__)   ||   (__)  (__)  (__)  (__)  (__)   ||   (__)   ||   ||
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)   ||   (__)  (__)  ||
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  ||
|/==============================================================\|

The main trick here is the abuse of J's boxing by redefining the characters it uses. It uses a global parameter - 9!:7 - to do this. There may be room for further golfing, but to be honest I was just glad to get something working that should compensate for my last attempt at this question.

Fits in a tweet with enough characters left to say 'Gaz made this' :-).

Edit: 3 characters of savings are due to borrowing the 2 6$' || (__)' from Jesse Millikan's answer.

Further edit: Lost 11 characters adding extra spaces at either side that I hadn't noticed were not present.

Explanation:

The code is in three main sections:

1) Setup

[(s=:[,.~,.)9!:7'\=/<=>/=\|='

This is itself in two parts.
9!:7'\=/<=>/=\|=' redefines the characters that J will use to display boxes. J's boxing normally looks like this:

   2 2$<"0[1 2 3 4
┌─┬─┐
│1│2│
├─┼─┤
│3│4│
└─┴─┘

but after redefining it looks like this:

   2 2$<"0[1 2 3 4
\===/
|1|2|
<===>
|3|4|
/===\

(s=:[,.~,.) defines a verb I'm going to be using a couple of times later. This turns out to be the best place to declare it. It takes a character on the left and an array of characters on the right and sandwiches the array between the character. For example:

   3 5$'abcdefghijklmno'
abcde
fghij
klmno

   '-' s 3 5$'abcdefghijklmno'
-abcde-
-fghij-
-klmno-

The final [ just serves to separate the setup from the next part.

2) Input and Representation

,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1

".,.1!:1[1 takes the input from the keyboard and separates it into individual digits:

   ".,.1!:1[1
314159
3 1 4 1 5 9

((i.5)</5|]) creates a zeros and ones representation of the bottom part of the abacus:

   ((i.5)</5|]) 3 1 4 1 5 9
1 1 1 1 0 1
1 0 1 0 0 1
1 0 1 0 0 1
0 0 1 0 0 1
0 0 0 0 0 0

|:@(1,.<&5) creates a zeroes and ones representation of the top part of the abacus:

   |:@(1,.<&5) 3 1 4 1 5 9
1 1 1 1 1 1
1 1 1 1 0 0

These two parts are boxed together using ;:

   (|:@(1,.<&5);((i.5)</5|])) 3 1 4 1 5 9
\=======================/
|1 1 1 1 1 1|1 1 1 1 0 1|
|1 1 1 1 0 0|1 0 1 0 0 1|
|           |1 0 1 0 0 1|
|           |0 0 1 0 0 1|
|           |0 0 0 0 0 0|
/=======================\

Then the boxes are placed one on top of the other to form the basis of the abacus, giving:

   ,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1
314159
\===========/
|1 1 1 1 1 1|
|1 1 1 1 0 0|
<===========>
|1 1 1 1 0 1|
|1 0 1 0 0 1|
|1 0 1 0 0 1|
|0 0 1 0 0 1|
|0 0 0 0 0 0|
/===========\

3) Output

'|'s":(' 's[:,.(_6[\'  ||   (__)'){~(,-.))&.>

&.> means that what follows will operate on both boxes in turn.
(,-.) this is a hook which will negate the input and then add it to the end of the original:

   2 5$1 0 1 0 1
1 0 1 0 1
1 0 1 0 1

   (,-.) 2 5$1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
0 1 0 1 0
0 1 0 1 0

This is really part of the representation, but for golfing purposes it's better to have it in this section. Applied to the previous input:

   (,-.)&.>,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1
314159
\===========/
|1 1 1 1 1 1|
|1 1 1 1 0 0|
|0 0 0 0 0 0|
|0 0 0 0 1 1|
<===========>
|1 1 1 1 0 1|
|1 0 1 0 0 1|
|1 0 1 0 0 1|
|0 0 1 0 0 1|
|0 0 0 0 0 0|
|0 0 0 0 1 0|
|0 1 0 1 1 0|
|0 1 0 1 1 0|
|1 1 0 1 1 0|
|1 1 1 1 1 1|
/===========\

[:,.(_6[\' || (__)'){~ The zeroes and ones are now used to select a string to represent a bead or the absence of one:

   ([:,.(_6[\'  ||   (__)'){~(,-.))&.>,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1
314159
\====================================/
| (__)  (__)  (__)  (__)  (__)  (__) |
| (__)  (__)  (__)  (__)   ||    ||  |
|  ||    ||    ||    ||    ||    ||  |
|  ||    ||    ||    ||   (__)  (__) |
<====================================>
| (__)  (__)  (__)  (__)   ||   (__) |
| (__)   ||   (__)   ||    ||   (__) |
| (__)   ||   (__)   ||    ||   (__) |
|  ||    ||   (__)   ||    ||   (__) |
|  ||    ||    ||    ||    ||    ||  |
|  ||    ||    ||    ||   (__)   ||  |
|  ||   (__)   ||   (__)  (__)   ||  |
|  ||   (__)   ||   (__)  (__)   ||  |
| (__)  (__)   ||   (__)  (__)   ||  |
| (__)  (__)  (__)  (__)  (__)  (__) |
/====================================\

But now, as Howard pointed out to me, there is one space short on either side of the beads. So we use the predefined s verb to sandwich the contents of each box between two columns of spaces:

   (' 's[:,.(_6[\'  ||   (__)'){~(,-.))&.>,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1
314159
\======================================/
|  (__)  (__)  (__)  (__)  (__)  (__)  |
|  (__)  (__)  (__)  (__)   ||    ||   |
|   ||    ||    ||    ||    ||    ||   |
|   ||    ||    ||    ||   (__)  (__)  |
<======================================>
|  (__)  (__)  (__)  (__)   ||   (__)  |
|  (__)   ||   (__)   ||    ||   (__)  |
|  (__)   ||   (__)   ||    ||   (__)  |
|   ||    ||   (__)   ||    ||   (__)  |
|   ||    ||    ||    ||    ||    ||   |
|   ||    ||    ||    ||   (__)   ||   |
|   ||   (__)   ||   (__)  (__)   ||   |
|   ||   (__)   ||   (__)  (__)   ||   |
|  (__)  (__)   ||   (__)  (__)   ||   |
|  (__)  (__)  (__)  (__)  (__)  (__)  |
/======================================\

Having done that, all that's left is to convert this into a string using ": so that we can sandwich it between two columns of |:

   '|'s":(' 's[:,.(_6[\'  ||   (__)'){~(,-.))&.>,.(|:@(1,.<&5);((i.5)</5|]))".,.1!:1[1
314159
|\======================================/|
||  (__)  (__)  (__)  (__)  (__)  (__)  ||
||  (__)  (__)  (__)  (__)   ||    ||   ||
||   ||    ||    ||    ||    ||    ||   ||
||   ||    ||    ||    ||   (__)  (__)  ||
|<======================================>|
||  (__)  (__)  (__)  (__)   ||   (__)  ||
||  (__)   ||   (__)   ||    ||   (__)  ||
||  (__)   ||   (__)   ||    ||   (__)  ||
||   ||    ||   (__)   ||    ||   (__)  ||
||   ||    ||    ||    ||    ||    ||   ||
||   ||    ||    ||    ||   (__)   ||   ||
||   ||   (__)   ||   (__)  (__)   ||   ||
||   ||   (__)   ||   (__)  (__)   ||   ||
||  (__)  (__)   ||   (__)  (__)   ||   ||
||  (__)  (__)  (__)  (__)  (__)  (__)  ||
|/======================================\|
\$\endgroup\$
6
  • \$\begingroup\$ I felt there was something "wrong" with your output but it took me some time to recognize: you omit an empty column on the very left and right of the display (third and third-last ascii column). \$\endgroup\$
    – Howard
    Commented Feb 9, 2014 at 12:48
  • \$\begingroup\$ @Howard Woah, good spot. I didn't notice that. I'll have a look at fixing it. \$\endgroup\$
    – Gareth
    Commented Feb 9, 2014 at 13:00
  • \$\begingroup\$ @Howard Fixed at a cost of 11 characters. Shame, I thought I might have a chance at getting below 100. :-( \$\endgroup\$
    – Gareth
    Commented Feb 9, 2014 at 13:05
  • \$\begingroup\$ Nice work. Looks like @Howard has a lot of work to do to catch up \$\endgroup\$
    – gnibbler
    Commented Feb 10, 2014 at 0:10
  • \$\begingroup\$ Bounty has 3 days left. I've marked this one as accepted for the mean time. \$\endgroup\$
    – gnibbler
    Commented Feb 12, 2014 at 4:12
14
\$\begingroup\$

Ruby 1.9, 154 characters

puts'|\%s/|'%$r=?=*(2+6*gets.size),(0..14).map{|a|a==4?"|<#$r>|":"|| #{$_.gsub(/./){(5*a+n=$&.hex)/10!=1&&(a-n%5)/5!=1?' (__) ':'  ||  '}} ||"},"|/#$r\\|"

Assumes the input is not terminated by a newline.

Fun fact: Due to the way I'm turning the input digits into numbers ($&.hex is one byte shorter than $&.to_i), this abacus actually works with hex digits up to e:

$ echo -n "0123456789abcdef" | ruby suanpan.rb
|\==================================================================================================/|
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)   ||    ||    ||    ||    ||    ||   ||
||  (__)  (__)  (__)  (__)  (__)   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   (__)  ||
||   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   (__)  (__)  (__)  (__)  (__)  (__)  ||
||   ||    ||    ||    ||    ||   (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  ||
|<==================================================================================================>|
||   ||   (__)  (__)  (__)  (__)   ||   (__)  (__)  (__)  (__)   ||   (__)  (__)  (__)  (__)   ||   ||
||   ||    ||   (__)  (__)  (__)   ||    ||   (__)  (__)  (__)   ||    ||   (__)  (__)  (__)   ||   ||
||   ||    ||    ||   (__)  (__)   ||    ||    ||   (__)  (__)   ||    ||    ||   (__)  (__)   ||   ||
||   ||    ||    ||    ||   (__)   ||    ||    ||    ||   (__)   ||    ||    ||    ||   (__)   ||   ||
||   ||    ||    ||    ||    ||    ||    ||    ||    ||    ||    ||    ||    ||    ||    ||    ||   ||
||  (__)   ||    ||    ||    ||   (__)   ||    ||    ||    ||   (__)   ||    ||    ||    ||   (__)  ||
||  (__)  (__)   ||    ||    ||   (__)  (__)   ||    ||    ||   (__)  (__)   ||    ||    ||   (__)  ||
||  (__)  (__)  (__)   ||    ||   (__)  (__)  (__)   ||    ||   (__)  (__)  (__)   ||    ||   (__)  ||
||  (__)  (__)  (__)  (__)   ||   (__)  (__)  (__)  (__)   ||   (__)  (__)  (__)  (__)   ||   (__)  ||
||  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  (__)  ||
|/==================================================================================================\|

For 0xf, a third bead magically appears in the upper half.

When allowing additional flags during script invocation, this can be shortened to 152 bytes (149 bytes code + 3 bytes additional invocation flags):

puts'|\%s/|'%$r=?=*(2+6*~/$/),(0..14).map{|a|a==4?"|<#$r>|":"|| #{$_.gsub(/./){(5*a+n=$&.hex)/10!=1&&(a-n%5)/5!=1?' (__) ':'  ||  '}} ||"},"|/#$r\\|"

Run with ruby -n suanpan.rb.

\$\endgroup\$
12
\$\begingroup\$

Perl (151 chars)

(168 163 158 157 156 154)

$i=<>;$c.=$_-4?"
||  $i||":"
|<$m>|",$m='==',$c=~s!\d!$m.='='x6;($_-$&%5)/5%2|(5*$_+$&)/10%7==1?' ||   ':'(__)  '!eg for 0..14;print"|\\$m/|$c
|/$m\\|"

Explanation

# Read the number from STDIN.
$i = <>;

# for statement starts here...

    # Append to $c a line containing either the horizontal dividing bar (in row #4)
    # or the current abacus row with the digits in place of the pegs.
    # This relies on the fact that $m will have been computed after at least one iteration.
    $c .= $_-4 ? "\n||  $i||" : "\n|<$m>|",

    # Notice that $m is redundantly recomputed from scratch in each iteration.
    $m = '==',

    # Substitute the correct pegs for the digit characters.
    $c =~ s!\d!
        $m .= '=' x 6;

        # Weird expression for the abacus function.
        # I have to use “% 7” because otherwise the division is floating-point...
        # Notice that $_ is the row and $& is the digit.
        ($_ - $& % 5)/5 % 2 | (5*$_ + $&)/10 % 7 == 1
        ? ' ||   '
        : '(__)  '
    !eg
for 0..14;

# Could shorten further by using “say” if you don’t mind excluding the “-E” from the count...
print "|\\$m/|$c\n|/$m\\|"

Edits

  • (154 → 151) Changed three \ns to actual newline characters. Can’t believe I didn’t think of that earlier!
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is just... Awesome! \$\endgroup\$
    – german_guy
    Commented Feb 11, 2014 at 13:59
8
\$\begingroup\$

Windows PowerShell, 191

$y='='*(2+6*($i=[char[]]"$input").Count)
filter f($v){"|| $((' (__)','  || ')[($i|%{iex $_%$v})])  ||"}"|\$y/|"
f 1
f 10-ge5
f 1+1
f 10-lt5
"|<$y>|"
1..5|%{f 5-lt$_}
1..5|%{f 5-ge$_}
"|/$y\|"

History:

  • 2011-03-11 23:54 (340) Initial attempt.
  • 2011-03-12 00:21 (323) Using string interpolation throughout the code.
  • 2011-03-12 00:21 (321) Inlined $l.
  • 2011-03-12 01:07 (299) Used a function for the more repetitive parts as well as a format string.
  • 2011-03-12 01:19 (284) Changed the arguments to the function slightly. Hooray for command parsing mode.
  • 2011-03-12 01:22 (266) More variables for recurring expressions.
  • 2011-03-12 01:28 (246) Now every row is generated by the function.
  • 2011-03-12 01:34 (236) Since I use the chars only in string interpolation I can safely ignore the % that makes numbers from the digits.
  • 2011-03-12 01:34 (234) Slightly optimized the array index generation in the function.
  • 2011-03-12 01:42 (215) I no longer need $r and $b. And $a is also obsolete. As is $l.
  • 2011-03-12 01:46 (207) No need to set $OFS if I need it only once.
  • 2011-03-12 01:49 (202) Inlined $f.
  • 2011-03-12 01:57 (200) No need for the format string anymore. String interpolation works just fine.
  • 2011-03-12 02:00 (198) Slightly optimized generating the individual rows (re-ordering the pipeline and array index).
  • 2011-03-12 02:09 (192) No need for -join since we can actually use the additional space to good effect.
\$\endgroup\$
5
\$\begingroup\$

Haskell, 243 characters

z x|x=" (__) ";z _="  ||  "
h[a,b]d f w='|':a:replicate(2+6*length d)'='++b:"|\n"++q z++q(z.not)
 where q b=w>>=(\v->"|| "++(d>>=b.(>v).f)++" ||\n")
s d=h"\\/"d(5-)[-9,0]++h"><"d(`mod`5)[0..4]++h"/\\"d id[]
main=interact$s.map(read.(:[])).init

Not particularly clever. I'm sure it can be shortened somehow...


  • Edit: (246 -> 243) took @FUZxxl's suggestion to use interact
\$\endgroup\$
4
  • \$\begingroup\$ How about using interact \$\endgroup\$
    – FUZxxl
    Commented Mar 23, 2011 at 19:33
  • \$\begingroup\$ First line can be shortend to z x|x=" (__) "|0<1=" || ". \$\endgroup\$
    – FUZxxl
    Commented Mar 23, 2011 at 20:33
  • \$\begingroup\$ Your alternative first line is only shorter because you dropped two spaces that are required! \$\endgroup\$ Commented Mar 24, 2011 at 2:07
  • \$\begingroup\$ Oops! You're of course right. \$\endgroup\$
    – FUZxxl
    Commented Mar 24, 2011 at 18:42
4
\$\begingroup\$

Delphi, 348

This version builds up a string to write just once; The digits are handled by a separate function that works via a digit modulo m >= value construct (negated if value <0).

var d,s,o,p:string;c:Char;i:Int8;function g(m,v:Int8):string;begin p:='|| ';for c in d do p:=p+Copy('  ||   (__) ',1+6*Ord(((Ord(c)+2)mod m>=Abs(v))=(v>0)),6);g:=p+' ||'^J;end;begin ReadLn(d);s:=StringOfChar('=',2+6*Length(d));for i:=1to 5do o:=g(5,6-i)+o+g(5,-i);Write('|\'+s+'/|'^J+g(1,-1)+g(10,-5)+g(1,1)+g(10,5)+'|<'+s+'>|'^J+o+'|/'+s+'\|')end.

Delphi, 565

First attempt :

var _:array[0..6]of string=('  ||  ',' (  ) ','======','|\==/|','||  ||','|/==\|','|<==>|');m:array[0..186]of Byte;o:array[0..16]of string;i,j,c,f,l:Word;begin for i:=0to 9do begin f:=i*17;m[f+1]:=1;m[f+2]:=Ord(i<5);m[f+3]:=0;m[f+4]:=Ord(i>4);for j:=6to 10do m[f+j]:=Ord(i mod 5>j-6);for j:=11to 15do m[f+j]:=Ord(i mod 5<=j-11);m[f]:=2;m[5+f]:=2;m[16+f]:=2;end;f:=170;m[f]:=3;for i:=1to 15do m[f+i]:=4;m[f+5]:=6;m[f+16]:=5;repeat for i:=0to 16do Insert(_[m[f+i]],o[i],l);Read(PChar(@c)^);c:=c-48;f:=c*17;l:=Length(o[0])-2;until c>9;for i:=0to 16do WriteLn(o[i])end.

This uses 3 arrays; one for the 7 strings that could be discerned, one for the output lines and one to map the 7 strings to 11 columns (10 digits and 1 initial column).

\$\endgroup\$
3
\$\begingroup\$

GolfScript, 139 characters

0+17'|':Q*:R'\<||'4Q**'/'+@{48-.5<)[1]*2,2*$+4<\5%[1]*2,5*$+10<+`{{1$=}%+2>'=='1/*5/('='+\+}+'     (|_|_ )'2//}/;;;;'/>||'4Q**'\\'+R]zip n*

Not much golfed yet, but it fits into a tweet (with only ASCII). Try it here.

\$\endgroup\$
2
\$\begingroup\$

J, 225

Passes two tests given, should work up to at least several hundred digits.

c=:2 6$'   ||   (__)'
f=:(2{.[),('='#~2+6*#@]),2}.[
d=:'||',"1'  ||',~"1,"2&(c{~|:)
g=:('|\/|'&f,d&(1,.-.,.0,.])&(4&<),'|<>|'&f,d&(5($!.0"1)0,~"(1)1#~"0|~&5),|.&d&(5($!.0"1)1#~"0(5-5|])),'|/\|'&f)
4(1!:2)~LF,"1 g"."0}:(1!:1)3

First off: Yes, yes, gravedigging. Second: That's just embarassingly long. Oh well. I haven't decided yet whether to golf it further or curl up in fetal position and cry. (Or both!)

Here's a bit of explanation in lieu of a shorter program:

  • c is 2x6 table of empty cell, bead cell for rendering.
  • f renders an '=' row with the four outside characters as left argument.
  • d renders an abacus row by translating 0/1 matrices into bead cells padded with ||
  • g takes digits and vertically compiles character rows using f for 'formatting' rows and d for abacus rows.
  • The last row gets input, splits into characters and converts those to numbers, feeds to g and then prints.
\$\endgroup\$
2
\$\begingroup\$

C, 277 274 characters

You know, it seems to me that we just don't have enough solutions here that really take advantage of the C preprocessor. Partly that's because those #defines actually take up a fair bit of space. But still, there's so much potential. I feel the need to address this deficiency.

#define L(x,z)for(printf("|"x),p=b;*p||puts(#z)<0;++p)printf(
#define F(x,y,z)L(#x,==z|)"======",y);
#define T(y)L("|  ",||)*p-47 y?"(__)  ":" ||   ");
i;char*p,b[99];main(j){gets(b);F(\\,0,/)T()T(<6)T(<1)T(>5)F(<,*p>52?*p-=5:0,>)
for(;++i<6;)T(>i)for(;++j<7;)T(<j)F(/,0,\\)}

That's better.

\$\endgroup\$
2
\$\begingroup\$

Mathematica 281

w@n_:= Module[{t=Table,f,g},
 f@d_:=ReplacePart["O"~t~{7},{2-Quotient[d,5]-> "|",3+Mod[d,5]-> "|"}];
 g@k_:=IntegerDigits@n~PadLeft~10;
 Grid[Insert[Insert[(f/@g@n)T,"=="~t~{10},{{1},{3},{8}}]T,""~t~{10},{{1},{11}}]T]]

Example

w[6302715408]

abacus

\$\endgroup\$
2
  • \$\begingroup\$ Congrats on 10k! :-D \$\endgroup\$
    – Doorknob
    Commented Mar 6, 2014 at 17:58
  • \$\begingroup\$ @Doorknob. Thanks! Congrats to you, too! \$\endgroup\$
    – DavidC
    Commented Mar 6, 2014 at 18:40
1
\$\begingroup\$

C, 548

#define p(a) printf(a);
#define F(x,m) for(x=0;x<m;x++)
#define I(x) {p("||")F(j,l)if(b[l*(i+x)+j]){p("  (__)")}else{p("   || ")}p("  ||\n")}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int a,char* c[]){int i,j,l,m,*b;l=strlen(c[1]);b=(int*)malloc(l*56);m=6*l;F(i,14*l)b[i]=0;
F(j,l){b[j]=1;if(c[1][j]<53){b[l+j]=1;}else{b[3*l+j]=1;c[1][j]-=5;}F(i,5){if(i<c[1][j]-'0'){
b[(i+5)*l+j]=1;}else{b[(i+9)*l+j]=1;}}}p("|\\=")F(i,m)p("=")p("=/|\n")F(i,4)I(0)p("|<=")F(i,m)
p("=")p("=>|\n")F(i,9)I(5)p("|/=")F(i,m)p("=")p("=\\|\n")}

First version, just a bit of golfing so far.

\$\endgroup\$
1
\$\begingroup\$

Scala (489 characters)

def a(i:String){val b=" (__) ";val n="  ||  ";1 to 17 map{l=>;{print(l match{case 1=>"|\\=";case 6=>"|<=";case 17=>"|/=";case _=>"|| "});print(l match{case 1|6|17=>"======"*i.size;case 2|16=>b*i.size;case 4|11=>n*i.size;case 3=>i flatMap{d=>{if(d.asDigit<5)b else n}};case 5=>i flatMap{d=>{if(d.asDigit>4)b else n}};case _=>i flatMap{d=>{if(l<11)if(d.asDigit%5<l-6)n else b else if(d.asDigit%5>l-12)n else b}}});;print(l match{case 1=>"=/|";case 6=>"=>|";case 17=>"=\\|";case _=>" ||"})}}}

Pretty crappy attempt really.

\$\endgroup\$
1
\$\begingroup\$

Sclipting, 77 chars

The bounty refers to tweets, and Twitter counts characters (not bytes). :)

꿓뤽꿓뤽①長復標독렽꿐②껷렊밃겠上❶가侔是丟돃렽꿐②꿧렊不돇렠글⓶①各❷❷걐剩減갰減갰分❸⓷걀大加增增감右與꺅뭟꺒갠⓵긇롼긂갠嗎⓶終丟돇렊終終돂묽꿐②뇇렊

I actually wrote this years ago (when this challenge was posted), but I never posted it because I invented Sclipting after this challenge was first posted. If you feel that this makes it ineligible, I understand.

Explanation

꿓뤽꿓뤽①長復 | let x = "======" times the length of the input string
標 | mark
독렽꿐②껷렊 | "|\\==" + x + "/|\n"
밃겠上 | for row in [-4 .. 10]
    ❶가侔是 | if row == 0
        丟돃렽꿐②꿧렊 | "|<==" + x + ">|\n"
    不 | else
        돇렠글 | "||  "
        ⓶①各 | foreach char c in input
            | ((r-(c%5)-3)/3 & ((r + (c>4?3:2)) >> 1)) ? "(__) " : " ||   "
            ❷❷걐剩減갰減갰分❸⓷걀大加增增감右與꺅뭟꺒갠⓵긇롼긂갠嗎⓶
        終丟
        돇렊 | "||\n"
    終
終
돂묽꿐②뇇렊 | "|/==" + x + "\\|\n"
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your interesting answer. I agree that it's best to stick to the rule that only languages that are published prior to the question being asked should be eligible. I don't believe you would deliberately have designed your language to win this challenge, but at the same time I don't think it's fair on the other answerers, and we don't want to open the floodgates for newly invented languages on old questions since we know what will happen... \$\endgroup\$
    – gnibbler
    Commented Feb 11, 2014 at 0:18
  • \$\begingroup\$ @gnibbler: Sure, I’m fine with that. I think this rule is an important one. \$\endgroup\$
    – Timwi
    Commented Feb 11, 2014 at 0:35
1
\$\begingroup\$

Python, 309 301 288 chars

Compact version:

q=p,b="  ||  "," (__) "
t="|| %s ||\n"
a=lambda h:t%"".join(q[i]for i in h)
n=[int(d)for d in str(input())]
c=len(n)
e="="*(c*6+2)
h="|\\"+e+"/|"
print h+"\n"+t%(b*c)+a(d<5 for d in n)+t%(p*c)+a(d>4 for d in n)+"|<"+e+">|\n"+"".join(a((d%5>i%5)^(i>4)for d in n)for i in range(10))+h[::-1]

Clear version:

bead = " (__) "
pole = "  ||  "
template = "|| %s ||\n"
output = ""
def addline(hasbeads):
    global output
    output += template % "".join([bead if item else pole for item in hasbeads])

digits = [int(d) for d in str(input())]
count = len(digits)
equalsigns = "=" * (count * 6 + 2)
output = ""
header = "|\\" + equalsigns + "/|"
output += header + "\n"
output += template % (bead * count)
addline([d < 5 for d in digits])
output += template % (pole * count)
addline([d > 4 for d in digits])
output += "|<" + equalsigns + ">|\n"
for i in range(5):
    addline([d % 5 > i for d in digits])
for i in range(5):
    addline([d % 5 <= i for d in digits])
output += header[::-1]
print output

Note that for compactification, variables were renamed to a single letter, and list comprehensions were changed to generators that don't require extra brackets.

\$\endgroup\$

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.