Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active October 10, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/043923870d367a539e128202580a6c29 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/043923870d367a539e128202580a6c29 to your computer and use it in GitHub Desktop.
Just PHP FUN 125.
<?php
# https://www.codewars.com/kata/5876e24130b45aaa0c00001d Esolang: Ticker.
function interpreter(string $tape): string {
$mem = [];
array_push($mem,0);
$slct = 0;
$answer = "";
for($i = 0; $i < strlen($tape); $i+=1){
$c = $tape[$i];
if('>' == $c) $slct += 1;
if('<' == $c) $slct -= 1;
if('+' == $c && false !== isset($mem[$slct]) ) $mem[$slct] = ($mem[$slct] + 1)%256;
if('-' == $c && false !== isset($mem[$slct])) {
$mem[$slct] -= 1;
if(0 > $mem[$slct]) $mem[$slct] = 255;
}
if('*' == $c && false !== isset($mem[$slct]) ) $answer .= (chr($mem[$slct]));
if('*' == $c && false === isset($mem[$slct]) ) $answer .= (chr(0));
if('/' == $c && false !== isset($mem[$slct]) ) $mem[$slct] = 0;
if('!' == $c) array_push($mem,0);
}
return $answer;
}
# ---------------------------------
# Below with error.
# --------------------------------------------
function interpreter(string $tape): string {
$mem = [0];
$slct = 0;
$answer = "";
for($i = 0; $i < strlen($tape); $i+=1){
$c = $tape[$i];
if('>' == $c) $slct += 1;
if('<' == $c) $slct -= 1;
if('*' == $c){
$value = false === isset($mem[$slct]) ? 0 : $mem[$slct];
$answer .= chr($value);
}
if('+' == $c && false !== isset($mem[$slct]) ){
$mem[$slct] = ($mem[$slct] + 1)%256;
}
if('-' == $c && false !== isset($mem[$slct]) ){
$mem[$slct] -= 1;
if(0 > $mem[$slct]) $mem[$slct] = 0;
}
if('/' == $c && false !== isset($mem[$slct]) ){
$mem[$slct] -= 0;
}
if('!' == $c) array_push($mem,0);
}
return $answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment