Skip to content

Commit

Permalink
Add Baklava in Tex (#4418)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzuckerm authored Jan 30, 2025
1 parent 0390af8 commit d0780a2
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions archive/t/tex/baklava.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
% Source: https://rosettacode.org/wiki/String_append#Plain_TeX
\def\addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}

\obeyspaces
\newwrite\out
\immediate\openout\out=baklava.txt

% TeX doesn't seem to support nested loops; therefore, this needs to be
% flattened out to a single loop that runs from 0 to 330, where 331
% is the total number of characters if you add the length of all the
% lines together. This was determined by the following python code:
%
% >>> lines = [" " * abs(n) + "*" * (21 - 2 * abs(n)) for n in range(-10,11)]
% >>> sum(len(line) for line in lines)
% 331
%
% The general algorithm is as follows (prototyped in python):
%
% linecounter = -10
% linelen = 0
% for charcounter = 0 to 330:
% # If start of line, reset all variables
% if linelen < 1:
% line = ""
% numspaces = abs(linecounter)
% numstars = 21 - 2 *numspaces
% linelen = numspaces + numstars
% # If not done with spaces, append space to line
% if linelen > numstars:
% line += " "
% # Else, append star to line
% else:
% line += "*"
% # If end of line, output line and increment line counter
% linelen -= 1
% if linelen < 1:
% print(line)
% linecounter += 1

\newcount\charcounter
\newcount\linecounter
\newcount\numspaces
\newcount\numstars
\newcount\linelen

\linecounter=-10
\linelen=0
\charcounter=0
\loop
\ifnum\linelen<1
\def\line{}
% numspaces = abs(linecounter)
\numspaces=\linecounter
\ifnum\numspaces<0
\multiply\numspaces -1
\fi
% numstars = 21 - 2 *numspaces
\numstars=\numspaces
\multiply\numstars -2
\advance\numstars 21
% linelen = \numspaces + \numstars
\linelen=\numspaces
\advance\linelen \numstars
\fi
\ifnum\linelen>\numstars
\addtomacro\line{ }
\else
\addtomacro\line{*}
\fi
\advance\linelen -1
\ifnum\linelen<1
\immediate\write\out{\line}
\advance\linecounter 1
\fi
\advance\charcounter 1
\ifnum \charcounter<331
\repeat
\end

0 comments on commit d0780a2

Please sign in to comment.