This commit is contained in:
Mylloon 2024-12-04 22:38:25 +01:00
parent e101e5550e
commit 99c05dbcc2
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -20,17 +20,19 @@ let from input =
;;
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
let is_safe l =
let 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 rec adjacent = function
| x :: y :: tl -> if Int.abs (x - y) >= 3 then true else adjacent (y :: tl)
| _ :: [] | [] -> false
in
order l && adjacent l
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
List.filter is_safe data |> List.length
;;
let second data =