$_ = "3223*4224*6666*3553*4014";

@pats = (
's/220(..)221/022${1}122/',
's/022(..)122/220${1}221/',
's/01(...)22(...)22/22${1}22${2}01/',
's/22(...)22(...)01/01${1}22${2}22/',
's/30(...)41/03${1}14/',
's/03(...)14/30${1}41/',
's/0(....)3(....)4/3${1}4${2}0/',
's/1(....)3(....)4/3${1}4${2}1/',
's/3(....)4(....)0/0${1}3${2}4/',
's/3(....)4(....)1/1${1}3${2}4/',
's/55([01])/${1}55/',
's/([01])55/55${1}/',
's/01(...)55/55${1}01/',
's/55(...)01/01${1}55/',
's/60/06/',
's/61/16/',
's/06/60/',
's/16/61/',
's/0(....)6/6${1}0/',
's/1(....)6/6${1}1/',
's/6(....)0/0${1}6/',
's/6(....)1/1${1}6/',
);

$states = 0;
unlink 'hakoiri.pag', 'hakoiri.dir', 'hakoiri.db';
dbmopen(d,"hakoiri",0666);
$d{'statestr',$states} = $_;
$d{'strstate',$_} = $states;
$states++;
 
for($i=0;$i<$states;$i++){
  $s = $d{'statestr',$i};
  for $pat (@pats){
    $_ = $s;
    if(eval $pat){
      s/([01])(.*)([01])/0${2}1/;
      if($d{'strstate',$_} eq ”){
        $d{'prev',$states} = $i;
        $d{'statestr',$states} = $_;
        $d{'strstate',$_} = $states;
        if(/22.$/){
          dbmclose(d);
          exit;
        }
        $states++;
          if $states % 1000 == 0
      }
    }
  }
}