Commits

shl...@b384bcd7-cfd4-0310-aca0-d78b80f7b91b  committed b0ea99f

Moved more functions to be methods.

  • Participants
  • Parent commits 22eee90

Comments (0)

Files changed (1)

File abc-path/perl-quick-and-dirty/abc-path-solver.pl

 
 use Carp;
 
+my @letters = (qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y));
+
+my %letters_map = (map { $letters[$_] => $_ } (0 .. $#letters));
+
+sub get_letter_numeric
+{
+    my ($self, $letter_ascii) = @_;
+
+    my $index = $letters_map{$letter_ascii};
+
+    if (!defined ($index))
+    {
+        confess "Unknown letter '$letter_ascii'";
+    }
+
+    return $index;
+}
+
 sub new
 {
     my $class = shift;
 }
 
 
+sub set_verdicts_for_letter_sets
+{
+    my ($solver, $letter_list, $maybe_list) = @_;
+
+    my %cell_is_maybe =
+        (map {; sprintf("%d,%d", @$_) => 1; } @$maybe_list);
+
+    foreach my $letter_ascii (@$letter_list)
+    {
+        my $letter = $solver->get_letter_numeric($letter_ascii);
+
+        $solver->xy_loop(
+            sub {
+                my ($x, $y) = @_;
+
+                $solver->set_verdict($letter, $x, $y,
+                    ((exists $cell_is_maybe{"$x,$y"})
+                        ? $ABCP_VERDICT_MAYBE
+                        : $ABCP_VERDICT_NO
+                    )
+                );
+            }
+        );
+    }
+}
+
+
 package main;
 
 # This will handle 25*25 2-bit cells and the $ABCP_VERDICT_MAYBE / etc.
         }
     }
 }
-
-my %letters_map = (map { $letters[$_] => $_ } (0 .. $#letters));
-sub get_letter_numeric
-{
-    my $letter_ascii = shift;
-
-    my $index = $letters_map{$letter_ascii};
-
-    if (!defined ($index))
-    {
-        confess "Unknown letter '$letter_ascii'";
-    }
-
-    return $index;
-}
-
 # Now let's process the layout string and populate the verdicts table.
 
-sub set_verdicts_for_letter_sets
-{
-    my ($letter_list, $maybe_list) = @_;
-
-    my %cell_is_maybe =
-        (map {; sprintf("%d,%d", @$_) => 1; } @$maybe_list);
-
-    foreach my $letter_ascii (@$letter_list)
-    {
-        $solver->xy_loop(
-            sub {
-                my ($x, $y) = @_;
-
-                my $letter = get_letter_numeric($letter_ascii);
-                $solver->set_verdict($letter, $x, $y,
-                    ((exists $cell_is_maybe{"$x,$y"})
-                        ? $ABCP_VERDICT_MAYBE
-                        : $ABCP_VERDICT_NO
-                    )
-                );
-            }
-        );
-    }
-}
-
 sub set_conclusive_verdict_for_letter
 {
     my ($letter, $xy) = @_;
 
     push @major_diagonal_letters, $1;
 
-    set_verdicts_for_letter_sets(
+    $solver->set_verdicts_for_letter_sets(
         \@major_diagonal_letters, 
         [map { [$_,$_] } (0 .. 4)],
     )
 
     push @minor_diagonal_letters, substr($1,0,1);
 
-    set_verdicts_for_letter_sets(
+    $solver->set_verdicts_for_letter_sets(
         \@minor_diagonal_letters,
         [map { [$_, 4-$_] } (0 .. $BOARD_LEN_LIM)]
     );
 
     foreach my $x (0 .. $BOARD_LEN_LIM)
     {
-        set_verdicts_for_letter_sets(
+        $solver->set_verdicts_for_letter_sets(
             [substr($top_row, $x+1, 1), substr($bottom_row, $x+1, 1),],
             [map { [$x,$_] } (0 .. 4)],
         );
     foreach my $y (0 .. $BOARD_LEN_LIM)
     {
         my $row = $rows[$y+1];
-        set_verdicts_for_letter_sets(
+        $solver->set_verdicts_for_letter_sets(
             [substr($row, 0, 1), substr($row, -1),],
             [map { [$_,$y] } (0 .. $BOARD_LEN_LIM)],
         );
     }
     
     set_conclusive_verdict_for_letter(
-        get_letter_numeric($clue_letter),
+        $solver->get_letter_numeric($clue_letter),
         [$clue_x, $clue_y],
     );
 }