#PHP, 75 bytes
This uses a modified version of Jörg Hülsermann'sJörg Hülsermann's answer.
eval(preg_replace('~.~','$s=($s\0(2-ord("\0")%11%3))|0;',$argv[1]));echo$s;
It heavily relies on string substitution, using a simple regular expression (~.~
).
The variable $s
is re-assigned with the new value for each character. At the end, it outputs the result.
**Note**: This is meant to be executed using the `-r` flag.
Try it here:
if('\0' == "\0")
{
$argv = Array($s = 0, prompt());
function preg_replace($pattern, $replacement, $subject)
{
$regexp = new RegExp($pattern.replace(new RegExp('~', 'g'), ''), 'g');
return $subject.replace($regexp, $replacement.split('\0').join('$&'));
}
function printf($string)
{
console.log($string);
}
function ord($chr)
{
return $chr.charCodeAt(0);
}
}
else
{
if(!isset($argv))
{
$argv = array('', '++*+');
}
}
eval(preg_replace('~.~','$s=($s\0(2-ord("\0")%11%3))|0;',$argv[1]));printf($s);
Or try on: http://sandbox.onlinephpfunctions.com/code/7d2adc2a500268c011222d8d953d9b837f2312aa
Differences:
- Instead of
echo$s
, I'm usingsprintf($s)
. Both perform the same action on numbers. Since this is just for testing, it is fine. - In case there's no passed argument, it will run as if you passed
++*+
as the first argument, which should show5
.