aboutsummaryrefslogtreecommitdiff
path: root/problem22.erl
diff options
context:
space:
mode:
authorNathan Lasseter <nathan.je.lasseter@googlemail.com>2009-10-04 00:56:56 +0100
committerNathan Lasseter <nathan.je.lasseter@googlemail.com>2009-10-04 00:56:56 +0100
commit0248ed34ce3d05228bc5084669c3a27933be0c2e (patch)
treeb9c4990344b0863a2af851b5917c49f3a5f03e6b /problem22.erl
parent00686037d57eb0a0d5eba6eeb6fcc9692e8b59f6 (diff)
first commit
Diffstat (limited to 'problem22.erl')
-rwxr-xr-xproblem22.erl28
1 files changed, 28 insertions, 0 deletions
diff --git a/problem22.erl b/problem22.erl
new file mode 100755
index 0000000..5017424
--- /dev/null
+++ b/problem22.erl
@@ -0,0 +1,28 @@
+-module(problem22).
+-export([solve/0]).
+
+solve() ->
+ word_values(read_words("names.txt"), 1, 0).
+
+word_values([], _, V) ->
+ V;
+word_values([H|T], N, V) ->
+ word_values(T, N + 1, V + (N * value(H))).
+
+read_words(Filename) ->
+ {ok, File} = file:read_file(Filename),
+ L = binary_to_list(File),
+ N = string:tokens(L, ","),
+ lists:sort(fun(A,B) -> A < B end, N).
+
+value([H|T]) ->
+ if
+ (H > 64) and (H < 91) ->
+ (H - 64) + value(T);
+ (H > 96) and (H < 123) ->
+ (H - 96) + value(T);
+ true ->
+ value(T)
+ end;
+value([]) ->
+ 0.