Algorithm-X-DLX

 view release on metacpan or  search on metacpan

examples/sudoku/sudoku.pl  view on Meta::CPAN

      next unless eof(STDIN);
    }
  }

  $input or next;

  eval {
    my $type = SudokuType::guess($input);

    my $format;
    if ($opt_format eq 'PRESERVE') {
      $format = SudokuFormat->new($type, $input);
    } elsif ($opt_format eq 'COMPACT') {
      $format = SudokuFormat->compact($type);
    } elsif ($opt_format eq 'ONELINE') {
      $format = SudokuFormat->oneline($type);
    } else {
      $format = SudokuFormat->new($type);
    }

    if (!$first && $opt_format ne 'ONELINE') {
      print "\n";
    }
    $first = 0;

    my $sudoku = Sudoku->new($type, $input);
    if ($sudoku->is_empty()) {
      $sudoku = $generator->generate($type);
    }

    my $solved = SudokuSolver::solve($sudoku);
    if (!$opt_print_solved_only && $opt_side_by_side) {
      print_side_by_side($sudoku->to_string_format($format), $solved->to_string_format($format));

    } else {
      if (!$opt_print_solved_only) {
        print $sudoku->to_string_format($format);
        if ($opt_format ne 'ONELINE') {
          print "\n";
        }
      }
      print $solved->to_string_format($format);
    }
  };

  if ($@) {
    print STDERR "\nERROR! $@\n";
    print STDERR $input, "\n";
  }

  $input = '';
}

sub print_side_by_side {
  my ($left, $right) = @_;

  my @ls = split("\n", $left);
  my @rs = split("\n", $right);
  my $max_left = 0;
  foreach my $l (@ls) {
    $max_left = length($l) if length($l) > $max_left;
  }

  my $max_lines = scalar(@ls) > scalar(@rs) ? scalar(@ls) : scalar(@rs);
  for (my $y = 0; $y < $max_lines; $y++) {
    my $pos = 0;
    if ($y < scalar(@ls)) {
      print $ls[$y];
      $pos = length($ls[$y]);
    }
    if ($y < scalar(@rs)) {
      print ' ' x (4 + $max_left - $pos);
      print $rs[$y];
    }
    print "\n";
  }
}



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