Yet Another TextTwist Solver

So, couple days ago, i found this nice Perl script to solve TextTwist games in Priyadi’s blog. And i said to myself, “hmm, i think i can make it too in PHP”. So here you go,

<?php

  function yatts($words)
  {
    $pspell_link = pspell_new("en");

    $aword=str_split($words);
    $length = count($aword);

    $res=array();
    for ( $sublength=3;$sublength<$length;$sublength++ )
    {
      for ( $k=1,$max=1;$k<=$length;$k++ )
        $max=$k*$max;

      for ($a=1; $a<($length+1); $a++) {
        $options[$a] = $pattern[$a] = $a;
      }

      for ($x=0; $x<$max; $x++)
      {
        $N = $x;
        for ($i=1; $i<= $length;) {
          $pattern[($length+1-$i)] = $N % $i;
          $N = $N/$i;
          $i++;
        } 

        unset($perm);
        $b = $options;

        foreach($pattern as $offset) {
          list($char) = array_splice($b, $offset, 1);
          $perm[] = $char;
        } 

        $m = array_slice($perm, 0, $sublength);   

        $jj='';
        foreach ( $m as $v )
          $jj.=$aword[$v-1];

        if ( !in_array($jj,$res) ) {
          if ( pspell_check($pspell_link,$jj) ) {
            $res[]=$jj;
          }
        }
      }
    }

    return $res;
  }

?>

All you have to do is write something like this,

<?php

  $res = yatts('ltorslt');
  echo implode(' ',$res);

?>

where “lorstl” is the letters presented in the game, and the output would be,

lot tor tot ors rot sol sot lots lost toll tors tort tots trot roll
rots slot sort tolls torts troll trots rolls trolls stroll

Yep, grab one and make scores. 🙂

Notes:

  • To make this script works you need to compile PHP with pspell support. Read the manual for that.
  • You also need to install Aspell program and its english dictionary, get it here.
  • For windows user, you’re going to need dos2unix program for windows. Somehow, some Aspell binary files can not be read because it’s in DOS format. Just type this in your DOS-prompt,

    C:\Program Files\Aspell\data> dos2unix iso8859-1.dat
    C:\Program Files\Aspell\data> dos2unix standard.kbd

Join the Conversation

2 Comments

  1. Thank so much for this article! I was looking for the proper format of iso8859-1.dat and I couldn’t find anything until this.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.