- https://www.codewars.com/kata/5876e24130b45aaa0c00001d Esolang: Ticker.
Started at 22:57 10.10.2020 Saturday Oct.
Finished at 22:28 10.10.2020 Satruday Oct (0hrs 31minutes)
<?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; | |
} |
Started at 22:57 10.10.2020 Saturday Oct.
Finished at 22:28 10.10.2020 Satruday Oct (0hrs 31minutes)