blob: d992ac2f0b24c19976ec6c06a62e043e4f651e0f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
let err_empty_input = "input is empty"
let log_matrix matrix =
let open Shared.Matrix in
Logs.debug (fun m -> m "%s" (M.of_list matrix |> M.pp (fun elt -> elt)))
let log_list lst =
Logs.debug (fun m ->
m "[%s]" (List.fold_left (fun acc i -> acc ^ i ^ "; ") " " lst) )
type operation = Mult | Add
let solve input () =
if input = "" then Error err_empty_input
else
let input' = Shared.Parser.columns_of_string input in
assert (input' <> []) ;
log_matrix input' ;
let nums =
List.map (List.filter (fun i -> i <> "*" && i <> "+")) input'
|> List.map
@@ List.map (fun n ->
Logs.debug (fun m -> m "peeking: %s" n) ;
int_of_string n )
and ops =
List.map (List.filter (fun i -> i = "*" || i = "+")) input'
|> List.concat
|> List.map (fun op ->
match op with
| "*" ->
Mult
| "+" ->
Add
| _ ->
failwith "case not covered" )
in
let answer =
List.map2
(fun op num_lst ->
match op with
| Add ->
List.fold_left (fun acc i -> acc + i) 0 num_lst
| Mult ->
List.fold_left (fun acc i -> acc * i) 1 num_lst )
ops nums
|> fun lst ->
log_list @@ List.map string_of_int lst ;
lst |> List.fold_left ( + ) 0
in
Ok answer
|