Created
October 30, 2020 13:01
-
-
Save sta1r/c87f207e14806112be5ff84ce968138d to your computer and use it in GitHub Desktop.
Halloween Code Golf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Spooky | |
{ | |
static $halloweenWords = ["apparition","bat","bloodcurdling","bloodsucker","bloody","bones","boogeyman","broomstick","cadaver","carved","cauldron","cemetery","chainsaw","cobweb","coffin","corpse","crypt","cursed","decapitated","decomposing","demon","dotdigital","enchanted","evil","exorcism","fangs","ghost","ghoulish","goblin","gory","gravestone","graveyard","grim","gruesome","halloween","happy","haunted","haunting","howling","incantation","knife","levitation","lurking","medium","monsters","mummified","mummy","occult","orbs","paranormal","petrified","phantom","pitchfork","poltergeist","possessed","reanimated","ritual","satanic","scary","screaming","shadow","shaking","skeleton","skull","sorcery","specter","spell","spider","spirit","spooked","terrified","terror","toad","trembling","troll","undead","unearthly","vampire","werewolf","witchcraft","wizard","wraith","zombie"]; | |
static $outputPhrase = ''; | |
public static function decipher($c) | |
{ | |
self::tryBlockSize($c); | |
return self::$outputPhrase; | |
} | |
private static function tryBlockSize($c, $start = 2) | |
{ | |
$blockSize = self::guessBlockSize(strlen($c), $start); | |
$string = self::unTangle($c, $blockSize); | |
self::parseString($string); | |
if (self::$outputPhrase === '') { | |
$blockSize = self::tryBlockSize($c, $start + 1); | |
} | |
} | |
private static function unTangle($cipher, $size) | |
{ | |
$string = ''; | |
$columns = str_split($cipher, (strlen($cipher) / $size)); | |
foreach ($columns as $i => $column) { | |
$charactersInColumn[$i] = str_split($column, 1); | |
} | |
for ($i = 0; $i <= count($charactersInColumn[0]); $i++) { | |
$string .= implode(array_column($charactersInColumn, $i)); | |
} | |
return $string; | |
} | |
private static function parseString($string) { | |
foreach (self::$halloweenWords as $word) { | |
$pos = strpos($string, $word); | |
if ($pos === 0) { | |
self::$outputPhrase .= $word; | |
$remainder = substr($string, strlen($word)); | |
if (strlen($remainder) === 0) { | |
return; | |
} | |
self::$outputPhrase .= ' '; | |
self::parseString($remainder); | |
} | |
} | |
} | |
private static function guessBlockSize($cLength, $startAt = 2) | |
{ | |
for ($x = $startAt; $x < 10; $x++) { | |
$mod = fmod($cLength, $x); | |
if ($mod === 0.0) { | |
return $x; | |
} | |
} | |
return 0; | |
} | |
} | |
$c = "haeialngpldipootywtahedl"; | |
echo Spooky::decipher($c); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment