Solved ProjectEuler/022: php
This commit is contained in:
15
ProjectEuler/022/desc.yml
Normal file
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
1
ProjectEuler/022/input
Normal file
File diff suppressed because one or more lines are too long
12
ProjectEuler/022/solve.php
Normal file
12
ProjectEuler/022/solve.php
Normal file
@ -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;
|
Reference in New Issue
Block a user