Shlomi Fish avatar Shlomi Fish committed a1d92d5

Add more to 118

Comments (0)

Files changed (1)

project-euler/118/euler-118.pl

 
 foreach my $rank (2 .. 8)
 {
-    # Configure 
+    my $sets = {};
+    # Make out sets of sub sets.
+    foreach my $sub_rank (1 .. int($rank/2))
+    {
+        my $other_sub_rank = $rank-$sub_rank;
+
+        my $sub_sets = $sets[$sub_rank];
+        my $other_sub_sets = $sets[$other_sub_rank];
+
+        my $compose = sub {
+            my ($sub_vec, $other_sub_vec) = @_;
+            # Check if the sets are mutually exclusive so they can be
+            # composed.
+            if (($sub_vec & $other_sub_vec) eq '')
+            {
+                my $total_vec = ($sub_vec|$other_sub_vec);
+
+                my $record = ($sets{$total_vec} ||= {c => 0, m => []});
+
+                $record->{c} += 
+                    ($sub_sets{$sub_vec}->{c} * 
+                        $other_sub_sets{$other_sub_vec}->{c}
+                    );
+
+                push @{ $record->{'m'} }, +{s => [$sub_vec, $other_sub_vec]};
+            }
+
+            return;
+        };
+
+        if ($sub_rank == $other_sub_rank)
+        {
+            my @sub_keys = keys(%$sub_sets);
+            foreach my $idx (0 .. $#sub_keys-1)
+            {
+                foreach my $other_idx ($idx+1 .. $#sub_keys)
+                {
+                    $compose->(@sub_keys[$idx,$other_idx]);
+                }
+            }
+        }
+        else
+        {
+            foreach my $sub_vec (keys(%$sub_sets)
+            {
+                foreach my $other_sub_vec (keys (%$other_sub_sets))
+                {
+                    $compose->($sub_vec, $other_sub_vec);
+                }
+            }
+        }
+    }
 }
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.