Commits

Shlomi Fish committed 4f78fa6

Working now.

Comments (0)

Files changed (1)

project-euler/186/euler_186.pl

 use strict;
 use warnings;
 
-use integer;
 use bytes;
 
 package Rand;
 
     my $r = Rand->new;
     my $count = 0;
+    my $max_len = 0;
+    my $num_users = 0;
     while (not (
             defined($friends[$PM_PHONE])
                 &&
-            (length(${$friends[$PM_PHONE]}) >= (4*99*1_000_000 / 100))
+            (length(${$friends[$PM_PHONE]}) >= ((4*99*1_000_000) / 100))
         )
     )
     {
                 vec($new_vec, 0, 32) = $p[0];
                 vec($new_vec, 1, 32) = $p[1];
                 $friends[$p[0]] = $friends[$p[1]] = \$new_vec;
+                $num_users += 2;
             }
             elsif (!defined($friends[$p[1]]))
             {
                 # If one is undefined
                 my $v = $friends[$p[0]] ;
                 vec ($$v, (length($$v) >> 2), 32) = $p[1];
-                $friends[$p[0]] = $v;
+                $friends[$p[1]] = $v;
+                $num_users++;
             }
             else
             {
                 if ($v0 ne $v1)
                 {
                     # Merge them if they are not identical.
-                    my $new_v = $$v0 . $$v1;
 
-                    my $new_v_ref = \$new_v;
+                    # Make sure $v1 is the larger.
+                    if (length($$v0) > length($$v1))
+                    {
+                        ($v0, $v1) = ($v1, $v0);
+                    }
+                    $$v1 .= $$v0;
 
-                    for my $idx (0 .. ((length($new_v) >> 2)-1))
+                    for my $idx (0 .. ((length($$v0) >> 2)-1))
                     {
-                        $friends[vec($new_v, $idx, 32)] = $new_v_ref;
+                        $friends[vec($$v0, $idx, 32)] = $v1;
+                    }
+
+                    if (length($$v1) > $max_len)
+                    {
+                        $max_len = length($$v1);
+                        print "Reached $max_len Count = $count\n";
                     }
                 }
             }