Martin Ender's 2D programming language Alice has two different modes depending on what orientation the IP has: orthogonal (Cardinal mode) or diagonal (Ordinal mode). Commands in Alice change their meaning depending on which mode the program is in when they're executed. One especially interesting implementation of this is Alice's
Z, or "pack", command. For strings in Ordinal mode, this simply takes two strings a and b and interleaves (also known as "zip") them. For example:
a = "Hello" b = "World" Z -> "HWeolrllod"
However, while in Cardinal mode,
Z pops two integers \$n\$ and \$m\$ and returns \$\pi(n,m)\$*, the Cantor pairing function. For example, for \$n = 2, m = 3\$,
Z returns \$\pi(2, 3) = 18\$. The reasoning behind this is explained in this answer.
For clarity, the Cantor pairing function uses the following formula:
$$\pi(n,m) = \frac12(n+m)(n+m+1)+m$$
You are to write two non-identical programs that implement the two modes of
Z. More specifically:
- The first should take two non-empty strings containing only printable ASCII (
0x7e) of the same length* and output these strings zipped/interleaved together
- The second should take two non-negative integers \$x\$ and \$y\$ and should output \$\pi(x, y)\$ as specified above.
*: This isn't technically how the
Z command works, read the Alice docs for more
You may input and output in any accepted method, and you may assume all inputs are reasonable for your language.
For the first program:
a, b -> Z "a", "b" -> "ab" "Hello,", "World!" -> "HWeolrllod,!" "World!", "Hello," -> "WHoerllldo!," "Alice", " " -> "A l i c e "
The second program:
n, m -> π(n, m) 2, 3 -> 18 5, 5 -> 60 0, 1 -> 2 1, 0 -> 1 100, 75 -> 15475