solve_1.ml 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. open Stdio
  2. open Str
  3. let rec construct_list l1=
  4. let line = In_channel.input_line In_channel.stdin in
  5. match line with
  6. | None -> l1
  7. | Some x -> let report = List.map int_of_string (Str.split (regexp {| |}) x) in
  8. construct_list (report::l1)
  9. ;;
  10. let monotony a b = if ((abs (a - b)) < 4) then
  11. (if (a < b) then "increasing" else
  12. (if (a > b) then "decreasing" else "ko"))
  13. else "ko"
  14. ;;
  15. let rec is_safe status l = match status with
  16. | "ko" -> false
  17. | "increasing" -> (match l with
  18. | a :: b :: tl -> if ((monotony a b) == "increasing") then (is_safe "increasing" (b::tl)) else (is_safe "ko" [])
  19. | _ :: [] | [] -> true)
  20. | "decreasing" -> (match l with
  21. | a :: b :: tl -> if ((monotony a b) == "decreasing") then (is_safe "decreasing" (b::tl)) else (is_safe "ko" [])
  22. | _ :: [] | [] -> true)
  23. | "init" -> (match l with
  24. | a :: b :: tl -> is_safe (monotony a b) (b::tl)
  25. | _ :: [] | [] -> true)
  26. | _ -> true (*any other value is considered ok*)
  27. ;;
  28. let rec solve accum = function
  29. | hd :: tl -> if (is_safe "init" hd) then solve (accum + 1) tl else solve accum tl
  30. | [] -> accum
  31. ;;
  32. let () =
  33. let li = construct_list [] in
  34. printf "Total: %d\n" (solve 0 li)