solve_1.ml 823 B

1234567891011121314151617181920212223
  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 -> (List.sort compare l1, List.sort compare l2)
  7. | Some x -> if (string_match (regexp {|\([0-9]+\) *\([0-9]+\)|}) x 0) then
  8. construct_list ((Float.of_string (matched_group 1 x))::l1)
  9. ((Float.of_string (matched_group 2 x))::l2)
  10. else
  11. (List.sort compare l1, List.sort compare l2)
  12. let rec solve accum = function
  13. | (x::tl1, y::tl2) -> solve (accum + (abs
  14. (int_of_float (x -.y))
  15. )) (tl1, tl2)
  16. | ([],[]) | (_,[]) | ([],_) -> accum
  17. let () =
  18. let (li1,li2) = construct_list [] [] in
  19. printf "Total: %d\n" (solve 0 (li1,li2))