Shlomi Fish avatar Shlomi Fish committed 19ed866

Add the solution to Euler #90.

Comments (0)

Files changed (1)

project-euler/90/euler-90.pl

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use List::MoreUtils ();
+
+my $max_digit = 9;
+
+my %die_combs =
+(
+    1 => [qw(0 1 2 3 4 5 6 7 8 9)],
+    2 => [qw(1 0 2 3 4 5 6 7 8 9)],
+);
+
+my @digits_map = (qw(0 1 2 3 4 5 69 7 8 96));
+
+my %found = ();
+
+sub get_regex
+{
+    my ($die_num, $indices) = @_;
+
+    return '[' . join("", sort { $a cmp $b }
+        @digits_map[@{$die_combs{$die_num}}[@$indices]]
+    ) . ']';
+}
+
+sub recurse_die1
+{
+    my ($indices) = @_;
+    
+    if (@$indices == 6)
+    {
+        return recurse_die2(get_regex(1, $indices), [0]);
+    }
+    
+    foreach my $next_idx ($indices->[-1]+1 .. $max_digit)
+    {
+        recurse_die1([@$indices, $next_idx]);
+    }
+
+    return;
+}
+
+sub recurse_die2
+{
+    my ($reg_comp1, $indices) = @_;
+
+    if (@$indices == 6)
+    {
+        my $reg_comp2 = get_regex(2, $indices);
+        
+        my $re = qr/\A(?:$reg_comp1$reg_comp2)|(?:$reg_comp2$reg_comp1)\z/;
+
+        if (List::MoreUtils::all { /$re/ } qw(01 04 09 16 25 36 49 64 81))
+        {
+            $found{join("", sort { $a cmp $b } ($reg_comp1, $reg_comp2))}++;
+        }
+        return;
+    }
+        
+    foreach my $next_idx ($indices->[-1]+1 .. $max_digit)
+    {
+        recurse_die2($reg_comp1, [@$indices, $next_idx]);
+    }
+
+    return;
+}
+
+recurse_die1([0]);
+
+print map { "$_\n" } keys(%found);
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.