Skip to content

Commit

Permalink
Refactors 022 with sum & mapi ~f:(name_score)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankKair committed Mar 7, 2020
1 parent a181242 commit e627ccd
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/022/p022.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ let load_names =
|> String.split ~on:','
|> List.sort ~compare:(Stdlib.compare)

let sum = List.fold ~f:(+) ~init:(0)

let name_score index name =
let char_val c = Char.to_int c - 64 in
name
|> Bytes.of_string
|> Bytes.to_list
|> List.map ~f:(fun c -> (index + 1) * char_val c)
|> List.fold ~f:(+) ~init:(0)
|> sum

let solve =
load_names
|> List.mapi ~f:(fun i s -> name_score i s)
|> List.fold ~f:(+) ~init:(0)
|> List.mapi ~f:(name_score)
|> sum

let () =
printf "%i\n" solve;;

0 comments on commit e627ccd

Please sign in to comment.