Shlomi Fish avatar Shlomi Fish committed ecfea35

Add the solution to problem #93.

It's working.

Comments (0)

Files changed (1)

project-euler/93/93.pl

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use IO::Handle;
+use Data::Dumper;
+
+STDOUT->autoflush(1);
+
+my %op_funcs = (
+    '+' => sub { $_[0]+$_[1] },
+    '-' => sub { $_[0]-$_[1] }, 
+    '*' => sub { $_[0]*$_[1] },
+    '/' => sub { return (($_[1]+0 != 0) ? ($_[0]/$_[1]) : undef); },
+);
+
+sub recurse_op
+{
+    my ($op, $rest) = @_;
+
+    my %ret;
+
+    my $cb = $op_funcs{$op};
+
+    if (@$rest == 4)
+    {
+        foreach my $e1 (0 .. 2)
+        {
+            foreach my $e2 ($e1+1 .. 3)
+            {
+                my $s1 = recurse([@$rest[$e1,$e2]]);
+                my $s2 = recurse([@$rest[
+                    grep { ($_ != $e1) && ($_ != $e2) } (0 .. $#$rest)
+                ]]);
+
+                foreach my $k1 (keys(%$s1))
+                {
+                    foreach my $k2 (keys(%$s2))
+                    {
+                        foreach my $v (
+                            $cb->($k1, $k2), 
+                            $cb->($k2, $k1)
+                        )
+                        {
+                            if (defined($v))
+                            {
+                                $ret{$v} = 1;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    foreach my $alone (0 .. $#$rest)
+    {
+        my $calc = recurse([@$rest[grep { $_ != $alone} (0 .. $#$rest)]]);
+        foreach my $k (keys(%$calc))
+        {
+            foreach my $v (
+                $cb->($k, $rest->[$alone]), 
+                $cb->($rest->[$alone], $k)
+            )
+            {
+                if (defined($v))
+                {
+                    $ret{$v} = 1;
+                }
+            }
+        }
+    }
+
+    return \%ret;
+}
+
+sub recurse
+{
+    my ($rest) = @_;
+ 
+    if (@$rest == 1)
+    {
+        return { $rest->[0] => 1 };
+    }
+    
+    my %ret;
+    foreach my $op (qw(+ - / *))
+    {
+        %ret = (%ret, %{recurse_op($op, $rest)});
+    }
+
+    return \%ret;
+}
+
+my @max_n;
+my $max_n_seq_len = 0;
+
+print Dumper(recurse([1,2,3,4]));
+
+foreach my $x (0 .. 6)
+{
+    foreach my $y ($x+1 .. 7)
+    {
+        foreach my $z ($y+1 .. 8)
+        {
+            foreach my $t ($z+1 .. 9)
+            {
+                my @n = ($x,$y,$z,$t);
+                
+                my $ret = recurse(\@n);
+
+                my $seq_len = 1;
+                while (exists($ret->{$seq_len}))
+                {
+                    $seq_len++;
+                }
+                $seq_len--;
+                
+                if ($seq_len > $max_n_seq_len)
+                {
+                    @max_n = @n;
+                    $max_n_seq_len = $seq_len;
+                    print "Found @max_n with $max_n_seq_len\n";
+                }
+            }
+        }
+    }
+}
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.