/o!
\i@/e)q&w[?'`-+k3-n
Try it online!
Input should be lower case. Prints 1
for dollar words and 0
otherwise.
Explanation
Time to show off Alice's tape and some advanced control flow. Despite being fairly good at working with integers and strings individually, Alice has no built-ins to a) determine a string's length, b) convert between characters and their code points. The reason for this is that all of Alice's commands either map integers to integers or strings to strings. But both of those would require mapping strings to integers or vice versa, so they don't fit into either of Alice's modes.
However, in addition to it's stack, Alice also has a tape and Cardinal and Ordinal mode interpret the data on the tape in different ways
- In Cardinal mode, it's a regular tape familiar from other languages like Brainfuck. You can store one integer in each cell and you can move a tape head around. The tape is infinitely long and initially holds a -1 in every cell. The cells are also indexed and the tape head starts at index 0.
- Ordinal mode has its own tape head (also starting at index 0) and it interprets the tape as a list of strings. Strings are terminated by non-character cells (i.e. any values which are not a valid Unicode code point), in particular -1. So for Ordinal mode, the tape is initially filled with empty strings.
This tape can be used for both of the above operations: to get a string length, we write it to the tape in Ordinal mode, seek the terminating -1 in Cardinal mode and retrieve the position of the tape head. To convert characters to their code points, we simply read them off the tape in Cardinal mode.
The other two important features used in this solution are the return stack and an iterator. Alice has a return stack which is usually filled when using the jump command j
, and which you can pop an address from to jump back with k
. However, it's also possible to push the current address to the return stack without jumping anywhere with w
. If we combine w
with the repeat command &
, we can push the current address to the return stack n times. Now each time we reach k
, one copy is popped off the return stack and we perform another iteration from w
(starting at the cell after it, because the IP moves before executing another command). When the return stack becomes empty, k
does nothing at all and the IP simply passes through. Hence &w...k
pops an integer n and then executes ...
n+1 times, which gives us a very concise way to express a simple for
loop.
On to the code itself...
/ Reflect to SE. Switch to Ordinal.
i Read the input word as a string.
Bounce off bottom boundary, move NE.
! Store the input word on the tape.
Bounce off top boundary, move SE.
/ Reflect to E. Switch to Cardinal.
e Push -1.
) Seek right on the tape for a -1, which finds the -1 terminating
the input word.
q Push the tape head's position, which gives us the string length N.
&w Repeat this loop n+1 times (see above for an explanation)...
[ Move the tape head left by one cell.
? Retrieve the code point of the character in that cell.
'` Push 96.
- Subtract it from the code point to convert the letters to 1...26.
+ Add the result to a running total. This total is initialised to
zero, because in Cardinal mode, the stack is implicitly filled with
an infinite amount of zeros at the bottom.
k End of loop.
Note that the above loop ran once more than we have characters in the
string. This is actually really convenient, because it means that we've
added a "-1 character" to the running total. After subtracting 96 to
convert it to its "letter value" this gives 97. So dollar words will
actually result in 100 - 97 = 3, which we can check against for one
byte less than for equality with 100.
3- Subtract 3 to give 0 for dollar words.
n Logical NOT. Turns 0 (dollar words) into 1 and everything else into 0.
The IP wraps around to the beginning of the first line.
\ Reflect to NE. Switch to Ordinal.
o Implicitly convert the result to a string and print it.
Bounce off top boundary, move SE.
@ Terminate the program.