adventofcode/2024/day2/main.ml
2024-12-04 22:32:49 +01:00

45 lines
1.1 KiB
OCaml

(** Get input *)
let from input =
let ic = open_in input in
(* Read a file *)
let rec read_line acc =
try
let line = input_line ic in
read_line (line :: acc)
with
| End_of_file ->
close_in ic;
acc
in
(* Lis une ligne et la découpe proprement *)
let scan line = String.split_on_char ' ' line in
(* Convertis les string en nombre *)
let convert l = List.map int_of_string l in
let list = List.map scan (read_line []) in
List.map convert list |> List.rev
;;
let first data =
let rec is_safe_adj = function
| x :: y :: tl -> if Int.abs (x - y) > 2 then true else is_safe_adj (y :: tl)
| _ :: [] | [] -> false
in
let is_safe_order l =
let sorted = List.fast_sort compare l in
List.equal (fun a b -> a = b) l sorted
|| List.equal (fun a b -> a = b) l (List.rev sorted)
in
let res = List.filter (fun x -> is_safe_order x && is_safe_adj x) data in
List.length res
;;
let second data =
(* todo *)
0
;;
let _ =
let data = from "input.txt" in
print_endline ("first: " ^ Int.to_string (first data));
print_endline ("second: " ^ Int.to_string (second data))
;;