solve_1.ml 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 rec is_safe status l = match status with
  11. | "ko" -> false
  12. | "increasing" -> (match l with
  13. | a :: b :: tl -> if ( (a < b) && ((abs (a - b)) < 4)) then (is_safe "increasing" (b::tl)) else (is_safe "ko" [])
  14. | _ :: [] | [] -> is_safe "ok" [])
  15. | "decreasing" -> (match l with
  16. | a :: b :: tl -> if ( (a > b) && ((abs (a - b)) < 4)) then (is_safe "decreasing" (b::tl)) else (is_safe "ko" [])
  17. | _ :: [] | [] -> is_safe "ok" [])
  18. | "init" -> (match l with
  19. | a :: b :: tl -> if ((abs (a - b)) < 4) then
  20. (if (a < b) then (is_safe "increasing" (b::tl))
  21. else
  22. (if (a == b) then (is_safe "ko" [])
  23. else (is_safe "decreasing" (b::tl))))
  24. else
  25. (is_safe "ko" [])
  26. | _ :: [] | [] -> is_safe "ok" [])
  27. | _ -> true (*any other value is considered ok*)
  28. ;;
  29. let rec solve accum = function
  30. | hd :: tl -> if (is_safe "init" hd) then solve (accum + 1) tl else solve accum tl
  31. | [] -> accum
  32. ;;
  33. let () =
  34. let li = construct_list [] in
  35. printf "Total: %d\n" (solve 0 li)