Algorithm-X-DLX

 view release on metacpan or  search on metacpan

examples/t/sudoku/03-sudokusolver.t  view on Meta::CPAN

  plan tests => 81;
  # 2 clones
  my ($solved, $sudoku) = map {Sudoku->new($_)} ((join '',
    "+---+---+---+\n",
    "|681|739|245|\n",
    "|497|562|813|\n",
    "|523|841|769|\n",
    "+---+---+---+\n",
    "|172|954|386|\n",
    "|865|317|924|\n",
    "|349|628|571|\n",
    "+---+---+---+\n",
    "|916|283|457|\n",
    "|234|175|698|\n",
    "|758|496|132|\n",
    "+---+---+---+\n"
  ) x 2);
  
  for (my $i = 0; $i < $sudoku->size(); ++$i) {
    my $digit = $sudoku->get_value($i);
    $sudoku->set_value($i, 0);
    is_deeply( $solved, SudokuSolver::solve($sudoku));
    $sudoku->set_value($i, $digit);
  }
};

subtest 'easy' => sub {
  plan tests => 2;

  my $easy = Sudoku->new(join '',
    "+---+---+---+\n",
    "|14.|8..|97.|\n",
    "|..6|75.|...|\n",
    "|7..|...|..8|\n",
    "+---+---+---+\n",
    "|5..|4.2|.93|\n",
    "|93.|.7.|.82|\n",
    "|62.|9.8|..4|\n",
    "+---+---+---+\n",
    "|4..|...|..9|\n",
    "|...|.15|8..|\n",
    "|.72|..4|.15|\n",
    "+---+---+---+\n"
  );
  my $solved = Sudoku->new(join '',
    "+---+---+---+\n",
    "|145|823|976|\n",
    "|286|759|431|\n",
    "|793|146|528|\n",
    "+---+---+---+\n",
    "|518|462|793|\n",
    "|934|571|682|\n",
    "|627|938|154|\n",
    "+---+---+---+\n",
    "|451|387|269|\n",
    "|369|215|847|\n",
    "|872|694|315|\n",
    "+---+---+---+\n"
  );
  # there is no 'isnt_deeply' in Test::More
  require Data::Dumper; 
  my @cmp = map {Data::Dumper->new([$_])->Sortkeys(1)->Indent(0)->Dump()} ($solved, $easy);
  isnt( $cmp[0], $cmp[1], 'different from start' );
  is_deeply( $solved, SudokuSolver::solve($easy), 'correct solution');
};

subtest 'hard' => sub {
  plan tests => 1;
  my $hard = Sudoku->new(join '',
    "050|002|000",
    "000|100|400",
    "700|000|000",
    "---|---|---",
    "010|700|080",
    "004|030|060",
    "000|500|000",
    "---|---|---",
    "308|060|000",
    "000|000|100",
    "600|000|000"
  );
  my $solved = Sudoku->new(join '',
    "159|482|673",
    "836|175|429",
    "742|693|518",
    "---|---|---",
    "213|746|985",
    "584|239|761",
    "967|518|342",
    "---|---|---",
    "378|961|254",
    "495|827|136",
    "621|354|897"
  );
  is_deeply( $solved, SudokuSolver::solve($hard), 'correct solution');
};

subtest 'region 2x2' => sub {
  plan tests => 1;
  my $puzzle = Sudoku->new(join '',
    "21|..",
    ".3|2.",
    "--+--",
    "..|.4",
    "1.|.."
  );
  my $solved = Sudoku->new(join '',
    "21|43",
    "43|21",
    "--+--",
    "32|14",
    "14|32"
  );
  is_deeply( $solved, SudokuSolver::solve($puzzle), 'correct solution');
};

subtest 'region 3x2' => sub {
  plan tests => 1;
  my $puzzle = Sudoku->new(join '',
    "5.6|...\n",
    "...|.2.\n",
    "---+---\n",



( run in 0.498 second using v1.01-cache-2.11-cpan-39bf76dae61 )