1. Shlomi Fish
  2. abc-path

Commits

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

Convert the layout to a vec() so it will run faster.

  • Participants
  • Parent commits a8e3da2
  • Branches default

Comments (0)

Files changed (1)

File abc-path/generator/generate-abs-path.pl

View file
 {
     my ($self, $pos, $xy) = @_;
 
+    my $l = $pos->{layout};
     my $in_range = sub { my $i = shift; return (($i >= 0) && ($i < $LEN)); };
 
     return
         my $applied_y = $xy->[$Y] + $m->[$Y];
 
         $in_range->($applied_x) && $in_range->($applied_y) &&
-        (!defined($pos->{layout}->[$applied_y]->[$applied_x]))
+        (vec($l, $applied_y*$LEN+$applied_x, 8) == 0)
         } ([-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1])
     ];
 }
 {
     my $self = shift;
 
-    my @initial_cell = $self->_to_xy($self->{rand}->range_rand($BOARD_SIZE));
+    my $init_xy = $self->{rand}->range_rand($BOARD_SIZE);
+    my @initial_cell = $self->_to_xy($init_xy);
+
+    my $init_layout = '';
+    vec($init_layout, $init_xy, 8) = 1;
+
     my $initial_position =
-    { layout => [map { [] } (0 .. $LEN-1)], last_pos => [@initial_cell]}
+    { layout => $init_layout, last_pos => [@initial_cell]}
     ;
 
-    $initial_position->{layout}->[$initial_cell[$Y]]->[$initial_cell[$X]] =
-        $letters[0];
-
     $self->_fill_available_moves($initial_position);
     
     my @dfs_stack = ($initial_position);
 
         my $last_state = $dfs_stack[-1];
 
+        my $l = $last_state->{layout};
         # TODO : remove these traces later.
         # print "Depth = " . scalar(@dfs_stack) . "\n";
         # print "Last state = " . Dumper($last_state) . "\n";
+        # print "Layout = \n" . $self->get_layout_as_string($last_state->{layout}) . "\n";
 
         {
-            my $l = $last_state->{layout};
+            my $first_int = first { vec($l, $_, 8) == 0 } (0 .. $BOARD_SIZE-1);
 
-            my $first_xy = first { my ($y,$x) = $self->_to_xy($_); 
-                !defined($l->[$y]->[$x]) } (0 .. $BOARD_SIZE-1);
-
-            my @connectivity_stack = ($first_xy);
+            my @connectivity_stack = ($first_int);
 
             my %connected;
             while (@connectivity_stack)
             $last_state->{last_pos}, $next_move
         );
 
+        my $next_layout = $l;
+        vec($next_layout, $self->_xy_to_int($next_pos), 8) = 1+@dfs_stack;
         my $next_state =
         {
-            layout => [ map { [@{$_}] } @{ $last_state->{layout} } ],
+            layout => $next_layout,
             last_pos => $next_pos,
         };
 
-        $next_state->{layout}->[$next_pos->[$Y]]->[$next_pos->[$X]] =
-            $letters[scalar @dfs_stack];
-
         $self->_fill_available_moves($next_state);
 
         push @dfs_stack, $next_state;
     my ($self, $l) = @_;
 
     my $render_row = sub {
-        my $row = shift;
+        my $y = shift;
 
-        return join(" | ", map { defined($_) ? $_ : '*' } @$row);
+        return join(" | ", 
+            map { my $x = $_; my $v = vec($l, $self->_xy_to_int([$y,$x]), 8);
+            $v ? $letters[$v-1] : '*' } (0 .. $LEN - 1));
     };
 
-    return join('', map { $render_row->($_) . "\n" } @$l);
+    return join('', map { $render_row->($_) . "\n" } (0 .. $LEN-1));
 }
 
 package main;