Bake a slice of Pi
MATL, 70 68 67 bytes
'()'12:)l10:&<toYP43Y$51hb(!10Xy'\::\'FFhZ++'|'3$Yc'||\'3:(95'Zd'o(
Try it online!
Explanation
What a mess. But hey, there's a convolution!
The explanation will be clearer if you can inspect the stack contents after a given statement. To do it, just insert X#0$%
at that point. (This means: X#
show stack contents, 0$
don't implicitly display anything else, %
comment out rest of the code). For example, see the stack right after the convolution.
'()' % Push this string
12: % Range [1 2 ... 12]
) % Index into string (modular, 1-based): gives '()()()()()()'
l % Push 1 (will be used later)
10: % Range [1 2 ... 10]
&< % All pairwise "less than" comparisons. Gives matrix with "true"
% below the main diagonal, and the remining entries equal to "false"
to % Duplicate. Convert to numbers (true becomes 1, false becomes 0)
YP43Y$ % Compute pi with 43 significant digits (42 decimals). Gives a string
51h % Append last decimal, '3' (ASCII 51). This is needed to avoid rounding
b % Bubble up the true-false matrix, to be used as logical index
( % Fill the chars from the pi string into the 0-1 matrix, at the positions
% indicated by the true-false matrix. Thus each 1 is replaced by a char
% from the pi string. Entries that were 0 remain as 0. This is done in
% columm-major order...
! % ...so transpose to make it row-major
10Xy % Identity matrix of size 10
'\::\' % Push this string...
FFh % ...and append two zeros
Z+ % 2D convolution keeping size. The identity matrix convolved with the
% above string gives the diagonal bands with chars '\' and ':'
+ % Add to the matrix containing the digits of pi. At each entry, only one
% of the two matrices is nonzero
'|' % Push this string
3$Yc % Three-input string concatenation. This prepends the 1 (which was pushed
% a while ago) and appends '|' to each row of the matrix. This converts
% the matrix to char. Note that char 1 will be displayed as a space. We
% used char 1 and not char 0 (which would be displayed as a space too)
% because function `Yc` (`strcat`) strips off trailing space from the
% inputs, counting char 0 as space, but not char 1
'||\' % Push this string
3:( % Assign it to the first 3 entries of the matrix (column-major), that is,
% to the top of the first column
95 % Push ASCII for '_'
'Zd'o % Push string 'Zd' and convert to numbers: gives [90 100]. These are the
% (column-major) indices where the '_' char should appear in the last row
( % Fill those chars
% Implicitly display. (Chars 0 and 1 are displayed as space)
Perl, 93 bytes
$_=bpi$=;printf'()'x6x!$`.'
%12s',F.ee x!$\--^substr"\32::\\$&|",-12while/.{$\}/g
Requires the command line option -l71Mbignum=bpi
, counted as 14. The \32
should be replaced by a literal character 26.
Sample Usage
$ perl -l71Mbignum=bpi pi-slice.pl
()()()()()()
|\3.1415926|
|:\53589793|
\::\2384626|
\::\433832|
\::\79502|
\::\8841|
\::\971|
\::\69|
\::\3|
\__\|
Perl, 111 bytes
$_=bpi$_*($l=($.=$_)-3);printf'()'x($./2)x!$`."
%$.s",F.ee x!$l--^substr"\32::\\$&|",-$.while/.{$l}/g
Parameterized version. Requires the command line option -nMbignum=bpi
, counted as 12.
Sample Usage
$ echo 10 | perl -nMbignum=bpi pi-slice.pl
()()()()()
|\3.14159|
|:\265358|
\::\97932|
\::\3846|
\::\264|
\::\33|
\::\8|
\__\|
$ echo 20 | perl -nMbignum=bpi pi-slice.pl
()()()()()()()()()()
|\3.141592653589793|
|:\2384626433832795|
\::\028841971693993|
\::\75105820974944|
\::\5923078164062|
\::\862089986280|
\::\34825342117|
\::\0679821480|
\::\865132823|
\::\06647093|
\::\8446095|
\::\505822|
\::\31725|
\::\3594|
\::\081|
\::\28|
\::\4|
\__\|
JavaScript (ES6), 187 174 bytes
This is 1 byte shorter than just displaying the plain text.
for(y=n=0,s=`()()()()()()
`;y<10;y++,s+=`|
`)for(x=-2;x++<9;)s+=x>y?(Math.PI+'2384626433832795028841971693')[n++]:`\\${y>8?'__':x+1|y>2?'::':'||'}\\`[y-x]||' ';console.log(s)