String Reversal

Discussion of challenges you have already solved
sascha27
Posts: 5
Joined: Wed Jan 02, 2013 10:03 am

Post by sascha27 »

Inspired by the first example I solved it with

0,s/x?\?/x?\!
___\,=/ \P=/
trofi
Posts: 23
Joined: Mon Oct 14, 2013 7:20 pm

Post by trofi »

The "memory pointers" description is not quite clear.
Would be nice to have real VM source available.

The http://www.hacker.org/sh/shphp.phps gives 404 here:

Code: Select all

Not Found
The requested URL /sh/shphp.phps was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Thanks!
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

I posted it to pastebin. Get it here: http://pastebin.com/AyEQ8xid
a.goth
Posts: 43
Joined: Sat Sep 14, 2013 10:39 am

String Reversal

Post by a.goth »

teebee wrote:One-liners are often the key to shorter solutions. The following program of size 21 passes all the test cases:

Code: Select all

0@@@@@@@@=,x?$+x?s!P$
Even smaller and without restrictions:

Code: Select all

 !:^1=/?@\
$P/%0s\,x/
Just felt like posting it, because using an '@' instruction within a loop is such a nice and hugely useful trick to minimize code size.
User avatar
yes-man
Posts: 32
Joined: Fri Jan 30, 2009 5:14 pm

Post by yes-man »

At first I looked at the SHVM documentation and instantly hated all of it. Put it away for a few years.
Now, that I tried it, it's kinda fun. Definitely more than the HVM.

I took a pretty straight forward approach to this one. But looking at the other solutions, I'm already learning.

* first loop reads stuff (and copies it for comparison)
* then it increases the counter (if stuff != 0)
* if it was, double jump (clear stack top) into second loop
* second loop decreases counter (jump to ! if 0) and prints

/%____,x?\?/_20<x?\!
\>02+1<02/_\P>02-1/

I can see the beauty of the other zero-termination solutions because they don't need a counter.
The deal with @ in the loop to reset is also quite nice.
Post Reply