Solved ProjectEuler/022: php

This commit is contained in:
FuryFire 2012-03-12 10:53:30 +01:00
parent 5dc80b4e9f
commit 6c8a2f23ff
3 changed files with 28 additions and 0 deletions

15
ProjectEuler/022/desc.yml Normal file

@ -0,0 +1,15 @@
title: What is the total of all the name scores in the file of first names?
url: http://projecteuler.net/problem=22
desc: |
Using input, a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 53 = 49714.
What is the total of all the name scores in the file?
(See file input)
|
solution: Bruteforce
solutions:
solve.php:
desc: Expects data on STDIN
language: php

1
ProjectEuler/022/input Normal file

File diff suppressed because one or more lines are too long

@ -0,0 +1,12 @@
<?php
$names = explode('","',substr(file_get_contents('php://STDIN'),1,-1));
sort($names);
foreach($names as $line=>$name) {
for($c =0; $c < strlen($name); $c++) {
$char_sum += ord($name[$c]) - 64;
}
$result += $char_sum * ($line+1);
$char_sum = 0;
}
echo $result;