1 files changed, 37 insertions(+), 0 deletions(-)
A day_2.ml
A day_2.ml => day_2.ml +37 -0
@@ 0,0 1,37 @@
+let count chr str =
+ (-) (List.length (String.split_on_char chr str)) 1;;
+
+let bool_to_int b =
+ match b with
+ | true -> 1
+ | false -> 0;;
+
+let validator1 min_occ max_occ chr str =
+ let occ = count chr str in
+ bool_to_int (min_occ <= occ && occ <= max_occ);;
+
+let validator2 idx1 idx2 chr str =
+ bool_to_int ((str.[idx1 - 1] = chr) <> (str.[idx2 - 1] = chr));;
+
+let is_valid (f : int -> int -> char -> string -> int) (line : string) =
+ Scanf.sscanf line "%d-%d %c: %s" f;;
+
+let rec count_valid lst f =
+ match lst with
+ | [] -> 0
+ | head::tail -> (is_valid f head) + (count_valid tail f);;
+
+let rec read_input chan =
+ match (try Some (input_line chan) with End_of_file -> None) with
+ | Some line -> line :: read_input chan
+ | None -> [];;
+
+let in_chan = open_in "day_2.in";;
+let lst = read_input in_chan;;
+close_in in_chan;;
+let out_chan1 = open_out "day_2_1.out";;
+let out_chan2 = open_out "day_2_2.out";;
+Printf.fprintf out_chan1 "%d\n" (count_valid lst validator1);;
+Printf.fprintf out_chan2 "%d\n" (count_valid lst validator2);;
+close_out out_chan1;;
+close_out out_chan2;;