Sunday, August 11, 2013

Problem 22 - D

Another problem solved with a language chosen off of the list of language used by other PE users, another language that has very C-like syntax, although D was a little more exciting to work with than Squirrel. The biggest problem I had with this one was a mistake in my find/replace to reformat the text file (My program assumes one name per line, no quotes or commas).

import std.stdio;
import std.conv;

void main() {
    string[] arr;
    auto sum = 0;
    foreach (line; stdin.byLine()) {
        arr ~= to!string(line);
    }
    arr.sort;
    foreach (i, name; arr) {
        auto wordval = 0;
        foreach (c; name) {
            wordval += to!int(c) - to!int('A') + 1;
        }
        sum += wordval * (i + 1);
    }
    writeln(sum);
}

No comments:

Post a Comment