Skip to content

Commit 7342761

Browse files
committed
Refactors 022 with sum & mapi ~f:(name_score)
- Also puts replace outside the scope of load_names function
1 parent a181242 commit 7342761

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/022/p022.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
open Core_kernel
22
open Stdio
33

4-
let load_names =
5-
let replace ~pattern ~with_str =
6-
Str.global_replace (Str.regexp_string pattern) with_str in
4+
let replace ~pattern ~with_str =
5+
Str.global_replace (Str.regexp_string pattern) with_str
76

7+
let load_names =
88
In_channel.read_all "p022_names.txt"
99
|> replace ~pattern:"\"" ~with_str:""
1010
|> String.split ~on:','
1111
|> List.sort ~compare:(Stdlib.compare)
1212

13+
let sum = List.fold ~f:(+) ~init:(0)
14+
1315
let name_score index name =
1416
let char_val c = Char.to_int c - 64 in
1517
name
1618
|> Bytes.of_string
1719
|> Bytes.to_list
1820
|> List.map ~f:(fun c -> (index + 1) * char_val c)
19-
|> List.fold ~f:(+) ~init:(0)
21+
|> sum
2022

2123
let solve =
2224
load_names
23-
|> List.mapi ~f:(fun i s -> name_score i s)
24-
|> List.fold ~f:(+) ~init:(0)
25+
|> List.mapi ~f:(name_score)
26+
|> sum
2527

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

0 commit comments

Comments
 (0)