Added esoteric handout
This commit is contained in:
parent
9415d91e6b
commit
5a70784dda
52
Advanced/Esoteric Languages/main.tex
Executable file
52
Advanced/Esoteric Languages/main.tex
Executable file
@ -0,0 +1,52 @@
|
|||||||
|
% use [nosolutions] flag to hide solutions.
|
||||||
|
% use [solutions] flag to show solutions.
|
||||||
|
\documentclass[
|
||||||
|
nosolutions,
|
||||||
|
singlenumbering
|
||||||
|
]{../../resources/ormc_handout}
|
||||||
|
\usepackage{../../resources/macros}
|
||||||
|
|
||||||
|
|
||||||
|
% Factorial (print x! from x=0 onwards)
|
||||||
|
% >++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]<[>+<-]<[>+<-[>+<-[>
|
||||||
|
% +<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-
|
||||||
|
% ]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]<<<<[<[
|
||||||
|
% >+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<[<[>+>+<<-]>.<<<<<]>.>>>>]
|
||||||
|
%
|
||||||
|
% Same, fibonacci:
|
||||||
|
% >++++++++++>+>+[
|
||||||
|
% [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
|
||||||
|
% [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
|
||||||
|
% [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
|
||||||
|
% ]<<<
|
||||||
|
% ]
|
||||||
|
% https://brainfuck.org/fib_explained.b
|
||||||
|
%
|
||||||
|
% Useful:
|
||||||
|
% https://brainfuck.org/
|
||||||
|
%
|
||||||
|
%
|
||||||
|
% +++[[<+>>++<-]>]
|
||||||
|
% This sets up the tape in the format 3*n^2, which looks like
|
||||||
|
% 3 6 12 24 48 96 192 128 0 0
|
||||||
|
% Why is this so important?
|
||||||
|
% Let's go down the list:
|
||||||
|
% - 3 and 6 are boring
|
||||||
|
% - 12: Close to 10 (newline) or 13 (carriage return). Can also be used for the counter for 0-9
|
||||||
|
% - 24: Close to 26, the number of letters in the alphabet
|
||||||
|
% - 48: ASCII for 0
|
||||||
|
% - 96: Close to 97, ASCII for a
|
||||||
|
% - 196 and 128: 196-128=64, close to 65, the ASCII for A.
|
||||||
|
|
||||||
|
|
||||||
|
\uptitlel{Advanced 2}
|
||||||
|
\uptitler{\smallurl{}}
|
||||||
|
\title{Esoteric Programming}
|
||||||
|
\subtitle{Prepared by Mark on \today}
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
\maketitle
|
||||||
|
|
||||||
|
\input{parts/00 turing.tex}
|
||||||
|
\input{parts/01 befunge.tex}
|
||||||
|
\end{document}
|
157
Advanced/Esoteric Languages/parts/00 turing.tex
Executable file
157
Advanced/Esoteric Languages/parts/00 turing.tex
Executable file
@ -0,0 +1,157 @@
|
|||||||
|
\section{Turing}
|
||||||
|
|
||||||
|
\definition{}
|
||||||
|
An \textit{esoteric programming langauge} is a programming langauge made for fun. \par
|
||||||
|
We'll work with two such languages today: \textit{Turing} and \textit{Befunge}.
|
||||||
|
|
||||||
|
\definition{}
|
||||||
|
\textit{Turing} is one of the most famous esoteric langauges, and is extremely minimalist. \par
|
||||||
|
It consists only of eight symbols, a data pointer, and an instruction pointer.
|
||||||
|
|
||||||
|
Turing's eight symbols are as follows:
|
||||||
|
\begin{itemize}[itemsep=2mm]
|
||||||
|
\item \texttt{>} : move the instruction pointer right
|
||||||
|
\item \texttt{<} : move the instruction pointer left
|
||||||
|
\item \texttt{+} : increment the current memory cell
|
||||||
|
\item \texttt{-} : decrement the current memory cell
|
||||||
|
\item \texttt{.} : output the value of the current cell as a character
|
||||||
|
\item \texttt{,} : consume one character from input and store it at the current cell
|
||||||
|
\item \texttt{[} : Jump to the matching \texttt{]} if the current cell is zero. \par
|
||||||
|
otherwise, execute the next instruction.
|
||||||
|
\item \texttt{[} : Jump back to the matching \texttt{]} if the current cell is not zero. \par
|
||||||
|
otherwise, execute the next instruction.
|
||||||
|
\item All other characters are ignored.
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Go to \href{https://langs.betalupi.com/}{\texttt{langs.betalupi.com}} and open the Turing editor. \par
|
||||||
|
Clear the pre-set program in the left side of the screen and replace it with the following:
|
||||||
|
\begin{center}
|
||||||
|
\texttt{>+>++>+++>++++>+++++[[-]<]}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\begin{itemize}
|
||||||
|
\item What does this program do?
|
||||||
|
\item What does the snippet \texttt{[-]} do?
|
||||||
|
\item Why is the first cell left as \texttt{0}?
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
\pagebreak
|
||||||
|
|
||||||
|
\remark{}
|
||||||
|
Run the program \texttt{+[+]}. Notice that the cell's value \textit{wraps}
|
||||||
|
from \texttt{127} to \texttt{-128}! \par
|
||||||
|
|
||||||
|
\vspace{2mm}
|
||||||
|
|
||||||
|
Note that \texttt{[+]} has the same effect as \texttt{[-]}.
|
||||||
|
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that moves the value of the current cell three cells to the right.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that \textbf{copies} the value of the current cell into the next cell.
|
||||||
|
|
||||||
|
\begin{solution}
|
||||||
|
Use a third \say{scratch} cell.
|
||||||
|
\end{solution}
|
||||||
|
|
||||||
|
\problem{}<bfadd>
|
||||||
|
Write a program that adds the value of the first cell and the second cell,
|
||||||
|
leaving the result in the second cell.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Solve \ref{bfadd} again, but multiply the two cells instead of adding.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that computes the square of the current cell.
|
||||||
|
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
\pagebreak
|
||||||
|
|
||||||
|
|
||||||
|
\definition{}
|
||||||
|
Turing uses ASCII to map numbers to characters. \par
|
||||||
|
You may use
|
||||||
|
\href{https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html}{\texttt{cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html}}
|
||||||
|
for reference.
|
||||||
|
|
||||||
|
\vspace{5mm}
|
||||||
|
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that prints the letter \texttt{a}.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that prints the alphabet as shown below: \par
|
||||||
|
\begin{center}
|
||||||
|
\texttt{abcdefghijklmnopqrstuvwxyz}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Modify your program so that it prints the alphabet with alternating case:
|
||||||
|
\begin{center}
|
||||||
|
\texttt{aBcDeFgHiJkLmNoPqRsTuVwXyZ}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that repeats the user's input exactly as it is entered.
|
||||||
|
\begin{solution}
|
||||||
|
\texttt{,[.,]}
|
||||||
|
\end{solution}
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that repeats the user's input in reverse
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
\pagebreak
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that prints \say{Hello World}
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that finds the first memory cell that is non-zero.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that initializes the tape with the following sequence:
|
||||||
|
\begin{center}
|
||||||
|
\texttt{3 6 12 24 48 96 192 128 0}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that decodes run-length encoding.
|
||||||
|
That is, it turns input like
|
||||||
|
\begin{center}
|
||||||
|
\texttt{a3j4k2d5}
|
||||||
|
\end{center}
|
||||||
|
into the decoded output
|
||||||
|
\begin{center}
|
||||||
|
\texttt{aaajjjjkkddddd}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
|
||||||
|
\problem{Bonus}
|
||||||
|
Write a program that prints a Turing program that prints the original program's input.
|
||||||
|
|
||||||
|
\problem{Bonus}
|
||||||
|
Write a program that outputs all squares between 0 and 400 %10,000
|
||||||
|
|
||||||
|
\problem{Bonus}
|
||||||
|
Write a program that prints the Fibonacci numbers
|
||||||
|
|
||||||
|
\begin{solution}
|
||||||
|
\href{https://brainfuck.org/fib\_explained.b}{\texttt{https://brainfuck.org/fib\_explained.b}}
|
||||||
|
\end{solution}
|
||||||
|
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
\pagebreak
|
127
Advanced/Esoteric Languages/parts/01 befunge.tex
Executable file
127
Advanced/Esoteric Languages/parts/01 befunge.tex
Executable file
@ -0,0 +1,127 @@
|
|||||||
|
\section{Befunge}
|
||||||
|
|
||||||
|
\definition{}
|
||||||
|
\textit{Befunge} is another esoteric programming langauge, designed to be very difficult to compile. \par
|
||||||
|
It consists of a \say{field} of instructions, a two-dimensional program counter, and a stack of values. \par
|
||||||
|
|
||||||
|
\vspace{2mm}
|
||||||
|
|
||||||
|
The program counter starts in the top-left corner of the field, moving right. \par
|
||||||
|
It executes any instruction it encounters.
|
||||||
|
The instructions \texttt{>}, \texttt{<}, \texttt{\^}, and \texttt{v} can be used to direct the program counter,
|
||||||
|
and \texttt{\_} and \texttt{|} are used for control flow.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
|
||||||
|
An instruction reference is below:
|
||||||
|
\begin{itemize}[itemsep=2mm]
|
||||||
|
\item \texttt{+} Addition: Pop two values $a$ and $b$, then push the result of $a+b$
|
||||||
|
\item \texttt{-} Subtraction: Pop two values $a$ and $b$, then push the result of $b-a$
|
||||||
|
\item \texttt{*} Multiplication: Pop two values $a$ and $b$, then push the result of $a \times b$
|
||||||
|
\item \texttt{/} Integer division: Pop two values $a$ and $b$, then push the result of $b \div a$, rounded down.
|
||||||
|
\item \texttt{\%} Modulo: Pop two values $a$ and $b$, then push the remainder of the integer division of $b \div a$.
|
||||||
|
\item \texttt{!} Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero.
|
||||||
|
\item \texttt{ \`} Greater than: Pop two values $a$ and $b$, then push 1 if $b>a$, otherwise zero.
|
||||||
|
\item \texttt{>} Program counter direction right
|
||||||
|
\item \texttt{<} Program counter direction left
|
||||||
|
\item \texttt{\textasciicircum} Program counter direction up
|
||||||
|
\item \texttt{v} Program counter direction down
|
||||||
|
\item \texttt{?} Random program counter direction
|
||||||
|
\item \texttt{\_} Horizontal \texttt{if}: pop a value; set direction to right if value=0, set to left otherwise
|
||||||
|
\item \texttt{|} Vertical \texttt{if}: pop a value; set direction to down if value=0, set to up otherwise
|
||||||
|
\item \texttt{"} Toggle string mode (push each character's ASCII value all the way up to the next ")
|
||||||
|
\item \texttt{:} Duplicate top stack value
|
||||||
|
\item \texttt{\textbackslash} Swap top stack values
|
||||||
|
\item \texttt{\$} Pop top of stack and discard
|
||||||
|
\item \texttt{.} Pop top of stack and output as integer
|
||||||
|
\item \texttt{,} Pop top of stack and output as ASCII character
|
||||||
|
\item \texttt{\#} Bridge: jump over next command in the current direction of the current PC
|
||||||
|
\item \texttt{g} A "get" call (a way to retrieve data in storage). Pop two values y and x, then push the ASCII value of the character at that position in the program. If (x,y) is out of bounds, push 0
|
||||||
|
\item \texttt{p} A "put" call (a way to store a value for later use). Pop three values y, x and v, then change the character at the position (x,y) in the program to the character with ASCII value v
|
||||||
|
\item \texttt{\&} Get integer from user and push it
|
||||||
|
\item \texttt{\~{}} Get character from user and push it
|
||||||
|
\item \texttt{@} End program
|
||||||
|
\item \texttt{0}-\texttt{9} Push corresponding number onto the stack
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
\vspace{2mm}
|
||||||
|
|
||||||
|
Note that the \texttt{p} instruction allows us to write self-modifying code.
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
\pagebreak
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that prints \say{\texttt{Hello World}}.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that prints the alphabet
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that generates a random sequence of numbers (\texttt{0}-\texttt{9}).
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Write a program that does not contain the string \say{\texttt{Hello World}}, \par
|
||||||
|
but writes that string somewhere inside its source.
|
||||||
|
|
||||||
|
\problem{}
|
||||||
|
Replace the \texttt{x}s in the following program
|
||||||
|
so that the loop runs forever. \par
|
||||||
|
Do not use any control-flow instructions
|
||||||
|
(\texttt{>}, \texttt{<}, \texttt{\textasciicircum{}}, \texttt{v}, \texttt{\_}, \texttt{|}, \texttt{\#}, or \texttt{?}) \par
|
||||||
|
\hint{
|
||||||
|
Start by replacing all the \texttt{x}s with spaces. \par
|
||||||
|
You may not need all the \texttt{x}s, feel free to use a smaller rectangle.
|
||||||
|
}
|
||||||
|
|
||||||
|
\vspace{2mm}
|
||||||
|
|
||||||
|
\begin{center}
|
||||||
|
\texttt{>xxxxxxxxv \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
x ~ ~ ~ ~x \\
|
||||||
|
\textasciicircum{}xxxxxxxx@
|
||||||
|
}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\begin{solution}
|
||||||
|
The intended solution is self-modifying code:
|
||||||
|
|
||||||
|
\begin{center}
|
||||||
|
\texttt{>69*6+97pv\\
|
||||||
|
p ~ ~ ~ ~8\\
|
||||||
|
7 ~ ~ ~ ~8\\
|
||||||
|
9 ~ ~ ~ ~*\\
|
||||||
|
* ~ ~ ~ ~0\\
|
||||||
|
8 ~ ~ ~ ~0\\
|
||||||
|
8 ~ ~ ~ ~p\\
|
||||||
|
\textasciicircum{}p00-2*88@
|
||||||
|
}
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\end{solution}
|
||||||
|
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
|
||||||
|
|
||||||
|
\problem{Bonus}
|
||||||
|
Write a quine. (i.e, write a program that outputs itself)
|
||||||
|
|
||||||
|
\begin{solution}
|
||||||
|
\texttt{01->1\# +\# :\# 0\# g\# ,\# :\# 5\# 8\# *\# 4\# +\# -\# \_@}
|
||||||
|
\end{solution}
|
||||||
|
|
||||||
|
\vfill
|
||||||
|
\pagebreak
|
Loading…
x
Reference in New Issue
Block a user