Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今日の"どう書く"問題のアルゴリズム例

 今日の"どう書く"問題のアルゴリズム例

Nagoya.php #15 補足資料
https://nagoyaphp.connpass.com/event/120856/

Takashi Kanemoto

April 17, 2019
Tweet

More Decks by Takashi Kanemoto

Other Decks in Programming

Transcript

  1. ͨ ͭ ͖ ͪ twitter.com/ttskch ͋͞ɺ͋ͳͨ΋ࠓ͙͢ϑΥϩʔ͠Α͏!!! ׬શແྉ ·͞ʹ໊ݴ੡଄ػ ਓੜͰେ੾ͳ͜ͱ͸ɺ͢΂ͯ ͖͔ͨͭͪΒڭΘͬͨ

    ͖ͨͭͪΛϑΥϩʔͨ͠Β
 ࠊ௧͕࣏Γ·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋཰ 100% ͍ͯ͠Δ ͍ͯ͠ͳ͍ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ
  2. 3 5 4 4 1 2 3 2 2 +

    3 = 5 3 -1 = 2
  3. 3 5 4 4 1 2 3 2 1 +

    3 = 4 2 -1 = 1
  4. 5

  5. 5 4

  6. 21 23 24 26 1 2 3 2 20 22

    25 27 ൪߸Λ2ͷྦྷ৐Ͱද͢ͱࢦ਺෦ͷࠩͰڑ཭ΛٻΊΒΕΔ
  7. 2 8 16 64 165 Λ 1 + 4 +

    32 + 128 ʹ෼ղ͍ͨ͠ 1 4 32 128
  8. 165 = 27x1 + 26x0 + 25x1 + 24x0 +

    23x0 + 22x1 + 21x0 + 20x1 21 23 24 26 20 22 25 27
  9. public function run(string $input): string { $lines = array_values(array_filter(range(0, 7),

    function (int $i) use ($input) { return str_split(sprintf('%b', $input))[$i] ?? false; })); $vertices = []; for ($i = 0; $i < count($lines); $i++) { $distance = ($lines[($i + 1) % count($lines)] - $lines[$i] + 8) % 8; $vertices[] = ($distance === 4 ? 2 : 3) + $distance - 1; } sort($vertices); return implode('', $vertices); }
  10. public function run(string $input): string { $lines = array_values(array_filter(range(0, 7),

    function (int $i) use ($input) { return str_split(sprintf('%b', $input))[$i] ?? false; })); $vertices = []; for ($i = 0; $i < count($lines); $i++) { $distance = ($lines[($i + 1) % count($lines)] - $lines[$i] + 8) % 8; $vertices[] = ($distance === 4 ? 2 : 3) + $distance - 1; } sort($vertices); return implode('', $vertices); } ೖྗΛ2ਐ਺ʹม׵ͯ͠ɺཱ͍ͬͯΔϏοτͷҐஔʢ0ʙ7ʣΛ഑ྻʹ͢Δɻ ྫ୊ͷ৔߹͸ $lines === [0, 2, 5, 7] ͱͳΔɻ͜Ε͕࣮ઢͷҐஔɻ ೖྗͷύʔε
  11. public function run(string $input): string { $lines = array_values(array_filter(range(0, 7),

    function (int $i) use ($input) { return str_split(sprintf('%b', $input))[$i] ?? false; })); $vertices = []; for ($i = 0; $i < count($lines); $i++) { $distance = ($lines[($i + 1) % count($lines)] - $lines[$i] + 8) % 8; $vertices[] = ($distance === 4 ? 2 : 3) + $distance - 1; } sort($vertices); return implode('', $vertices); } [0, 2, 5, 7] ͱ͍͏഑ྻʹରͯ͠ɺཁૉؒͷ͔ࠩΒ࣮ઢؒͷڑ཭ΛٻΊΔͱɺ (2-0+8)%8=2, (5-2+8)%8=3, (7-5+8)%8=2, (0-7+8)%8=1 ͱͳΔɻ ڑ཭͕4ͷ৔߹͸ 2+ڑ཭-1 ͕ɺڑ཭͕ͦΕҎ֎ͷ৔߹͸ 3+ڑ཭-1 ͕௖఺਺ɻ ௖఺਺ͷܭࢉ