solve_2.ml 896 B

1234567891011121314151617181920212223242526
  1. open Stdio
  2. open Str
  3. let rec construct_list l1 l2 =
  4. let line = In_channel.input_line In_channel.stdin in
  5. match line with
  6. | None -> (l1, l2)
  7. | Some x -> if (string_match (regexp {|\([0-9]+\) *\([0-9]+\)|}) x 0) then
  8. construct_list ((Float.to_int (Float.of_string (matched_group 1 x)))::l1)
  9. ((Float.to_int (Float.of_string (matched_group 2 x)))::l2)
  10. else
  11. (l1, l2)
  12. let rec count_occurence x accum = function
  13. | [] -> accum
  14. | hd :: tl -> if (hd == x) then count_occurence x (accum + 1) tl else count_occurence x accum tl
  15. let rec solve accum = function
  16. | (hd1::tl1, li2) -> let nb_occurences = count_occurence hd1 0 li2 in
  17. solve (accum + (hd1 * nb_occurences)) (tl1, li2)
  18. | ([],_) -> accum
  19. let () =
  20. let (li1,li2) = construct_list [] [] in
  21. printf "Total: %d\n" (solve 0 (li1,li2))