Project Euler Problem 42
Project Euler Problem 42 involved some text parsing -- a nice change of pace. I went on a bit of a binge last night and solved 6 problems, but they were all erpa-derpa-factor-erpa-derpa-sieve, so I didn't bother posting.. as, we'll, they're boring. This one gave me an excuse to write some perverse perl, though, so here goes; I have no shame.
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
- #!/usr/bin/perl -w
- use strict;
- # clean up our input and create a hash of the form {55=>[sky]}
- # to track association between numerical values and words
- my $words;
- while(<STDIN>) {
- $_ =~ s/\"//g;
- foreach my $word (@row) {
- }
- }
- # from the above hash, find the maximum possible numerical value.
- # calculate triangle numbers up to that point, and keep track
- # of how many words are associated with their values.
- my $cur_value = 0;
- my $words_found = 0;
- for(my $i = 0; $cur_value < $max_value; $i++) {
- $cur_value = ($i*($i+1)/2);
- $words_found += @{$words->{$cur_value}} if($words->{$cur_value});
- }
- # calculate a given word's numerical value.
- sub word_value {
- }