From fbd4f2129ce8fe106391302896dd86e05b2f331b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 5 Dec 2025 04:08:22 +0900 Subject: add files --- archive/q/brainf-ck/a/43/index.html | 118 ++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 archive/q/brainf-ck/a/43/index.html (limited to 'archive/q/brainf-ck/a/43') diff --git a/archive/q/brainf-ck/a/43/index.html b/archive/q/brainf-ck/a/43/index.html new file mode 100644 index 0000000..0d728a3 --- /dev/null +++ b/archive/q/brainf-ck/a/43/index.html @@ -0,0 +1,118 @@ + + + + + 問題 #3 - 回答 #43 | Albatross.PHP + + + + + +
+ +
+
+

問題 #3 - 回答 #43

+ + +

Brainf*ck

+

+ Brainf*ck を実装してください。標準入力から Brainf*ck のソースコードが渡されます。Brainf*ck の仕様のうち、入力命令である「,」は未実装で構いません。 + +一部明確に挙動が定められていない部分については、以下の仕様とします。 +メモリの各セルは 8 bit の符号なし整数です。今回の入力でオーバーフローは発生しません。 +メモリのアドレスは非負整数です。今回の入力で負のアドレスへポインタを動かすことはありません。 +Brainf*ck に存在する命令以外の文字を受け取った場合は無視してください。 +今回の入力に不正なプログラムは含まれません。すなわち、「[」と「]」は必ず釣り合っています。 +

+

回答 #43

+

+ ticknical が 2024-03-08 10:23:21 に投稿 +

+

コード

+

+ 580 byte +

+
$src=stream_get_contents(STDIN);$p=$m=[];$r=0;
+
+while($r<strlen($src)){
+    switch($src[$r]){
+        case'>':$p++;break;
+        case'<':$p--;break;
+        case'+':if(!isset($m[$p]))$m[$p]=0;$m[$p]++;break;
+        case'-':if(!isset($m[$p]))$m[$p]=0;$m[$p]--;break;
+        case'.':echo chr($m[$p]);break;
+        case'[':if(!isset($m[$p])||$m[$p]===0){$d=1;while($d>0){$r++;if($src[$r]==='[')$d++;elseif($src[$r]===']')$d--;}}break;
+        case']':if(isset($m[$p])&&$m[$p]!==0){$d=1;while($d>0){$r--;if($src[$r]===']')$d++;elseif($src[$r]==='[')$d--;}}break;
+    }
+    $r++;
+}
+

実行結果

+
+
+
+ ステータス: 失敗 +
+
+

テストケース 1

+
+ ステータス: 実行時エラー +
+

標準出力

+

+Fatal error: Uncaught TypeError: Illegal offset type in isset or empty in php.wasm code:12
+Stack trace:
+#0 {main}
+  thrown in php.wasm code on line 12
+
+
+

標準エラー出力

+

+
+

テストケース 2

+
+ ステータス: 実行時エラー +
+

標準出力

+

+Fatal error: Uncaught TypeError: Illegal offset type in isset or empty in php.wasm code:12
+Stack trace:
+#0 {main}
+  thrown in php.wasm code on line 12
+
+
+

標準エラー出力

+

+
+

テストケース 3

+
+ ステータス: 実行時エラー +
+

標準出力

+

+Fatal error: Uncaught TypeError: Illegal offset type in isset or empty in php.wasm code:12
+Stack trace:
+#0 {main}
+  thrown in php.wasm code on line 12
+
+
+

標準エラー出力

+

+
+
+ +
+ + + -- cgit v1.2.3-70-g09d2