From 0248ed34ce3d05228bc5084669c3a27933be0c2e Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Sun, 4 Oct 2009 00:56:56 +0100 Subject: first commit --- problem22.erl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 problem22.erl (limited to 'problem22.erl') 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. -- cgit v1.2.1