Shlomi Fish avatar Shlomi Fish committed ce8072a

Now No. 126 appears to yield the right results.

However, the naive approach appears to be too slow.

Comments (0)

Files changed (1)

project-euler/126/euler-126.pl

 
 use integer;
 
-use List::MoreUtils qw(any);
+use List::MoreUtils qw(any all);
 
 my @C;
 
 # reached layer.
 my %cuboids;
 
+my $max_C_n = 0;
+
 sub add_layer
 {
     my ($x, $y, $z) = @_;
             {
                 @coords = ($xx, $yy, $zz);
 
-                if ($array->[$xx+1]->[$yy+1]->[$zz+1])
+                if ($xx and $yy and $zz and $array->[$xx-1]->[$yy-1]->[$zz-1])
                 {
                     $new_array->[$xx]->[$yy]->[$zz] = 1;
                 }
                 elsif (any {
+                        my $deltas = $_;
+                        my @nc = (map { $coords[$_]-1+$deltas->[$_] } (0 .. 2));
+
+                        ((all { $_ >= 0 } @nc) and
                         $array
-                        ->[$coords[0]+1+$_->[0]]
-                        ->[$coords[1]+1+$_->[1]]
-                        ->[$coords[2]+1+$_->[2]]
+                        ->[$nc[0]]
+                        ->[$nc[1]]
+                        ->[$nc[2]]
+                        )
                     } ([0,0,1],[0,0,-1],[0,1,0],[0,-1,0],[1,0,0],[-1,0,0])
                 )
                 {
 
     $cuboids{$key} = {array => $new_array, dims => \@new_dims, n => $new_layer_count};
 
-    if ((++$C[$new_layer_count]) >= 10)
+    if ((++$C[$new_layer_count]) > $max_C_n)
     {
         print "Found C[$new_layer_count] == $C[$new_layer_count]\n";
-        # exit(0);
+        $max_C_n = $C[$new_layer_count];
+
+        if ($max_C_n == 1000)
+        {
+            exit(0);
+        }
     }
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.