summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/public/posts/2022-08-27
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-09-07 22:27:48 +0900
committernsfisis <nsfisis@gmail.com>2023-09-07 22:35:53 +0900
commit994e0114d76ae19768d5c303874a968cf6369fd0 (patch)
tree5fd3f8b169eea00084b24fbae820f75273864d2a /vhosts/blog/public/posts/2022-08-27
parent57f015992f678bfd7281f171fb9d71349c96a1a0 (diff)
downloadnsfisis.dev-994e0114d76ae19768d5c303874a968cf6369fd0.tar.gz
nsfisis.dev-994e0114d76ae19768d5c303874a968cf6369fd0.tar.zst
nsfisis.dev-994e0114d76ae19768d5c303874a968cf6369fd0.zip
meta: migrate to monorepo
Diffstat (limited to 'vhosts/blog/public/posts/2022-08-27')
-rw-r--r--vhosts/blog/public/posts/2022-08-27/php-conference-okinawa-code-golf/index.html192
1 files changed, 192 insertions, 0 deletions
diff --git a/vhosts/blog/public/posts/2022-08-27/php-conference-okinawa-code-golf/index.html b/vhosts/blog/public/posts/2022-08-27/php-conference-okinawa-code-golf/index.html
new file mode 100644
index 00000000..2d311e7f
--- /dev/null
+++ b/vhosts/blog/public/posts/2022-08-27/php-conference-okinawa-code-golf/index.html
@@ -0,0 +1,192 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2022 nsfisis">
+ <meta name="description" content="PHP カンファレンス沖縄の懇親会 LT で出題されたコードゴルフの問題を解いてみた。">
+ <meta name="keywords" content="カンファレンス,PHP,PHP カンファレンス">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>PHP カンファレンス沖縄で出題されたコードゴルフの問題を解いてみた | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">PHP カンファレンス沖縄で出題されたコードゴルフの問題を解いてみた</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/conference/">カンファレンス</a>
+ </li>
+ <li class="tag">
+ <a href="/tags/php/">PHP</a>
+ </li>
+ <li class="tag">
+ <a href="/tags/phpcon/">PHP カンファレンス</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2022-08-27">2022-08-27</time>: 公開
+ </li>
+ </ol>
+ </section>
+ <section id="section--intro">
+ <h2><a href="#section--intro">はじめに</a></h2>
+ <p>
+ 本日 <a href="https://phpcon.okinawa.jp/">PHP カンファレンス沖縄 2022</a> が開催された (らしい)。
+ </p>
+
+ <p>
+ カンファレンスには参加できなかったものの、懇親会の LT で出題されたコードゴルフの問題が Twitter に流れてきたので、解いてみた。
+ </p>
+
+ <ul>
+ <li>
+ ツイート: <a href="https://twitter.com/m3m0r7/status/1563397620231712772">https://twitter.com/m3m0r7/status/1563397620231712772</a>
+ </li>
+
+ <li>
+ スライド: <a href="https://speakerdeck.com/memory1994/php-conference-okinawa-2022-extra?slide=3">https://speakerdeck.com/memory1994/php-conference-okinawa-2022-extra?slide=3</a>
+ </li>
+ </ul>
+ </section>
+
+ <section id="section--solution">
+ <h2><a href="#section--solution">解</a></h2>
+ <p>
+ 細かいレギュレーションは不明だったので、勝手に定めた。
+ </p>
+
+ <ul>
+ <li>
+ コマンドライン引数の第1引数で受けとる
+ </li>
+
+ <li>
+ 結果は標準出力に出す
+ </li>
+
+ <li>
+ コンマの直後にはスペースを1つ置く
+ </li>
+
+ <li>
+ 末尾コンマは禁止
+ </li>
+
+ <li>
+ 数字でないものは入ってこないものとする
+ </li>
+
+ <li>
+ 負数は入ってこないものとする
+ </li>
+ </ul>
+
+ <p>
+ 書いたものがこちら:
+ </p>
+
+ <pre class="highlight" language="php" linenumbering="unnumbered"><code class="highlight">[<span class="hljs-meta">&lt;?php</span> <span class="hljs-variable">$n</span>=<span class="hljs-variable">$argv</span>[<span class="hljs-number">1</span>];<span class="hljs-keyword">foreach</span>([<span class="hljs-number">1e4</span>,<span class="hljs-number">5e3</span>,<span class="hljs-number">2e3</span>,<span class="hljs-number">1e3</span>,<span class="hljs-number">500</span>,<span class="hljs-number">100</span>,<span class="hljs-number">50</span>,<span class="hljs-number">10</span>,<span class="hljs-number">5</span>,<span class="hljs-number">1</span>]<span class="hljs-keyword">as</span><span class="hljs-variable">$x</span>)<span class="hljs-keyword">for</span>(;<span class="hljs-variable">$n</span>&gt;=<span class="hljs-variable">$x</span>;<span class="hljs-variable">$n</span>-=<span class="hljs-variable">$x</span>)<span class="hljs-variable">$r</span>[]=<span class="hljs-variable">$x</span>;<span class="hljs-keyword">echo</span> <span class="hljs-title function_ invoke__">implode</span>(<span class="hljs-string">&#x27;, &#x27;</span>,<span class="hljs-variable">$r</span>??[]);<span class="hljs-meta">?&gt;</span>]</code></pre>
+
+ <p>
+ しめて 123 バイトとなった (末尾改行を含めずにカウント)。
+ </p>
+
+ <p>
+ こちらは改行とスペースを追加したバージョン:
+ </p>
+
+ <pre class="highlight" language="php" linenumbering="unnumbered"><code class="highlight">[<span class="hljs-meta">&lt;?php</span>
+
+<span class="hljs-variable">$n</span> = <span class="hljs-variable">$argv</span>[<span class="hljs-number">1</span>];
+<span class="hljs-keyword">foreach</span> ([<span class="hljs-number">1e4</span>, <span class="hljs-number">5e3</span>, <span class="hljs-number">2e3</span>, <span class="hljs-number">1e3</span>, <span class="hljs-number">500</span>, <span class="hljs-number">100</span>, <span class="hljs-number">50</span>, <span class="hljs-number">10</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-variable">$x</span>)
+ <span class="hljs-keyword">for</span> (; <span class="hljs-variable">$n</span> &gt;= <span class="hljs-variable">$x</span>; <span class="hljs-variable">$n</span> -= <span class="hljs-variable">$x</span>)
+ <span class="hljs-variable">$r</span>[] = <span class="hljs-variable">$x</span>;
+<span class="hljs-keyword">echo</span> <span class="hljs-title function_ invoke__">implode</span>(<span class="hljs-string">&#x27;, &#x27;</span>, <span class="hljs-variable">$r</span> ?? []);
+
+<span class="hljs-meta">?&gt;</span>]</code></pre>
+ </section>
+
+ <section id="section--techniques">
+ <h2><a href="#section--techniques">使用したテクニック</a></h2>
+ <section id="section--techniques--exponential-notation">
+ <h3><a href="#section--techniques--exponential-notation">指数表記</a></h3>
+ <p>
+ 割と多くの言語のゴルフで使えるテクニック。 <code>e</code> を用いた指数表記で、大きな数を短く表す。 このコードでは <code>10000</code>、<code>5000</code>、<code>2000</code>、<code>1000</code> を指数表記している。
+ </p>
+ </section>
+
+ <section id="section--techniques--shorten-loop">
+ <h3><a href="#section--techniques--shorten-loop">foreach や for の中身を1つの文に</a></h3>
+ <p>
+ <code>foreach</code>、<code>for</code>、<code>if</code> などの後ろには、 通常 <code>{</code> を続けて複数の文を連ねるが、中身の文を1つにしてしまえば、<code>{</code> と <code>}</code> を省略できる。 C言語などでも使える。
+ </p>
+ </section>
+
+ <section id="section--techniques--omit-initialization">
+ <h3><a href="#section--techniques--omit-initialization">$r に初期値を入れない</a></h3>
+ <p>
+ PHP では、<code>$r[] = ......</code> のような配列の末尾に追加する式を実行したとき、 <code>$r</code> が未定義だった場合は <code>$r</code> を勝手に定義して空の配列で初期化してくれる。 これを利用すると、<code>$r = [];</code> のような初期化が不要になる。
+ </p>
+
+ <p>
+ ただし、プログラムに 0 が渡されるとループを一度も回らないので、<code>$r</code> が未定義になってしまい、 <code>implode()</code> に渡すところでエラーになる。 それを防ぐために <code>$r ?? []</code> を使っている。
+ </p>
+
+ <p>
+ もし 0 が渡されたケースを無視するなら、これが不要になるので 4 バイト縮む。
+ </p>
+ </section>
+
+ <section id="section--techniques--put-text-outside-php-tag">
+ <h3><a href="#section--techniques--put-text-outside-php-tag">PHP タグの外に文字列を置く</a></h3>
+ <p>
+ PHP では、<code>&lt;?php</code> <code>?&gt;</code> で囲われた部分の外側にある文字列は、そのまま出力される。 今回のケースでは、先頭と末尾に必ず <code>[</code> と <code>]</code> を出力するので、そのまま書いてやればよい。
+ </p>
+ </section>
+ </section>
+
+ <section id="section--outro">
+ <h2><a href="#section--outro">おわりに</a></h2>
+ <p>
+ 最後になりましたが、<a href="https://twitter.com/m3m0r7">めもりー</a>さん、楽しい問題をありがとうございました。
+ </p>
+ </section>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>