Project Euler Problem 22

More sum-a-bunch-of-absurdly-large-numbers fun in Project Euler Problem 22. We're given a 46KB text file full of names, a way of assigning each one a value, and told to give some sums.

Here's a perl solution -- maybe one day I'll get around to writing these in another language.

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use Data::Dumper;
  5.  
  6. my @names;
  7.  
  8. while(<STDIN>) {
  9.   my @temp = split(/,/, $_);
  10.   foreach my $name (@temp) {
  11.     $name =~ s/"//g;
  12.     push(@names, $name);
  13.   }
  14. }
  15.  
  16. my @sorted = sort { $a cmp $b } @names;
  17.  
  18. my $i     = 1;
  19. my $total = 0;
  20. foreach my $name (@sorted) {
  21.   my $sum = 0;
  22.   foreach my $c (split(//, $name)) {
  23.     $sum += int(ord($c))-64;
  24.   }
  25.   $total += $sum*$i;
  26.   printf("%d: %s: %d: %d\n", $i, $name, $sum, $total);
  27.   $i++;
  28. }